views:

71

answers:

1

Hi all, 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  A: 
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.
* */
Amarghosh
thanx for your reply. But is it possible to write with more details because I wrote this code in my action script and it had 3 errors about functions not being in class. I really am newbie. thanx in advance.
Ali Bozorgkhan
You need to declare a document class and write the functions in it. What exactly are the error messages? The code is AS3 by the way.
Amarghosh
Is it possible to write it with AS1 !?
Ali Bozorgkhan
it should be possible, but I don't know how to do that. I guess MovieClipLoader is a good keyword to start searching for
Amarghosh
thank you so much ;)
Ali Bozorgkhan