|
Parent Child Control - Control of a child swf that has getters set up in it
/* Click on the swf first, then when you press space the bird will peck for worms.
Eat 100 worms and the bird will be able to fly. Move closer to the tree and the worms drop faster.
Using the getters and setters of the abird,
this swf controls the abird swf that is loaded in at run time.*/
package
{
import flash.display.*;
import flash.events.*;
import flash.net.*;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.utils.Timer;
/* the liaison class is imported in */
import classes.liaison;
[SWF(frameRate = '30', backgroundColor = '0x000000', width = '650', height = '500')]
public class parentChildControl extends Sprite
{
private var theBird:Sprite;
private var theBack:DisplayObject;
private var wormCreator:Timer;
private var birdHurt:Number = 100;
private var status:TextField;
private var wormCount:Number = 0;
private var wormSpawnSpeed:Number = 3000;
private var framesPerSecond:Number = 30;
private var birdl:liaison;
[Embed(source="vectors/aworm.png")]
private var worm:Class;
/* constructor function */
public function parentChildControl()
{
/* Here we load the abird swf we created earlier */
var bL:Loader = new Loader();
bL.load(new URLRequest("abird.swf"));
bL.contentLoaderInfo.addEventListener(Event.COMPLETE, getStartedOne);
}
/* loader complete function for the abird swf file */
private function getStartedOne(e:Event):void
{
theBird = e.target.content as Sprite;
var backL:Loader = new Loader();
backL.load(new URLRequest("vectors/vgamebacksun.jpg"));
backL.contentLoaderInfo.addEventListener(Event.COMPLETE, getStartedTwo);
}
/* loader complete function for the background scene jpg file */
private function getStartedTwo(e:Event):void
{
theBack = e.target.content as DisplayObject;
addChild(theBack);
theBird.y = theBack.height - theBird.height - 10;
addChild(theBird);
theBird.addEventListener(Event.ENTER_FRAME, statusShow);
status = new TextField();
status.selectable = false;
status.autoSize = TextFieldAutoSize.LEFT;
status.multiline = true;
addChild(status);
stage.addEventListener(KeyboardEvent.KEY_DOWN, controlBird);
stage.addEventListener(KeyboardEvent.KEY_UP, upNess);
wormCreator = new Timer(wormSpawnSpeed, 0);
wormCreator.addEventListener(TimerEvent.TIMER, createWorms);
wormCreator.start();
birdl = new liaison(theBird);
}
/* main game event functions */
private function statusShow(e:Event):void
{
status.x = theBird.x - 3;
status.y = 1;
status.text = "Worms eaten: "+wormCount+"\n"+
"Health: "+e.target.alpha+""+"\n"+"Worm Drop Delay: "+wormSpawnSpeed;
e.target.alpha = (100/birdHurt >= 0.05) ? 100/birdHurt : 0.05;
if (e.target.alpha <= 0.05) {status.text = "Game Over";wormCreator.stop();}
if (wormCount >= 100) {status.text = "You Won!";wormCreator.stop();}
wormSpawnSpeed =
((theBack.x + theBack.width - 470) - theBird.x > 10) ?
((theBack.x + theBack.width - 470) - theBird.x) : 10;
}
private function createWorms(e:TimerEvent):void
{
if (status.text != "Game Over" && status.text != "You Won!") {
var aworm:DisplayObject = new worm() as DisplayObject;
aworm.x = theBack.x + theBack.width - 470;//470 was arrived at by trial and error
aworm.y = 250;//same with this 250
aworm.addEventListener(Event.ENTER_FRAME, wormMovement, false, 0, true);
addChild(aworm);
}
}
private function wormMovement(e:Event):void
{
var tWorm:DisplayObject = e.target as DisplayObject;
if(tWorm.y < (theBird.y+theBird.height-45))
{tWorm.y +=45;}
tWorm.x -=3;
if (tWorm.hitTestObject(theBird) && theBird.getChildByName("catch").visible == true
&& tWorm.x >= ((theBird.x + theBird.width)-(tWorm.width+10)) &&
tWorm.x <= ((theBird.x + theBird.width)-(tWorm.width-5)) )
{
wormCount++;birdHurt-=0.8;tWorm.removeEventListener(Event.ENTER_FRAME, wormMovement);
tWorm.visible = false;removeChild(tWorm);
}
else
{
if (tWorm.hitTestObject(theBird) && theBird.getChildByName("catch").visible == false &&
tWorm.x < ((theBird.x + theBird.width)-(tWorm.width-15))) {birdHurt+=0.5;}
}
if (tWorm.x < 0) {tWorm.removeEventListener(Event.ENTER_FRAME, wormMovement);removeChild(tWorm);}
}
private function controlBird(e:KeyboardEvent):void
{
var delayer:int = Math.round(1 / framesPerSecond * 1000);
switch (e.keyCode) {
/* Below are examples of how we call the birdMove function/property using the liaison class,
thiss is possible for a DisplayObjectContainer loaded in,
or given the same ApplicationDomain, that has public getters set up to be used in this way */
/* space bar */
case 32:
if (status.text == "You Won!") {birdl.setP("fly", true);}
birdl.invoke("birdMove", delayer, 6);
break;
/* up */
case 38:
if (birdl.getP("fly") == true) {birdl.invoke("birdMove", delayer, 6);}
break;
/* down */
case 40:
if (birdl.getP("fly") == true) {
birdl.invoke("birdMove", delayer, 6);
if (theBird.y < (theBack.height - theBird.height - 10)) {theBird.y += 12;}
}
break;
/* right */
case 39:
if (theBird.x < stage.stageWidth-theBird.width) {theBird.x +=5;}
if (theBack.x > -(theBack.width-stage.stageWidth-theBird.width)) {theBack.x -=15;}
break;
/* left */
case 37:
if (theBird.x > 0) {theBird.x -=5;}
if (theBack.x <= 0-theBird.width) {theBack.x +=15;}
break;
}
}
private function upNess(e:KeyboardEvent):void
{
wormCreator.delay = wormSpawnSpeed;
}
}
}
|