Showing posts with label getter. Show all posts
Showing posts with label getter. Show all posts

Monday, October 24, 2011

Actionscript 2: One Way to catch when a MovieClip becomes visible

In my previous post, I outlined the difficulties that I have been having reacting to when a MovieClip becomes visible.  I think the big reason for these difficulties is that _visible is a MovieClip property that is impossible to override with getters and setters.

However, I happened to be trolling around Senocular's site and found his MovieClipProxy class.  This class creates an object with all of the standard MovieClip properties with getter and setter functions and all the standard MovieClip methods together with the standard functions, like onEnterFrame, envoked using the call statement and the correct scope.

If I change the setter and getter for _visible as follows:

 function get _visible():Boolean {
  trace("get _visible");
  return _mc._visible; 
 }
 function set _visible(v:Boolean):Void {
  trace("set _visible");
  this._xscale *=2;
  _mc._visible = v; 
 }

Note that I am just setting the _xscale when _visible is set just for a double visual confirmation.

If you create a new Actionscript 2 .fla tester file with the code:

import edu.clips.test.MovieClipProxy;

var mcp:MovieClipProxy = 
 new MovieClipProxy(this, "test", 1);

trace("_xscale before first set _visible "+mcp._xscale);
mcp._visible = false;

mcp.clear();
mcp.lineStyle(2, 0x0000FF, 100, true);
mcp.moveTo(0, 0);
mcp.lineTo(200, 100);

trace("_xscale before second set _visible "+mcp._xscale);
mcp._visible = false;
trace("_xscale after second _visible "+mcp._xscale);

this.test._visible = true;
trace("_xscale after movieClip set _visible "+mcp._xscale);
trace("or "+this.test._xscale);

and test it, you get the output:

_xscale before first set _visible 100
set _visible
get _visible
_xscale before second set _visible 200
set _visible
get _visible
_xscale after second _visible 400
_xscale after movieClip set _visible 400
or 400

When you reference the MovieClipProxy object mcp, the setters and getters are used and applied to the  MovieClip this.test.  When you reference this.test._visible directly, the setter is not used.

Now you are in the position to capturing the setting of _visible and do something more sensible than upscaling, as long as you create a MovieClipProxy and reference it.

This still leaves the question of whether this approach can be bolted on to the method we are using to create Classes that extend MovieClip.


Actionscript 2: Trouble overriding setter for MovieClip Properties like _visible

In my previous post, I described how we create classes to extend MovieClip.

Continuing with the ubSquare example, now let's suppose that I want to scale my square whenever it becomes visible.  Well, _visible is a property of the class, just like side was so how about adding some code like:

//at the top with _side
private var __visible:Boolean = true;

//with the side getter and setters
public function get _visible():Boolean{
 trace("in ubSquare get _visible");
 return this.__visible;
}
public function set _visible(newVis:Boolean):Void{
 trace("in ubSquare set _visible");
 this.__visible = newVis;
 this._visible = newVis;
}

This is just like what I did for side, except that I can't just change my private __visible property and expect the movieclip to change, so I add the last line to tell the movieclip to actually change its visibility.

There are two problems with this:
  1. If I put a line like:
    this.mySquare_ubS._visible = false;
    in my tester, the set function is not called.
  2. If I put a line like
    this._visible = true;
    in my scale method, I get 256 levels of recursion since the set for _visible calls itself.
One way to fix the second problem is to change the last line of the set _visible function from:
this._visible = newVis;
to
setProperty(this, _visible, newVis);

The output becomes:

scale making movieclip visible true
in ubSquare set _visible
in ubSquare get _visible

This, of course, begs three questions:
  1. what is the point of writing a setter for _visible if it can be avoided by using a (deprecated) setProperty operation?
  2. why is a get _visible call made when I set _visible? The line in the scale method (this._visible = true) calls both the setter and the getter.  I was sure I had a good link that explained why but cannot for the life of me find it now.
  3. how do I solve the first problem which was the important one?  I want to be able to notice when anything makes my square visible, not just when some of my own class code does.
I have figured out one way to catch the setting of _visible for a MovieClip, which I will blog about next, but have yet to figure out how to apply it to a Class that extends MovieClip.