Call: (209) 560-6611

as3 custom event, dispatch event, load swf

Date: July 1, 2014 Author: ang Category: Tutorials Tags: , Comments: 0

Here is a tutorial that shows you how to create and use a custom event using the dispatchEvent() method.

Here is a class for a start screen '.fla' file:

 Javascript |  copy code |? 
01
package {
02
 
03
import flash.display.MovieClip;
04
import flash.events.Event;
05
import flash.events.MouseEvent;
06
 
07
public class mcStartGameScreen extends MovieClip {
08
 
09
//create vars
10
public var mcStart:MovieClip;
11
 
12
public function mcStartGameScreen() {
13
// constructor code
14
mcStart.buttonMode = true;
15
mcStart.addEventListener(MouseEvent.CLICK, startClick);
16
}
17
 
18
private function startClick(e:MouseEvent):void
19
{
20
dispatchEvent(new Event("START_GAME"));
21
}
22
 
23
}
24
}

Here is another class that loads the '.swf' file and listens/reacts to the custom event:

 Javascript |  copy code |? 
01
02
package  {
03
 
04
	import flash.display.MovieClip;
05
	import flash.events.Event;
06
	import flash.events.KeyboardEvent;	
07
	import flash.display.Loader;
08
	import flash.net.URLRequest;
09
 
10
public class eaterMain extends MovieClip {
11
 
12
//create vars
13
public var menuStart:mcStartGameScreen;//start game screen swf
14
 
15
public function eaterMain() {
16
// constructor code
17
//set focus of keyboard output
18
stage.focus = stage;
19
//create a loader object
20
var startLoader:Loader = new Loader();
21
//add event listener to listen for the complete event
22
startLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, startLoaded);
23
//load out loader object
24
startLoader.load(new URLRequest("startScreen.swf"));
25
}
26
 
27
public function startTheGame(e:Event):void
28
{
29
trace("Game has started!");
30
}
31
 
32
private function startLoaded(e:Event):void
33
{
34
//get a reference to the loaded movieclip
35
menuStart = e.target.content as mcStartGameScreen;
36
//add loaded movieclip (start scree) to the stage
37
addChildAt(menuStart, 1);
38
//listen for start game event
39
menuStart.addEventListener("START_GAME", startTheGame);
40
}
41
 
42
}
43
}
44

Leave a Reply

Your email address will not be published. Required fields are marked *

Before you post, please prove you are sentient.

What is that thing with fingers at the end of your arm (one word)?

Before you post, please prove you are sentient.

What color is a typical spring leaf?