View Full Version : How do I adress instances by variables ?
Kae
January 7th, 2003, 01:15 AM
Hello.
I'm sitting here stuck with a Flash Mx dilemma:
How can I use a object instance with a name stored in a variable ? Im going to run hittests on two arrays containg the names of objects I want to check for collision, but I can't get this to work.. so please help me if you can.
Ok. To make it clearer, this is kinda the script I did:
for (i=1;i<10;i++){
ObjName1="_root.PLANE"+i;
ObjName2="_root.ROCKET"+i;
Here I wanna check if they collide... can this be done in any way by using String information ? I know THIS code can be solved in other ways...
if (ObjName1.hitTest(ObjName2)){
ObjName1.gotoAndPlay(20);//Play crashanim;
}
}
Thanks for any help.
/ Kae
lostinbeta
January 7th, 2003, 01:19 AM
Well I have never worked with collision detection before, but I do know a good place to start here....
"_root.PLANE"+i; is executed wrong.
The proper way to execute it would be...
_root["PLANE"+i]
This searches all the items in your _root to look for anything that has the instances name PLANE+i (aka PLANE1, PLANE2, PLANE3, etc)
Kae
January 7th, 2003, 08:16 AM
Thanks !
Im kinda new at Flash, mostly worked with Director before, but I think that your tip solved my issue.
Thanks a lot.
/ Kae
senocular
January 7th, 2003, 10:02 AM
[ this post (http://www.kirupaforum.com/forums/showthread.php?s=&threadid=12082#post85182) ]
to expand on loastinbeta's post, just fyi, _root will only work if your objects exist in _root... that prefix can be any scope and can reference any property/object witin it, ie:
_root.circle["square"];
_root.myMC["_x"];
i = 1;
this["mc"+i];
^ that (using this) is common for referencing things in the same scope as the code
this form of referencing is "associative array" referencing, which uses array (or 'list' for director folk) format to access variables/properties/objects via their name as a string.
ObjectScope["variable/propertie/object"];
in essence it replaces .propName
This format can also be used heirarchically on itself ie:
_root["circle"]["square"]["_y"] = 10;
and can also reference functions/methods
_root["gotoAndPlay"](1);
(note the parens are not part of the string and are used on the outside of the '[]'s)
also look at this example for initiating a constructor call from the 'test' class.
test = function(a){
this.a = a;
}
obj = new ["test"]("my a");
trace(obj.a); // traces my a
Basically it all boils down to this method of retrieving object properties through a string using brackets ([]). Arrays themselves are nothing more than generic objects with numbered properties. consider the following:
ary = ["x","y","z"];
trace(ary.0); // gives error since numbers cant be used as variable/property names
trace(ary["0"]); // traces x
obj = {a:"x", b:"y", c:"z"};
trace(obj.a); // traces x
trace(obj["a"]); // traces x
The only thing different about an array compared to a basic object is that it has properties with numbered names. You can reference these directly with dot syntax because it causes an error in flash as flash treats that number as a number and not a property so the "associative array syntax" is forced to allow you to gain access to that ary.0. This being the case, Objects can be treated as arrays all the same:
obj = {};
obj["0"] = "a";
obj[1] = "b";
trace(obj[0]); // traces a
trace(obj[1]); // traces b
The only thing is that arrays, through their prototypes gain certain functions and methods which are not present in generic objects.
anywho, if this is all confusing then just know that
_root.clip1
can also be written as
_root["clip1"]
where you can replace .name with ["name"]
NOTE: in using [] notation, the brackets have to be preceded by an object reference so that they know what object to reference the property or object from. ex:
incorrect:
["mc"+i]
correct:
this["mc"+i]
An exception being functions, which, because of the parentesis as a way to "ground" the [] notation into being a reference, a preceding object reference isnt needed and this is assumed
["function"+i]();
==============================================
the following was been added, adopted from another post
==============================================
the array notation [] is using am associative method of aquiring properties or objects from its target. Make any sense? didnt think so ;) So Ill start from the basics.
:Arrays:
arrays are a list of "things" contained in one variable where each "thing" is accessable through using arrayName[index number]. The index number is a number representative of its position in the array.
myArray = ["Joe","Bob","Harry","Senocular"]
where...
myArray[0] is "Joe"
myArray[1] is "Bob" etc.
Easy stuff (once you get the hang of it ;)) This is referencing indexically - using numbers to retrieve your "thing"
:Objects:
objects are like arrays but you can use a variable name to reference a "thing" rather than a number. This the associative part. "Associative arrays" are arrays with elements that are accessable through a name rather than a number. For all events and purposes in this example, you can consider your basic object to be an associative array, which at its basics it pretty much is.
myObject = {neightbor:"Joe", brother:"Bob", father:"Harry", sexiestManAlive:"Senocular"} // ;)
where...
myObject.neightbor is "Joe"
myObject.sexiestManAlive is "Senocular" ...naturally. etc.
Here instead of using numbers to get a "thing" we are using variable names.
:Objects Like Arrays:
Now when you look at it, an object is very similar to array in that it has many elements and each of these elements are accessed through first referencing the object name then the "thing" you want to access. Here is where you get the associative array play. Assuming an object is like an array, then you should be able to say object[index]... which you can, but since objects use variable names to reference their "things" instead of a number as an index, we use a string to represent the variable it is we want to access, i.e. object["variableName"]. where
myObject["sexiestManAlive"] is "Senocular"
Note that there is no "." between myObject and ["sexiestManAlive"] - its just being accessed like you would a normal array.
:All the World's an Object:
Now think about flash and Movieclips. All movieclips are objects. They can have variables in them and you can access these variables using movieclipName.variableName - as well as other movieclips in the same format. This being said, then we know that to access a variable in a movieclip you can also say movieclipName["variableName"] or movieclipName["movieclipName"] !! See the connection? So in treating you movieclips like arrays to access their contents, you can say things like
_root["HMbar"]["slideOut"]["HM1"]["HM1"]
//which is the same as
_root.HMbar.slideOut.HM1.HM1
however, we can add expressions in the brackets [] to add variation in what it is we want to access
n = 1
_root["HMbar"]["slideOut"]["HM"+n]["HM"+n]
which is great for loops and things of the such
^ in that example, _root is the base scope where we then reference HMbar in that and slideOut in that etc, but if something exists in the current scope then you can use this[]
myVar = 5
this["myVar"] is 5
pom
January 7th, 2003, 10:50 AM
One last thing,
for (i=1;i<10;i++){
ObjName1="_root.PLANE"+i;
ObjName2="_root.ROCKET"+i;
}doesn't make any sense, because you're overwritting ObjName1 and ObjName2 each time the lop runs. Maybe Sen said it in his post, I didn't read all of it. If so, sorry :beam:
senocular
January 7th, 2003, 11:08 AM
its actually fine in that instance since those variables are only being used for that short time in the for loop. Thats when the hitTest is checked and the gotoAndPlay is resolved or not. Afterwards its fine if the variables get overwritten since they arent used again (supposedly).
pom
January 7th, 2003, 11:09 AM
OK, I thought that it was 2 separate pieces of code :beam:
senocular
January 7th, 2003, 11:28 AM
its something to definitely look out for though, it happens a lot :)
lostinbeta
January 7th, 2003, 01:29 PM
Whew..... I am glad that was just an expansion on my post. I thought you were going to prove me wrong again and make me feel AS illiterate ;)....lol. But I don't because I already knew all the stuff you said... WOO HOO!! :beam:
senocular
January 7th, 2003, 01:34 PM
if you knew all that (which Im not surprised at all ;)) then you are far from AS illiterate... and of course I never mean to make you feel that way :) think of what I say just being an 'alternative' without any presumed attempts to make you feel inferior or ignorant.
lostinbeta
January 7th, 2003, 01:46 PM
Nonono, I learn so much from your posts actually. I believe it is better to know more than 1 way to do something.
Guig0
January 7th, 2003, 02:08 PM
If you donīt mind, Iīm going to put this thread on best of kirupa, so other ppl can find it too.:)
No, wait, Iīm not a mod :P is there a good mod that can do that? :geek:
At least I rated this thread a 5, thatīs all I can do:crazy:
lostinbeta
January 7th, 2003, 02:14 PM
It is usually Ilyas that decides what goes in there, so we will let him make the final decision.
I think it should too though :goatee:
Guig0
January 7th, 2003, 02:20 PM
if he knows what is best for him, know what I mean?;)
pom
January 12th, 2003, 03:22 PM
Yeah, right... :sure:
Guig0
January 13th, 2003, 07:41 AM
:P :P
senocular
April 17th, 2004, 12:56 PM
_root[las]["txt"].setTextFormat(_root.defTxt);
[] syntx replace .propname
to continue normal referencing you'll need to include the "." again after []
4days
April 17th, 2004, 12:58 PM
lol, i was just moving my post having noticed this was a 'best of' forum :)
here it is again, thanks for your help - that works perfectly. you're a star :)
------------------------------------------------------------
nice post senocular :)
total newb to flash and it helped me a lot, gj.
a bit stuck with the syntax though. i'm positive there's a much easier way to do it - but what i'm trying to achieve is a basic menu with mouseovers and this is what i've got so far:
(not brilliant at explaining things so an example seemed like a good idea - the commented out line in each of the 'item' options is the one i'm having trouble with)
file: mouseovers.fla (http://www.l0unge.com/flash/mouseovers.fla)
the problem is that i don't know how to make the previously selected item return to the default colour when another item is selected.
have tried setting the value of a variable ('las' in the example below) to the name of another variable, which works up to a point - it's okay for statements like "foo = bar" but not for applying functions to things, eg:
_root[las]["txt"]["text"] = "test";
works just fine, but:
_root[las]["txt"]setTextFormat(_root.defTxt);
does not. neither does:
_root.""+las.txt.setTextFormat(_root.selTxt);
if i make it literal (without using the 'las' variable to represent 'item_1')
_root.item_2.txt.setTextFormat(_root.selTxt)
that works too - just don't know what the correct syntax is to be able to use a variable name as a variable :(
appreciate i'm going round the houses to do something quite simple, but that's how i learn (slowly and inefficiently but eventually :))
solutions/alternatives much appreciated.
senocular
April 17th, 2004, 01:12 PM
Thats ok ;) best of posts can be expanded upon if the new topic is relative and not covered. New posts in the forum aren't a good idea though :)
Ozeona
August 8th, 2005, 06:25 AM
Hi
wonder if this thread is still actively read??
anyway, i was reading through the explanations of Senocular and found that its related to the problem im facing. Currently i have 2 arrays, and the first one should respond to the second one base on a same variable.
i.e:
first_array[ "obj1", "obj2", "obj3", "obj4"]
second_array [ ""obj1", "obj2","obj4"]
so if first_array's variable matches second_array, then they will display something in a listbox. However, they arr arrays, so i cannot access their via the content inside. My question is: how do i change the array to objects?
Not sure if thats possible.. but hope its something that can be done.
btw i think i messed up this forum? coz i currently have another thread thats about this problem too. Hope its not too messy.
carlmal
December 22nd, 2006, 01:32 PM
Just wanted to say that the tutorila on Dynamic Movie Clips and the ScrollPane was almost exactly what I was looking for, thanks a ton! Now back on topic, how do I create a var that includes the counter in a for loop?
var this["appNm_tf"+i]:TextField = mcMain.createTextField("appNm_tf"+i, getNextHighestDepth() , 0, i * 20, 150, 20);
Doesn't work.
Thanks,
Carl
TheCanadian
December 22nd, 2006, 03:05 PM
Just wanted to say that the tutorila on Dynamic Movie Clips and the ScrollPane was almost exactly what I was looking for, thanks a ton! Now back on topic, how do I create a var that includes the counter in a for loop?
var this["appNm_tf"+i]:TextField = mcMain.createTextField("appNm_tf"+i, getNextHighestDepth() , 0, i * 20, 150, 20);
Doesn't work.
Thanks,
Carl
Read the thread that you created ;)
http://www.kirupa.com/forum/showthread.php?t=244837
bombsledder
December 23rd, 2006, 02:55 PM
im not sure if anyone said it yet but this might help you understand it
_root["myMovie"]["_x"] = 150;
is the same as saying
_root.myMovie._x = 150;
just a little easier on the eyes :pa:
carlmal
December 24th, 2006, 02:20 PM
Thanks, I'm starting to get the hang of it. I just started using AS in the last couple of weeks. I've noticed, I'm getting a lot more comfortable with it over this weekend.
I'm getting by with search results from this great forum and tutorials, instead of getting you fine folks to hold my hand too much. Profuse gratitude!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.