|
Simple Getter Setter Use - abird.swf
/* This abird class will get compiled then loaded into another swf.
That other swf, the Parent Child Control example, will use the variables that have been get and set in abird,
in order to invoke a method of abird (birdsMovement) and change/know a variable of abird (canFly). */
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class abird extends Sprite
{
[Embed(source="vectors/uprightbirdb.png")]
private var upb:Class;
[Embed(source="vectors/midwaybirdb.png")]
private var midb:Class;
[Embed(source="vectors/catcherbirdb.png")]
private var catchb:Class;
[Embed(source="vectors/flyonebirdb.png")]
private var flyone:Class;
[Embed(source="vectors/flytwobirdb.png")]
private var flytwo:Class;
[Embed(source="vectors/flythreebirdb.png")]
private var flythree:Class;
private var notani:Boolean;
private var canFly:Boolean;
public function abird()
{
notani = true;
canFly = false;
addChild(new upb());
getChildAt(0).name = "up";
addChild(new midb());
getChildAt(1).name = "mid";
addChild(new catchb());
getChildAt(2).name = "catch";
addChild(new flyone());
getChildAt(3).name = "flyone";
addChild(new flytwo());
getChildAt(4).name = "flytwo";
addChild(new flythree());
getChildAt(5).name = "flythree";
getChildByName("flyone").visible = false;
getChildByName("flytwo").visible = false;
getChildByName("flythree").visible = false;
getChildByName("catch").visible = false;
getChildByName("mid").visible = false;
}
/* Here we make use of the get and set ActionScript 3.0 defenition keywords.
When we use "get" the "function" becomes callable as a property, without needing ()
so, birdMove will be a public property, callable by a parent swf.
it will be treated like a property, but really is a function.
notice that the notani var is still accesable.
The set keyword takes one arg and changes a variable in the class to that arg.
Using get and set together (as with fly below) creates a read/write public variable.
Using just get creates a read only property. Just set, write only.
You can not use getters and setters in the Actions panel of Flash,
they can only be used in packaged ActionScript code in .as files.
*/
/* This public variable/function (called a getter)
will be used so that a parent swf can invoke the metod it returns */
public function get birdMove():Function
{
return birdsMovement;
}
private function birdsMovement(delay:Number, interv:Number = 6):void
{
/* notice that this function is never called from this class
instead, a parent swf must use the birdMove variable to invoke this function */
if (notani == true) {
var catchAnimation:Timer = new Timer(delay, interv);
(canFly) ? catchAnimation.addEventListener(TimerEvent.TIMER, doFly, false, 0, true) :
catchAnimation.addEventListener(TimerEvent.TIMER, doCatch, false, 0, true);
catchAnimation.start();
}
}
public function set fly(f:Boolean):void
{
canFly=f;
}
public function get fly():Boolean
{
return canFly;
}
private function doCatch(e:TimerEvent):void
{
switch (e.target.currentCount) {
case 1:
notani = false;
visibleOrder("up", "catch", "mid");
break;
case 2:
visibleOrder("up", "mid", "catch");
break;
case 3:
visibleOrder("up", "mid", "catch");
break;
case 4:
visibleOrder("up", "mid", "catch");
break;
case 5:
visibleOrder("up", "catch", "mid");
break;
case 6:
visibleOrder("catch", "mid", "up");
e.target.stop();e.target.reset();
e.target.removeEventListener(TimerEvent.TIMER, doCatch);
notani = true;
break;
}
}
private function doFly(e:TimerEvent):void
{
if (getChildByName("up").visible == true || getChildByName("catch").visible == true ||
getChildByName("mid").visible == true)
{
getChildByName("up").visible = false;
getChildByName("catch").visible = false;
getChildByName("mid").visible = false;
}
switch (e.target.currentCount) {
case 1:
this.y -= 1;
notani = false;
visibleOrder("flythree", "flytwo", "flyone");
break;
case 2:
this.y -=1;
visibleOrder("flythree", "flyone", "flytwo");
break;
case 3:
this.y -= 2;
visibleOrder("flythree", "flyone", "flytwo");
break;
case 4:
this.y -= 2;
visibleOrder("flytwo", "flyone", "flythree");
break;
case 5:
this.y -= 2;
notani = true;
visibleOrder("flyone", "flytwo", "flythree");
break;
case 6:
this.y -= 1;
visibleOrder("flythree", "flytwo", "flyone");
e.target.stop();e.target.reset();
e.target.removeEventListener(TimerEvent.TIMER, doCatch);
break;
}
}
private function visibleOrder(cone:String, ctwo:String, cthree:String):void
{
getChildByName(cone).visible = false;
getChildByName(ctwo).visible = false;
getChildByName(cthree).visible = true;
}
}
}
|