Flash Actionscript

2019-08-27 04:08发布

问题:

It is my first time to code an actionscript for flash. I want to write a flash clip which works as a parent of another flash clip. I want to write a function in the parent flash, and call that function in the child flash clip. For example I wanna create an actionscript which sends game score to "submitscore.php". The parent is just a controller and the child is my game. I want to send game score to the controller, then send it to my php file. Do you have any sample code or something to do that? I really don't know what I want is hard or easy because it's my first time ;) thanx in advance

回答1:

var game:Object;
private function sendToPHP(e:CustomEvent):void
{
    var score:Number = e.score;
    //send it
}
//load the game.swf
var ldr:Loader = new Loader();
addChild(ldr);
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest("Game.swf"));

private function onLoad(e:Event):void
{
    game = LoaderInfo(e.target).content;
    game.addEventListener("sendScore", sendToPHP);
}

//Game.as
//call this whenever you want to send score to php
dispatchEvent(new CustomEvent("sendScore", score));

/**
* CustomEvent.as should extend Event and its constructor should update the public
* property score:Number and call super() with the first parameter. 
* Feel free to ask if you have any doubts implementing custom events.
* */