Archive

Posts Tagged ‘AS3’

AS3 framework option #3

January 24th, 2012 5 comments

After analyzing 2 most popular framework both from syntax and performance point of view – I realized that both frameworks is not good enough for me. I work mainly with games and heavy applications that requires both code writing speed and code execution speed. With that knowledge I did what any programmer usually does in such situation – started my own project! where everything will be perfect! it will be fast! it will be comfortable to work with! and sun will shine all day long!

So I started with writing requirements…

Read more…

RobotLegs VS PureMVC: performance battle!

January 6th, 2012 2 comments

We all know that using framework in you application will hurt performance, and Flash has long and sad history of performance problems. I decided to find out how much exactly performance using framework costs us. As in last post I focus on 2 most popular frameworks: RobotLegs and PureMVC.


Run PureMVC tests           [Source code: PureMVCSpeedTest.zip]

I tried to measure 4 things: running commands, getting stuff, creating mediators, communication.
I measured how much time one such action will take, and calculated how much actions you can use in your code to waste 1ms of execution time.

PureMVC VS RobetLegs match-up!

December 14th, 2011 1 comment

Then you start creating bigger applications, or start working in bigger teams – using good framework becomes essential.
I spent good amount of time with 2 currently most popular AS3 frameworks – PureMVC and RobotLegs, and here I will compare pros and cons of both.



Generally both frameworks are very similar! :


RobotLegs and PureMVC Pros:

  • will help divide your code is small units and wire them;
  • will help standardize your code;
  • based on MVC;
  • can be extended to your needs;
  • makes you focus on your fun app instead of focusing on solving architecture puzzles;
  • have big and active communities;



RobotLegs and PureMVC Cons:

  • hurts performance a bit;
  • harder to debug “black box” framework code;
  • couples implementation with framework;



…but RobotLegs: Read more…

AS3 KeyboardEvent stage focus problem solution

August 3rd, 2011 No comments

So I needed global KeyboardEvent handling in AS3 project, but ran into problem – stage losses focus in couple of scenarios..

The fact that flash has no global object that never loses focus just SUCK!!!

I found in documentation that in Flex you can put your event on application object and solve this problem, but because I hate Flex – I can’t be bothered.

So possible solution I found all over the internet is force focus back to stage:

stage.focus = stage;

But this method creates another problem if you actually need focus on something not stage.(like text input fields).

My approach makes KeyboardEvent listeners travel with focus, from object to object that gets it.

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
56
57
58
59
package {
import flash.display.InteractiveObject;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.FocusEvent;
import flash.events.KeyboardEvent;
 
public class KeyBoardFocusMain extends Sprite {
 
	private var focusObject:InteractiveObject;
 
	public function Main():void {
		setFocusObject(this.stage);
	}
 
	private function setFocusObject(newFocusObject:InteractiveObject):void {
		if (focusObject){
			focusObject.removeEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
			focusObject.removeEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
			focusObject.removeEventListener(FocusEvent.FOCUS_OUT, handleFocusChange);
		}
		focusObject = newFocusObject;
		//
		focusObject.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
		focusObject.addEventListener(KeyboardEvent.KEY_UP, handleKeyUp);
		focusObject.addEventListener(FocusEvent.FOCUS_OUT, handleFocusChange);
	}
 
	private function handleFocusChange(event:Event = null):void {
		if (focusObject != stage.focus){
			if (stage.focus != null){
				setFocusObject(stage.focus);
			} else {
				setFocusObject(stage);
			}
 
		}
	}
 
	private function handleKeyUp(event:KeyboardEvent):void {
		trace("handleKeyUp:", event);
		switch (event.keyCode){
			//...
			default: 
				break;
		}
	}
 
	private function handleKeyDown(event:KeyboardEvent):void {
		trace("handleKeyDown:", event);
		switch (event.keyCode){
			//...
			default: 
				break;
		}
	}
 
}
}

Hope it helps, have fun.

AssetLibrary : basics

July 28th, 2011 No comments

I want to post series of articles about tool I am building during free time.

All started then I got this insane idea of building GUI library, for creating GUI in applications more fluently and conversant then FLEX. (there is so much things I hate about FLEX…)
I needed a asset library to handle my assets easily. I starter with something simple, but ended up having a lot of features I initially didn’t planed.

Read all features here : http://code.google.com/p/msa-lib/

I will write couple of posts about this library, and transform them to documentation.

If it’s something that interests you, grab code in http://msa-lib.googlecode.com/svn/trunk/ and lets get started.


Then you build application, you almost always have some sort of assets: texts, images, sounds, binary files for 3d and so on.

 

I am dividing those assets in 3 category:

  • Internaly embedded assets.
  • External permanent asset. (it’s loaded once, usually in the beginning, and then never removed from memory. )
  • External temporal asset. (it’s loaded then needed, and unloaded then not needed any-more.)

Then you deal with internally embedded assets, there is not much to do with them, you just use it then you need it. With external assets its a bit trickier, that’s there AssetLibrary comes in.

 

Lets just start with most simple example possible:

 

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
package examples {
import com.mindscriptact.assetLibrary.AssetLibrary;
import com.mindscriptact.assetLibrary.AssetLibraryIndex;
import flash.display.Sprite;
 
public class MinimalLoad extends Sprite {
 
	public function MinimalLoad(){
 
		// add assets to library.
		var assetIndex:AssetLibraryIndex = AssetLibrary.getIndex();
		assetIndex.addFileDefinition("test1", "assets/simpleTest/test1.swf");
 
		// load asset and send it to function.
		AssetLibrary.sendAssetToFunction("test1", handleTest1);
	}
 
	private function handleTest1(asset:SWFAsset):void {
		// get instance of object linked in asset library.
		var testSprite:Sprite = asset.getSprite("SquareA_SPR");
		this.addChild(testSprite);
		testSprite.x = 100;
		testSprite.y = 100;
	}
 
}
}

 
To use external assets you need to define them first. AssetLibraryIndex class is created for that.
To get it’s instance you need to call AssetLibrary.getIndex() function. In most cases you will be defining you assets once, and then application starts. After assets are defined you most likely will never use this class again. AssetLibraryIndex class lets you define folders, groups, add file definitions from xml file, and add single file definitions, lets start with those.

Every file definition in AssetLibrary needs an Id. This id is used to work with asset instead of all it’s de-tales like url, asset type, how it’s loaded and such. Most simple asset has id and url. To add asset we use assetIndex.addFileDefinition(); function.

Now then asset is defined we can start using it. We tell AssetLibrary to load it and send asset as parameter to function. We use AssetLibrary.sendAssetToFunction() for that. This function expects assedId and function to send asset to.

Then we write asset handling function we must remember, that same as event handling function gets an event object as parameter, asset handler gets asset as parameter. Because we are using swf asset, we will get SWFAsset class object. This object can be used to get any instance of symbol in assets library. We need to know the type of linked object and its ActionScript linkage class.


Next time I explain how permanent and temporal assets differ, and how to track loading progress with this tool.

MindScriptAct is Stephen Fry proof thanks to caching by WP Super Cache