How to dispatch a custom event in Actionscript 3

This is going to be a very simple example of how to dispatch an event. In actionscript 3, you dispatch an event by extending the EventDispatcher class. I’m not going to get into a big complicated explanation right now, but I will show you a couple of simple examples.

How to create an AS3 game grid

One of the things that you have to do a lot when developing grid based games in Actionscript 3 is obviously lay out the grid. This is confusing to a lot of people but in flash, it is really quite simple. I created a simple class just for this blog to demonstrate how this can be achieved.

Load AS2 SWF into your AS3 Project

There is a Flash game that I have been working on that I’ve had to load into an as3 interface and I noticed that my previous class will not work to do this.

There were a few modifications that I had to make… Here is the Actionscript 3 class:

Quick and easy GetURL class / actionscript 3 urlrequest

Here is a quick basic flash url loader for quickly adding links to buttons and movie clips in Actionscript 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// GetURL Class by Rick Nuthman
// 10.4.09
// Usage:
// new GetURL("url", "target");

package com.frigidfish{
   
    import flash.net.*;
   
    public class GetURL{
       
        private var request:URLRequest;
       
        public function GetURL(url:String, target:String = '_self'){
           
            request = new URLRequest(url);         
            navigateToURL(request, target);
           
        }
    }
}

It’s just a bare bones script that works like the old getURL() method from actionscript 2.
It defaults to opening the url in a blank page, but you can change this by modifying the 2nd parameter.

Load SWF via Actionscript 3 with this class

Here is a class that you can use to load swf’s into an object that you can display or do whatever you’d like with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SWFLoader Class by Rick Nuthman
// 2.10.10
//
// Usage:
/*
var mySWF:Object;
mySWF = SWFLoader.create("urlToSWF.swf", 0,0);
mySWF.dispatcher.addEventListener("ready", displaySwf);
function displaySwf(event:Event){
    addChild(mySWF.swf);
}
*/


package com.frigidfish{

    import flash.display.Sprite;
    import flash.display.Loader;
    import flash.events.Event;
    import flash.net.URLRequest;
    import flash.events.EventDispatcher

    public class SWFLoader{        

        public function SWFLoader() {
        }

        public static function create(url, x=0,y=0):Object {
           
            var loaded:Event                = new Event("ready");
            var dispatcher:EventDispatcher  = new EventDispatcher();
            var swf:Sprite                  = new Sprite  ;
            var mLoader:Loader              = new Loader  ;
            var obj:Object                  = new Object;
           
            obj.swf = swf;
            obj.dispatcher = dispatcher;
           
            swf.x = x;
            swf.y = y;
           
            var mRequest:URLRequest = new URLRequest(url);
            mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfLoaded);

            mLoader.load(mRequest);

            function swfLoaded(event:Event) {
                var target = event.currentTarget.content;              
                swf.addChild(target);  
                dispatcher.dispatchEvent(loaded);
            }
           
            return obj;
        }
    }
}

You don’t need to make an instance of the class, just use the static function ‘create’ like so: