This is very handy feature when programming in AS3.
Imagine that you make a AlertBox class, that when called, it makes a custom Alert message. Imagine then, that you can put 1, 2 or 3 buttons, according to the message type.
If you want to reuse this class in several projects, it's a good idea to tell the different buttons what or where to return when it's pressed.
That's were the Function Call Back enters. After a couple of hours and some search in the web, I came up with a solution, which I want to share.
It's very simple.
First declare in your class a variable that will keep the function reference later on, like this:
private var callBack:Function;
Then when you instantiate the Class, one of the arguments, of the constructor or function, will keep the function name, like this:
createAlert(..., callback){
...
}
Finally inside the constructor or function, you have to attribute the argument to the variable you defined earlier, like this:
callBack=callback;
Then place this variable as a call of some event, like this:
button1.addEventListener(MouseEvent.CLICK, callBack);
... and that's it.
If you want to call the call back function with arguments, instead of a event, just call this way:
typeOfMessage=1;
callback(typeOfMessage);
Personal blog were I will post as3 snippets that will help all AS3 programmers to achieve small tasks, not always simple or straightforward.
Friday, 13 August 2010
Sunday, 30 May 2010
Cloning arrays
On the other day I had the need to copy an Array. It was not the first time, but this time I had to make a temporary copy of another array, and put some new data in it and make some calculations.
In this process my need to change the original Array, was none.
The problem is that if you make something like this:
var myArray:Array=new Array("Blue", "Yellow");
var myCopyArray:Array=new Array();
myCopyArray=myArray;
... it works to some extent. You can also do this with concat or slice commands.
This method makes a shallow copy, which means that the copy only stores the reference to the original values, that is, it only stores the memory reference where the values are stored. This means that if you change one of the arrays, the other will change also... This was not what I meant.
I needed to make a hard copy of the array, change it, without changing the original. During the research, I found this generic solution presented in a webpage by Adobe at the following link:
https://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000092.html
As always I transcript the code to here and make a brief explanation.
So the function looks like this:
In this process my need to change the original Array, was none.
The problem is that if you make something like this:
var myArray:Array=new Array("Blue", "Yellow");
var myCopyArray:Array=new Array();
myCopyArray=myArray;
... it works to some extent. You can also do this with concat or slice commands.
This method makes a shallow copy, which means that the copy only stores the reference to the original values, that is, it only stores the memory reference where the values are stored. This means that if you change one of the arrays, the other will change also... This was not what I meant.
I needed to make a hard copy of the array, change it, without changing the original. During the research, I found this generic solution presented in a webpage by Adobe at the following link:
https://www.adobe.com/livedocs/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000092.html
As always I transcript the code to here and make a brief explanation.
So the function looks like this:
import flash.utils.ByteArray; function clone(source:Object):* { var myBA:ByteArray = new ByteArray(); myBA.writeObject(source); myBA.position = 0; return(myBA.readObject()); }
Basically what this does is it goes to the memory and gets what is stored in the place where the original Array is referenced. To get the original data and not the reference, it uses the ByteArray class and stores the data in the myBA:ByteArray object. The ByteArray class insures that it copies whatever is stored in the original Array, i.e, Numbers, String, DisplayObjects, whatever...
Pretty handy...
Etiquetas:
as3 flash Array
Friday, 21 May 2010
How to erase a element in a Array
I came across this problem recently.
The Array has some methods, and the most common are PUSH, that lets you put an element in a Array. This element will go to the end.
And then you have the POP method, but this only gets the last element in a Array.
Now the question is "How do I delete an element giving the position for that element"?
It's a bit trickier, and it's done using another method called SPLICE.
The syntax is:
splice(startIndex:int, deleteCount:uint, ... values)
This means:
- startIndex -> Initial position were you want to delete. It's the index position of the Array;
- deleteCount ->This is how many elements you want to delete, that includes the index position you specified in startIndex. If you say 1, then it will delete the index position that you've specified. If you say 2, then it's the index position, and the next element... and so on.
As an example:
var myList:Array=new Array("Blue", "Red", "Green", "Black,"White");
myList.splice(2,2);
trace(myList); //Blue, Red, White
//Green is Index number 2, and then deletes two elements including Green, which are Green and Black
The Array has some methods, and the most common are PUSH, that lets you put an element in a Array. This element will go to the end.
And then you have the POP method, but this only gets the last element in a Array.
Now the question is "How do I delete an element giving the position for that element"?
It's a bit trickier, and it's done using another method called SPLICE.
The syntax is:
splice(startIndex:int, deleteCount:uint, ... values)
This means:
- startIndex -> Initial position were you want to delete. It's the index position of the Array;
- deleteCount ->This is how many elements you want to delete, that includes the index position you specified in startIndex. If you say 1, then it will delete the index position that you've specified. If you say 2, then it's the index position, and the next element... and so on.
As an example:
var myList:Array=new Array("Blue", "Red", "Green", "Black,"White");
myList.splice(2,2);
trace(myList); //Blue, Red, White
//Green is Index number 2, and then deletes two elements including Green, which are Green and Black
Etiquetas:
as3 Array erase
Tuesday, 4 May 2010
Instantiate an SWF as a Class
At the company where I work we add the need to make several SWF that needed to come up, in different timings and according to user navigation. But since we wanted to keep the final application as light as possible, we had the need not to instantiate all the external swf at once.
After some research we found and implemented the following solution: instantiate external swf's as separate classes:
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Loader;
public class Load extends MovieClip
{
var storeLoadedObject:Class; //Self explained ;)
var loader:Loader;
public function Load():void{
loader=new Loader();
loader.load(new URLRequest("example.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeObjForEdit);
}
public function completeObjForEdit(evt:Event):void{
var className:String="com.gameRoot"; //Document class of loaded file
storeLoadedObject=loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
makeLotsOfCopies();
}
public function makeLotsOfCopies():void{
for(var i:uint=0; i<20;i++){
var copy:MovieClip=new storeLoadedObject();
addChild(copy);
copy.x=i*5;
copy.y=i*5;
}
}
}
}
And that's it.
After some research we found and implemented the following solution: instantiate external swf's as separate classes:
package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Loader;
public class Load extends MovieClip
{
var storeLoadedObject:Class; //Self explained ;)
var loader:Loader;
public function Load():void{
loader=new Loader();
loader.load(new URLRequest("example.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeObjForEdit);
}
public function completeObjForEdit(evt:Event):void{
var className:String="com.gameRoot"; //Document class of loaded file
storeLoadedObject=loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
makeLotsOfCopies();
}
public function makeLotsOfCopies():void{
for(var i:uint=0; i<20;i++){
var copy:MovieClip=new storeLoadedObject();
addChild(copy);
copy.x=i*5;
copy.y=i*5;
}
}
}
}
And that's it.
Etiquetas:
instantiation swf external as3
Sunday, 25 April 2010
Video smoothing in AS3
This is an issue I found that has made serious difficulties to developers about video playback. If you have a video file in flv format, and it has a size that is smaller than the stage where you want to show it in, Flash pixelizes the video. This is the default behaviour of Flash when playing video.
There's a workaround but it's a direct one, as it was in AS2.
In AS2 you would do like this:
myVideoObject
.smoothing = true | false
In AS3 you have to get to the "smoothing" in other way. Like this:
videoLoader.getVideoPlayer(videoLoader.activeVideoPlayerIndex).smoothing = true;
Where videoLoader is the FLVPlayback component that you insert with ActionScript.
Etiquetas:
video as3
Monday, 12 April 2010
How to load an external SWF
This is how you can load an external SWF, using the progress event to make some sort of preloader.
import flash.net.URLRequest;
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad() {
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(“MouseActions.swf”);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent) {
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
import flash.display.Loader;
import flash.events.Event;
import flash.events.ProgressEvent;
function startLoad() {
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest(“MouseActions.swf”);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,onProgressHandler);
mLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event) {
addChild(loadEvent.currentTarget.content);
}
function onProgressHandler(mProgress:ProgressEvent) {
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
startLoad();
Tuesday, 19 January 2010
XML loading class
This article is about loading xml data, on files or webservices. Sometimes, some projects require that you must read lots of information that are scattered throught many xml files or webservice. This class will help you not to recode every time you must read such type of information. Instead you only have to read the data acording to the response you receive.
So if you use this class, you just need to instatiate an object and call the file/webservice. Instantiate a new object for each file/webservice.
This code was copied from the website: http://www.mediareason.com/blog/?p=20
So if you use this class, you just need to instatiate an object and call the file/webservice. Instantiate a new object for each file/webservice.
This code was copied from the website: http://www.mediareason.com/blog/?p=20
package com.func { import flash.net.URLLoader import flash.net.URLRequest import flash.xml.XMLDocument; import flash.errors.* import flash.events.* public class LoadXML { private var loader : URLLoader; private var mainXML : XML; public function LoadXML() { loader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onComplete); loader.load(new URLRequest("http://.../test.xml")); } private function onComplete(evt:Event) { try { mainXML = new XML(loader.data) trace(mainXML); } catch(e:Error) { trace("Error: " + e.message) return; } } } }
Subscribe to:
Posts (Atom)