0

I'm using Actionscript 2.0 for a mobile phone and can't get my head around Events.

I'm creating a class object with all my code and using a group of functions (all as direct 1st level children of the class). There's one function that creates a Movieclip with a square on it and sets the onPress event to another function called hit:

public function draw1Sqr(sName:String,pTL:Object,sSide:Number,rgb:Number){
    // create a movie clip for the Sqr
        var Sqr:MovieClip=this.canvas_mc.createEmptyMovieClip(sName,this.canvas_mc.getNextHighestDepth());
    // draw square
        Sqr.beginFill(rgb); 
        //etc  ...more lines        

    //setup properties (these are accessible in the event)
        Sqr.sSide=sSide;
        Sqr.sName=sName; 

    //setup event
        Sqr.onPress = hit; // this syntax seems to lead to 'this' within
                            // the handler function to be Sqr (movieclip)

        //Sqr.onPress = Delegate.create(this, hit); 
        //I've read a lot about Delegate but it seems to make things harder for me.
    }



Then in my event handler, I just cannot get the scope right...

public function hit(){
    for (var x in this){
        trace(x + " == " + this[x]);
    }
            //output results
                //onPress == [type Function]
                //sName == bSqr_7_4
                //sSide == 20

    trace(eval(this["._parent"])); //undefined
    trace(eval(this["._x"])); //undefined

}

For some reason, although the scope is set to the calling object (Sqr, a Movieclip) and I can access properties I defined, I can't use the 'native' properties of a Movieclip object.

Any suggestions on how I can access the _x, _y and other properties of the Movieclip object that is pressed.

Ferric
  • 5
  • 1
  • trace(eval(this["._parent"])); will not return what you expect. You are using the array accessor and the dot accessor at the same time. Try this: trace(this._parent); and this: trace(eval(this["_parent"])); They should be the same. Any luck? – Ross Smith Jan 30 '12 at 21:07
  • As for the results of your iteration, I recall AS2 being screwy on this front. IIRC only dynamic properties are returned when looping with for ... in. This prevents Objects (which often serve as hash maps) from including their native properties when all you want are the key/value pairs you set yourself. That or I'm full of horse manure. – Ross Smith Jan 30 '12 at 21:08
  • Thanks @RossSmith, those were really helpful comments. By just using the array form OR the dot form I could access the properties. (Following some further reading I also decided to drop the eval function, which I think I've been overusing.) You're definitely right about for...in for dynamic properties in AS3 so probably also in AS2. This seems a counter-intuitive 'feature' to me. If you re-post your comments as an answer, I'm happy to accept it. Many thanks. – Ferric Jan 31 '12 at 09:49

1 Answers1

0

Use the array accessor or the dot accessor, but not both. For example:

trace(this._parent); // OR
trace(this["_parent"]);

As for the results of your iteration, I recall AS2 being screwy on this front. IIRC only dynamic properties are returned when looping with for ... in. This prevents Objects (which often serve as hash maps) from including their native properties when all you want are the key/value pairs you set yourself.

Also - the eval() function can be easily overused. Unless you absolutely must execute a String of AS2 that you don't have at compile-time I would recommend avoiding it. Happy coding!

Ross Smith
  • 702
  • 4
  • 5