how to make your own events
First i have to warn you, this whole new event bureaucracy is SLOWER than if you just put the parameters you want to send in a object and make everyone have acess to it
If you are like me, or like Jack Welch,you will not fall in love for useless complication
However, just in case:
here is how you make your how events:
Events are things that happen
They are also objects with information about what happened
The most important thing on a event object is his type
When you add a listener to it, you do it using his type
When you go
Code:
stage.addEventListener("enterFrame", oef);
"enterFrame" is his type.
when you go
Code:
stage.addEventListener(Event.ENTER_FRAME , oef);
ENTER_FRAME is a constant of the object Event that will return "enterFrame".
oef is the function that will listen for that event.It will be fired whenever
the event is dispatched.In AS2 isteners could be objects, but in AS3 they are functions.
addEventListener is a method that sends the event object to the function listeners
Those who can use addEventlisteners are objects who extends IEEventDispatcher class like
stage, sprite and movieClip
The listener functions will receive the event object, so they have
parameter objects like
Code:
function oef(ev:Event){
}
Now, how to create an event object, you go like this:
First your event class should be in a .as file, it should import the event class and extend it
Code:
package{
import import flash.events.Event;
public class alarm extends Event{
}
}
Lets give him a message property to be acessed by the listeners, lets make him
receive it in his constructor
Code:
package{
import import flash.events.Event;
public class alarm extends Event{
var message:String;
//constructor function
public function alarm(message:String ){
this.message = message;
}
}
}
Now, inside the constructor you have to add another line,
super("alarm");
The goal of 'super' is to put parameters in the constructor function of
the class extenDED ; i mean,this function of the Event class itself (now in your
custom event class)has to know what is the type of your custom event.It is needed when you go
add listeners to it in the future, like this
des.addEventListener("alarm", listenerFunction);
Other thing you will have to do is override his clone() function.That function
is needed when you go dispatch this same event many times.All it does is to return
a new customEvent
Code:
package{
import import flash.events.Event;
public class alarm extends Event{
var message:String;
public function alarm(message:String ){
this.message = message;
super("alarm");
}
//override clone()
public override function clone():Event{
return new alarm(message);
}
}
}
Now lets go to the class that will broadcast it
It will have to be a class that extends the IEEventDispatcher class,(im not sure if this is the correct name neither this matter) like Sprite
or MovieClip
In this example the class will build a object alarm event based on a string that it receives when is
created, and will have a function dispa() that will dispatch this event to his listeners
The event to be dispatched is like a property of the class,created like this
Code:
public var al:alarm;
//inside the costructor function
al = new alarm(mess);
I will make dispa() function public to make others fire it, but the class itself could figure out
when is time to fire it or not
Once it creates his event object(al), to dispatch it
to the listeners is just
this.dispatchEvent(al);
So the dispa function will be
Code:
public function dispa(){
this.dispatchEvent(al);
}
And the class will be
Code:
public class diz extends Sprite{
public var al:alarm;
public function diz(mess){
al = new alarm(mess);
}
public function dispa(){
this.dispatchEvent(al);
}
}
To make your life easyer you can put all of them inside the same package and
save it diz.as
If you want to make them separated, make sure the diz class import the alarm class
import flash.display.Sprite;
import alarm;
All together would be:
Code:
package {
import flash.events.Event;
import flash.display.Sprite;
public class alarm extends Event{
var message:String;
//constructor
public function alarm(message:String ){
//the goal of super is put stuff in the cosntructor of the class extenDED
//"alarm" is the type of the event, need this when go addEventListener
//desInstance.addEventListener("alarm", listenerFunction);
super("alarm");
this.message = message;
}
//override clone()
public override function clone():Event{
return new alarm(message);
}
}
public class diz extends Sprite{
public var al:alarm;
public function diz(mess){
al = new alarm(mess);
}
public function dispa(){
this.dispatchEvent(al);
}
}
}
And how do you use those creatures?You use like this
Code:
var des:diz = new diz("bla");
des.addEventListener("alarm", tot);
des.addEventListener("alarm", tet);
function tot(ev:Event){
trace("the type of event is "+ev.type);
}
function tet(ent:Event){
trace("the message of the event is "+ent.message);
}
des.dispa();
des.dispa();
//the type of event is alarm
//the message of the event is bla
//the type of event is alarm
//the message of the event is bla
[CS3 only] rotating around given point
Note: FlashCS3 and Flash Player 9.0.28.0 required. Does not work in F9 Alpha (probably not in Flex either, but I havent tested).
Flash 9 motion package has predefined MatrixTransformer class which adds extra methods for transform matrix. One of them is rotateAroundExternalPoint method which allows to rotate the object around any point.
Lets say you have movie clip mc1 on stage, you can now rotate it around point at 100,150 using:
PHP Code:
//Since fl packages are specific to F9 it must be imported first.
import fl.motion.*;
//create new matrix
var m=mc1.transform.matrix;
//apply rotation
MatrixTransformer.rotateAroundExternalPoint (m,100,100,5);
//transfrom original mc
mc1.transform.matrix=m;
m is a matrix, 100 is x and 150 y coordinate of rotation point, 5 is number of degrees (not radians) to rotate. Notice that point is in the coordinate space outside of mc1. If you want to rotate around a point declared in the object, use rotateAroundInternalPoint method.
You may also find interesting other methods in MatrixTransformer class, for example matchInternalPointWithExternal ("Moves a matrix as necessary to align an internal point with an external point").
[CS3 only] built-in quadratic equation solver
Note: FlashCS3 and Flash Player 9.0.28.0 required. Does not work in F9 Alpha (probably not in Flex either, but I havent tested).
The BezierSegment class for FCS3 (found in fl.motion package) can solve quadratic equation (at^2 + bt + c) for you:
Finds the real solutions, if they exist by returning an array of number values, indicating the real roots of the equation. There may be no roots, 1 root or two roots. Imaginary or complex roots are ignored. Call it like this:
getQuadraticRoots (a,b,c);
PHP Code:
import fl.motion.*;
//solve equation 1*t*t+6*t+5
trace(BezierSegment.getQuadraticRoots (1,6,5));
Notice that result is not always exact, instead of full number (1) you may end up with something very slightly different (0.999999999999999). Something to do with rounding in the Flash player I guess.
BezierSegment class also has other interesting methods to play with:
*getCubicCoefficients
*getCubicRoots
*getSingleValue
*getValue
*getYForX
Various speed tests in AS3/CS3
Here you can see the speed tests of different things in AS3. Please wait for a while after pressing Test button, some may take several seconds to show results.
The Arrays and Objects, looping through elements:
http://www.hot.ee/tonypa/arraytest.swf
With double "for" loops Array[i][j] performs very well, better then flat array (A[i*w+j]) and much better then using object (O[i+"_"+j]).
"For..in" loop is much faster when you use Object, not recommended for Arrays.
"For each in" is lightning fast for both flat Array and Object. Of course you cant modify elements with this, you only get the values. But this would be very nice for example adding up all numbers in Array.
FCS3 fla: http://www.hot.ee/tonypa/arraytest.zip
Different types of hitTest:
http://www.hot.ee/tonypa/hittest.swf
AS3 has hitTestObject method to compare 2 display Objects and hitTestPoint method to compare point with Objects actual pixels or with its bounding box. Calculating distance between points with square root is still fastest way.
FCS3 fla: http://www.hot.ee/tonypa/hittest.zip
Different types of loops:
http://www.hot.ee/tonypa/looptest.swf
Comparing "for", "while" and "do..while" loops. They all behave same. If you really-really must have fastest loop then "do..while" is very slightly faster (10 ms after 100 million loops). "for" and "while" are so close in time that I think they are converted into same bytecode.
FCS3 fla: http://www.hot.ee/tonypa/looptest.zip
Different types of numbers in simple operations:
http://www.hot.ee/tonypa/typetest.swf
Here the speed of 4 operations is measured: add, substract, multiply and divide. And 4 types of numbers are used: Number, int, uint and undefined type.
int is best if you only need add and substract, dont use it for multiplication.
uint is even worse for anything beside addition.
Number is slower then int in add and substract, but faster to multiply and divide.
undefined type is very slow in every operation, you should never use it.
FCS3 fla: http://www.hot.ee/tonypa/typetest.zip
Converting external class file to F9 timeline code
Much of the code examples (even in F9 help files) uses external class files and that code can not be copy/pasted into F9 timeline directly. However, these are easy to convert.
Lets say you have class file like this:
PHP Code:
package {
import flash.display.Sprite;
import flash.events.Event;
public class ClassExample extends Sprite {
private var somevar:Number;
private var someothervar:String;
public function ClassExample() {
someothervar="Hello World";
somefunction();
}
private function somefunction():void {
somevar+=10;
}
}
You cant take the code and simply put it in F9 timeline, you will get lots of error when trying to publish it. First, you need to remove package block and all the private/public keywords:
PHP Code:
import flash.display.Sprite;
import flash.events.Event;
class ClassExample extends Sprite {
var somevar:Number;
var someothervar:String;
function ClassExample() {
someothervar="Hello World";
somefunction();
}
function somefunction():void {
somevar+=10;
}
}
Next you remove the class declaration while leaving the declared variables in timeline:
PHP Code:
import flash.display.Sprite;
import flash.events.Event;
var somevar:Number;
var someothervar:String;
function ClassExample() {
someothervar="Hello World";
somefunction();
}
function somefunction():void {
somevar+=10;
}
and for last you remove the class construction function and leave the code from this function in main timeline:
PHP Code:
import flash.display.Sprite;
import flash.events.Event;
var somevar:Number;
var someothervar:String;
someothervar="Hello World";
somefunction();
function somefunction():void {
somevar+=10;
}