Audrius Butkus
Audrius Butkus

Reputation: 15

Action Script 3 (AS3) how MovieClip(root).MyVariable works?

I am new on AS3, trying to learn it. Trying to create game, with main time line and many movieclips in it. I have a few variable in main timeline, and I want to change them each time by clicking each diferent movieclip. let say, I have stage coordinates cx and cy, and then I click on Any movieclip i want to change these vars to the cordinates the movieclip is. Here what i did:

Main timeline

import flash.events.MouseEvent;
import fl.motion.Color;
import fl.motion.MotionEvent;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.InterpolationMethod;

var cx:int;        //stage coordinate x;
var cy:int;        //stage coordinate y;
var tx:int;        //table coordinate x;
var ty:int;    //table coordinate y;

c1.addEventListener(MouseEvent.CLICK, tracing);
c2.addEventListener(MouseEvent.CLICK, tracing);

function tracing(e:MouseEvent):void {
e.currentTarget.gotoAndPlay(1);
trace(cx,cy,tx,ty);
}

c1 movieclip (symbol name mc1) code on first frame:

MovieClip(root).cx=0;
MovieClip(root).cy=0;
MovieClip(root).tx=0;
MovieClip(root).ty=0;

c2 movieclip (symbol name mc2) code on first frame:

MovieClip(root).cx=85;
MovieClip(root).cy=85;
MovieClip(root).tx=85;
MovieClip(root).ty=85;

the problam is I always get value of the first movieclip I clicked. I kinda found solution for that, but i dont think it should be like that, in each mc1 and mc2 I made second empty keyframe, and now its working, but I dont think it should be like this, what am I doing wrong, any way to do without creating more frames in movieclips?

Upvotes: 0

Views: 1296

Answers (1)

Amy Blankenship
Amy Blankenship

Reputation: 6961

That actually doesn't sound right. I'd suspect it is because you get to the trace statement before you get to the first frame. However, I'd encourage you to abandon this approach now and start building better habits.

Having child clips know about and operate on objects higher up in the heirarchy is a fantastic shortcut--to an unmaintainable mess. Instead, you should either make the cx, cy, tx, ty properties available on them to be read or dispatch an event from them from which these values can be inferred.

Here is a simple example of how you might accomplish this:

class MainDocument extends MovieClip {
   //presume these are stage instances
   public var c1:MovieClip;
   public var c2:MovieClip;
   public var valueRegistry:Dictionary =  new Dictionary();
   protected var cx:int;
   protected var cy:int;
   protected var tx:int;
   protected var ty:int;
   function MainDocument () {
       super();
       if (c1) {
           valueRegistry[c1] = {cx:0, cy:0, tx:0, ty:0};
           c1.addEventlistener(MouseEvent.CLICK, tracing);
       }
       if (c2) {
           valueRegistry[c2] = {cx:85, cy:85, tx:85, ty:85};
           c2.addEventlistener(MouseEvent.CLICK, tracing);
       }
   }
   protected function tracing(e:MouseEvent):void {
      var mc:MovieClip = e.currentTarget as MovieClip;
      mc.goToAndPlay(1);
      var values:Object = valueRegistry[mc];
      if (values) {
         cx = values[cx];
         cy = values[cy];
         tx = values[tx];
         ty = values[ty];
      }
      trace(cx, cy, tx, ty);
   }

}

Note how now you can make those mc's be literally anything--with or without their own code. One optimization I'd suggest is that if cx, cy, tx, ty are always the same value to go with just that value instead of an Object.

Upvotes: 0

Related Questions