CreateJs MovieClip Instance name - html

Hi Guys (sorry for my bad english), I've stuck with this in a week, is it possible to get the movieclip instance name with createJS (I've put the movieclip manually on the stage and I give it an instance name), in AS3 I can use movieclip.name to get the instance name, how can I do that in createJS, when I use this.movieclipName.name it always return null in console log, do You have any suggestion? Thanks for Your help?

#Ferry thank You very much for Your solution. finally I figured it out, We can't get the instance name of the object by myObject.name because the name of the object is not signed previously (it looks like the instance name not same as name in html5). that's why it always giving a null. This is my script to get all the instances name of my objects.
for (var a = 0; a <= maxObject - 1; a++) {
var myObjectName = 'object' + (a + 1);
var myObject = this[arrObject[a]];
myObject.name = myObjectName;
}
Thank you guys for all of your respon.

i solve this problem using:
for (i = 1; i < 6; i++) {
this["m" + i].addEventListener("click", clickme.bind(this));
this["m" + i].hamdy=i;}
so you can use var hamdy to pass any change in clickme funaction.

Related

ActionScript : Adding value to object name?

Struggling to find the answer. Right so I have a variable which is storing a number, and I have a movieclip called "note". I want to add the number onto the end of the note while adding it to the stage. Any suggestions please ?
var num = 1;
addChild(note); // Should be note1
Edit: Thanks for all your help, i've solved the solution. What I have done is created an array for of the movieclips and used a for statement to go through all of the movieclips.
If note here is the name of your class (AS linkage), and you want that your instance be called note1, for example, your code can be like this :
var num:int = 1;
this['note' + num] = new note();
addChild(this['note' + num]);
Hope that can help.
Try this
import flash.utils.getDefinitionByName;
import flash.display.MovieClip;
var num = 1;
var cl:Class = getDefinitionByName("Note"+num) as Class;
addChild(new cl() );

SimpleButton(getChildByName(button_name)) returns null

I just started learning AS3 and I'm stuck.
I'm trying to get the instance names of some buttons from an XML file and then make those buttons disapear, but there is a problem. When I try to locate the buttons, the getChildByName
method I use returns null.
Here is my code:
for (var i:int = 1; i < countries.item.length(); i++) {
var button_name:String = countries.item[i].Name.toString();
trace(button_name);
var button1:SimpleButton = SimpleButton(getChildByName(button_name));
trace(button1);
button1.visible=false;
}
As you see, I check if I get the instance names and trace(button_name) works fine.
Now, trace(button1) on the other hand returns null and I don't understand why.
Any suggestions please?

How to push instantiated MC into Array dynamically?

Im really stuck. I have 5 MC´s that are being spliced from one array at a certain time. In that same function I want to push another movieclips into another array. The two arrays holds mc's that represent right or wrong answers. So when one question is being given a correct answer that questions visualisation alters.
This function holds a incrementing variable as I do want the mc's to be pushed by the user and one at the time. The thing is I cant seem to refer them properly.
I´ve tried
pQuestSum = this[pQuest + pQuestNumber];
and
pQuestSum = this[pQuest] + pQuestNumber;
and pretty much everything I´ve imagined would work...but the problem is I havent tried
the right thing.
when I trace pQuestSum (which would be the reference) I get an error saying thats its not a number.
this is one of 5 mc's named from 1-5:
var passedquest1:PassedQuest = new PassedQuest();
this is the vars that i try to to build a reference of
var pQuest = "passedquest";
var pQuestNumber = 1;
var pQuestSum;
var questCorrArray:Array = [];
if(event.target.hitTestObject(questArray[ix])){
removeChild(questArray[ix]);
questArray.splice(ix,1);
pQuestNumber ++;
pQuestSum = this[pQuest] + pQuestNumber;
trace("pQuestSum"); // NaN
questCorrArray.push(pQuestSum);
//trace(questArray.length);
pointsIncreased = false;
questPoints = 0;
}
How do I refer an existing movieclip when the reference consists of both a string and a number? Hope I made myself somewhat clear:)
If you had an instance of an object on your timeline called "passedquest1" (as an example), then you could access it this way:
var myObj = this["passedquest" + 1];
Or,
var pQuest = "passedquest";
var pQuestNumber = 1;
var myObj = this[pQuest+ pQuestNumber.toString()];
When you do this: pQuestSum = this[pQuest] + pQuestNumber;, you are trying add the number to an object (this[pQuest]), unless you have number/int var called "passedquest", this will result in NaN.

AS3 class instances names

I really wonder. I made a MovieClip class Apple, I wrote a function that creates a new instance with a name "apple". Each time a new instance is pushed into an Array "apples". I call the function 5 times and I get 5 apples. I can manipulate them by calling them i.e. apples[0]. And when I trace my array I see 5 [object Apple] things. So maybe I don't really understand the structure of AS3 objects but shouldn't each object have a name?
When I set apple.name and get an array with 5 different names, I can't manipulate objects by names like apple1.x = 10. How does computer know which apple is where if each has own coordinates? Is the only way to call them: apples[0]-apples[4]? And if I create a code that should be same for all apples, how should I address the function, to "this"? Cause when I write class code I don't have any names yet...
For example, if I want to make Apple class a picture(MovieClip) that can be dragged, create any number of apples, up to a million, I can't possibly add apples[0].addEventListener, apples[1].addEventListener ... apples[1000000].addEventListener to the code. How do I make it global?
I'm asking cause when I'm directly coding for a specific instance, it has a name and I know exactly what am I addressing. And working with a class and making many objects I kinda don't... Sorry, I'm green
You don't need names to be able to access and manipulate your instances.
You can do something like this:
var apples:Array = new Array();
for (var i:int = 0; i < 100; i++)
{
var apple:Apple = new Apple();
apples.push(apple);
apple.x = Math.random() * 500;
apple.y = Math.random() * 500;
addChild(apple);
apple.addEventListener(MouseEvent.CLICK, onAppleClick);
}
function onAppleClick(e:MouseEvent):void
{
var ind:int = apples.indexOf(e.currentTarget);
var apple:Apple = apples[ind];
trace(ind);
apples[ind].visible = false;
//or
//apple.visible = false;
}
The same problem, huh? :)
For example Apple extends Sprite and inherits property name.
It's public, this means that you can change its name.
var apple:Apple = new Apple() ;
apple.name = "MegaApple" ;
Also, if you add the apple to stage, you can get him via name.
stage.addChild(apple) ;
get apple back:
var oldApple:Apple = stage.getChildByName("MegaApple") ;
But that never means, that you can use apple like that:
MegaApple.move() - because name of apple is supposed to be a property of apple, not a name of a variable.
Of course, you can create a lot of apples manually:
var apple1 = new Apple() ;
var apple2 = new Apple() ;
var apple3 = new Apple() ;
But if they all behave the same way, there is no point in doing. That's why you store them into array.
Hope you understand what I mean.

What does this mean in AS3?

Hello I've started to learning AS3 from one book and found something I don't understand.
Ellipse(_board[row][column]).fill = getFill(row, column);
_board is two dimensional array of Ellipse type, so I just dont understand why is Ellipse(Ellipse object) used when it apparently works without it, or I haven't seen any changes when I omitted it.
Ellipse(_board[row][column]) is a type cast Type(object)
Because you can push anything into an Array the compiler doesn't know what kind of objects are stored within the Array. So it is good style to cast objects if you retrive them from an Array to their correct type.
This has several benefits:
if such an object is not of the type you expect or null, you will know when you typecast instead of getting an error later somewhere far away
the code executes a bit faster if you are explicit about the types
if you use a good IDE it can provide you with autocompletion when it knows the types
_board is a multidimensional array filled first with Arrays.
In the BoardDisplay.mxml
(Hello! Flex 4 : Chapter 3. Hello Spark: primitives, comp... > FXG and MXML graphics—building a game.. - Pg. 80) ,
<Graphic version="1.0" viewHeight="601" viewWidth="701"
xmlns=" library://ns. adobe. com/flex/spark"
xmlns:fx=" http://ns. adobe. com/mxml/2009"
xmlns:mx=" library://ns. adobe. com/flex/halo"
initialize="createBoard()"
click=" clickHandler(event)">
initialize calls createBoard().
private function createBoard():void {
newGame();
_board = new Array(6);
for (var row:int = 0; row < 6; row++) {
_board[row] = new Array(7);
for (var col:int = 0; col < 7; col++) {
_board[row][col] = addEllipse(row, col); //Magic Happens!
}
}
}
addEllipse returns Ellipse to each entry in _board
private function addEllipse(row:int, col:int):Ellipse {
var ellipse:Ellipse = new Ellipse();
ellipse.x = 4 + col*100;
ellipse.y = 5 + row*100;
ellipse.width = 90;
ellipse.height = 90;
ellipse.fill = getFill(row,col); // Magic Found !
ellipse.stroke = new SolidColorStroke(0x000000, 1, 1.0, false,"normal", null, "miter", 4);
boardGroup.addElement(ellipse);
return ellipse;
}
The author casted it as maxmx said but did not really need to as all the entries were of type Ellipse so
Ellipse(_board[row][column]).fill = getFill(row, column);
can work as
_board[row][column].fill = getFill(row, column);