ActionScript : Adding value to object name? - actionscript-3

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() );

Related

CreateJs MovieClip Instance name

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.

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.

Obtain a Class reference to Vector.<T>

Is it possible to obtain a Class reference to a Vector.<T>? I tried the following code:
var cls : Class = Vector.<int>;
But it fails with a coercion error, presumably because Vector.<T> is also a global function. Is there a simple way around this?
Edit: Best solution:
var vectorIntDefinition:Class = Vector.<int> as Class;
I don't know if you call this solution simple as you need some "reflection magic" but it works:
var vectorIntClassName:String = getQualifiedClassName(Vector) + ".<" + getQualifiedClassName(int) + ">";
var vectorIntDefinition:Class = getDefinitionByName(vectorIntClassName) as Class;
Hint: If you use that more than once you could create a little helper method.
Edit: Look at my 2nd comment.

AS3 How to instantiate a class with getdefintionbyname and getqualifiedbyclass with TYPED declaration

example:
var c : Class = Sprite;
//This can be random class such as movieclip/etc
var o = getDefintionByName(getQualifiedClassName(c));
this works, but in flash develop,
it says that the variable 'o' has no type declaration
which basically means
var o : SOMETHING = getDefintionByName(getQualifiedClassName(c));
but how do i put that something there when i do not know what its coming because of random classes?
one solution would be using
var o : * = getDefintionByName(getQualifiedClassName(c));
the asterick symbol is a temporary one, but it works
Create an interface or a base class for the classes you wish to instantiate and then type the var to that.
var o:ICustomClass = ...
or
var o:BaseClass = ...