PDA

View Full Version : AS2 to AS3, a few basics on passing values...


jt-developer
07-14-2008, 09:41 AM
I've been doing AS2 for years now and my code has always been proceedural rather than OOP, so going from AS2 to OOP AS3 is a massive leap for me. I'm getting my head around using classes, and having separate AS files for each object, including the main class but I still have some problems with some of the more basic attributes that were so useful in AS2 and seem to be a lot more complex in AS3. Could anybody explain these to me....

1.) Say I wanted an object (MovieClip) on the stage to get a vlaue from the main timeline. In AS2 I used to set a variable _root.n = 10; Then from within the MovieClip (which I'll call 'mov1') I could call "trace _root.n" and this would give me the value. I could also add something to that _root variable from either inside mov1 itself (i.e. _root.n+=10) or from the main timeline using some variable in mov1 (i.e. n+= _root.mov1.p). This was the perfect way to share and pass variables. IN AS3 however the rules have changed and I find myself completely lost. For example I have created a root class with a variable n, created a new MovieClip 'mov1' with an attached .as file with a class structure for that Mc and want to access and change the root class's n variable. How would I go about doing that?

2.) I want to create 5 instances of a movieclip, and call them mov1->mov5. In AS2 I simply did a for loop...

for (i=0;i<=5;i++){
_root.attachMovieClip("movtemplate","mov"+i,i++);
}

..Using a similar technique I could then pass details to each of these Mcs at a later date, for example...

for (i=0;i<=5;i++){
_root[mov"+i].n = 10;
}

...with AS3 this sort of thing seems incredibly difficult. I read that you can attach a name variable to each MovieClip that you add to the stage but I dont know whether that is the right way to do it, or how to then send values to a particular MovieClip.

I understand why AS3 can be seen as much easier and better to use, but some things just seem so much more complicated in AS3 than AS2. For example the project I am working on at the moment would take 45 minutes to make in AS2. So far it has taken me 5 days in AS3.

Any help really appreciated.

lsatdown
07-14-2008, 10:43 AM
for answer 2:

for(var i:unit; i<5; i++) {
var movetemplate:MovieClip = new MovieClip();
movetemplate.name = "mov"+i;
}

lsatdown
07-14-2008, 10:44 AM
and to target those:

getChildByName("mov"+i)

jt-developer
07-14-2008, 11:27 AM
Thanks for the reply. I'll have a go at that.

Out of curiosity would I call a mov1's variable x by using something like...

newx = stage.getChildByName("mov"+i).x;

? Also what is the reverse of this, i.e. a child getting a parent's variable rather than parent getting the child's?

thanks again

Canazza
07-14-2008, 12:14 PM
var parentMC:MovieClip = new MovieClip()
var childMC:MovieClip = new MovieClip()
parentMC.addChild(childMC)
parentMC["wibble"] = "wobble"

trace(childMC.parent["wibble"])



is this what you're after?

jt-developer
07-14-2008, 02:58 PM
thanks for that. I'm going to work on it ovee next couple of days. I'll let you know how it goes on.

thanks again

jt-developer
07-14-2008, 06:49 PM
I've done some playing about with the code you both provided and both of them helped. I'm starting to understand it a bit more now I've jumped that first hurdle.

Just to let you know (for anyone who wants to know why I needed to know about passing variables) this is what i was doing:

I was making a simple swf that contained 5 graphics (loaded in from a URL address), placed one below each other. Each one would scroll up on every enterframe. When it reached a certain y position the image would jump back down to the bottom then start moving up again, creating a continual moving group of images.

Next to the images is a button that, when pressed, moved all the image MCs ro the right.

To do this I had a root class that created 5 instances of a movieClip ("mov"). I used a for loop to create the 5 instances ("mov1" to "mov5"). I used the ".name" method to store the names. This class also creates the button that, when clicked, moves the movs to the right.

Within the "mov" movieClip I link to a mov class. This deals with the Y movement of the "mov" instances on every enterframe.

The button to move the "mov" MCs along the X pos is called "Shutter". I link up a "Shutter" class that checks for on mouse click. On mouse click this starts off a function that calls each "mov" instance (mov1 to mov5 using a for loop again), and tells them to move to the right.

Hope that makes sense. And I hope I've structured it correctly. I'll post the code once I've finished it so other people who are having similar problems can check it out.