Tag Archives: actionscript 3

Box2D with the WorldConstructionKit

Before the weekend, I suggest to dig into the Box2D API for Flash with the great WorldConstructionKit.
Box2D is a physical engine wrote in C++ and translated into many languages such as Java, Objective-C, AS3, JavaScript…

For ActionScript3, there are 2 ports : Boris the brave’s port, and the WCK. You can find a performance comparison here made by Allan Bishop.
The WCK is the best thanks to the Alchemy port : it translates the C++ into something which can be understable by Flash. More information here.

The WCK provides a “a toolset / framework for rapidly developing physics based games / websites within the Flash IDE. WCK allows you to layout your 2d worlds / game levels entirely within Flash and have them hook into the physics simulation without writing any code.”

Continue reading Box2D with the WorldConstructionKit

Dynamic variable to parse in a XML

A current problem in AS3 with XML : if you want to parse a XML with a dynamic variable as a root, you can’t with a for each statement. It doesn’t recognize the variable.

The XML Format :

<?xml version="1.0" encoding="UTF-8"?>
 
<boite>
 
	<Box name="Box1">
		<lien nom="BENCHMARK">http://www.google.fr</lien>
		<lien nom="FOCUS GROUP">http://www.google.fr</lien>
	</Box>
 
	<Box name="Box2">
		<lien nom="FORMATION">http://www.google.fr</lien>
		<lien nom="COACHING">http://www.google.fr</lien>
	</Box>
 
</boite>

The way which doesn’t work (N.B. in this case the XML is Box1…/Box1 Box2…/Box2:

var myBox:String = "Box1";
for each (var box:XML in myXML.myBox) {
	trace(box);
}

The good way :

private function _xmlLoaded(mEvt:Event):void {
 
	mEvt.target.removeEventListener(Event.COMPLETE, _xmlLoaded);
	_xml = new XML(mEvt.target.data);
 
        _boiteNom = "Box1";
 
	_textes = [];
	_liens = [];
 
	var indexBoite:uint = _getIndexBox(_boiteNom);
	_nbrFiches = _xml.Box[indexBoite].lien.length();
 
	for (var i:uint; i < _nbrFiches; ++i) {
		_textes.push(_xml.Box[indexBoite].lien[i].@nom);
		_liens.push(_xml.Box[indexBoite].lien[i]);
	}
 
	_ajoutFiche();
}
 
private function _getIndexBox($name:String):uint {
 
  	for(var i:uint; i < _xml.Box.length(); ++i) {
   		if(String(_xml.Box[i].@name) == $name)
   			return i; 
   	}
   return 666; // The devil's number, too evil to be real
 }

Handle the Right Click in AS3

It can not be denied that handling the Right Click in Flash is a problem.
At first, it was made to display a Contextual Menu with Flash Player’s options, this is great for a website. However Flash is also used to make browser’s games and it is reductive if we can’t use this. For example in a Point & Click game, like the mythical Diablo, the Right Click is very important and simply to use. If you want to develop that kind of game with Flash (one of my dream ;-)) it will be difficult to manage this. In this direction, old Apple’s mouses with their only click were preposterous !! I think that was a commercial argument like “our Operating Systems is so ergonomic that you don’t need an alternative button”. But if you used software like Photoshop, you will always push the ctrl button on the keyboard, but also shift for other action or alt, cmd…

So today I try to look over that. And what I notice is :
– the right click and the middle click are functional in AS3 (those MouseEvent exist) but work only for Air !!
– you can easily change the context menu options (good example) and add functions but About and Settings can not be removed from the menu !!
– use JavaScript to handle the Right Click thanks to this class. Here we go :

I created a .swf in which with a right click you create a circle, and with a left click on a circle delete it (the application). Note it seems it is no more updated. But what happens ? It doesn’t work with Safari and Opera… failed. And you should have notice that I created a circle by pressing the right button, logically it should be the left… ?? Because of the JavaScript, you can’t give an occurence through it to Flash, so how can I obtain the target ?
This is the code :

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
var circle:Circle;
 
stage.addEventListener(MouseEvent.MOUSE_DOWN, deleteCircle);
 
var i:int = 0;
 
var methodName:String = "rightClick";
var method:Function = rightClick;
ExternalInterface.addCallback(methodName, method);
 
function rightClick():void {
 
	circle = new Circle();
	i++;
	circle.name = String(i);
	addChild(circle);
	circle.x = mouseX;
	circle.y = mouseY;
}
 
function deleteCircle(m:MouseEvent):void {
 
	if (m.target.name != null) {
		removeChild(getChildByName(m.target.name));
	}
}

So to conclude, at the moment you can’t use cleanly the right click in Flash like the left. You will always have the contextual menu with About and Settings’ buttons. But in fact who use them ? Aside on a website using Webcam or Microphone. So I think that developers should have the choice if they want a contextual menu (by default if we don’t need the right click) or not to have a more ergonomical application.