All posts by Aymeric

About Aymeric

Freelance Interactive & Game Developer.

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
 }

Testing as3isolib

Hi, folks !

I know my last post was taken a while, the sandwich course is really intensive. However I will try to post more article. I’ve made many experiments since two months that I would like to share : stuff on games engine and datastructure.

For this firts post of 2011 (already !), I will introduce the actionscript isometric library : as3isolib. There are two others actionscript isometric library : TheoWorlds and OpenSpace but they are not free !

We will create an isometric world and add random boxes, if you click on one it will be removed. The result.

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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package {
 
	import as3isolib.display.IsoView;
	import as3isolib.display.primitive.IsoBox;
	import as3isolib.display.scene.IsoGrid;
	import as3isolib.display.scene.IsoScene;
	import as3isolib.enum.RenderStyleType;
	import as3isolib.geom.IsoMath;
	import as3isolib.geom.Pt;
	import as3isolib.graphics.SolidColorFill;
 
	import eDpLib.events.ProxyEvent;
 
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
 
	public class Main extends Sprite {
 
		private const _CELL_SIZE:uint = 50;
 
		private var _grid:IsoGrid;
		private var _scene:IsoScene;
		private var _view:IsoView;
 
		private var _box:IsoBox;
		private var i:uint;
 
		private var _timerCreationBox:Timer;
 
		public function Main() {
 
			this.addEventListener(Event.ADDED_TO_STAGE, _init);
		}
 
		private function _init(evt:Event):void {
 
			this.removeEventListener(Event.ADDED_TO_STAGE, _init);
 
			_grid = new IsoGrid();
			_grid.setGridSize(10, 10, 1);
			_grid.showOrigin = false;
			_grid.cellSize = _CELL_SIZE;
 
			_scene = new IsoScene();
			_scene.hostContainer = this;
			_scene.addChild(_grid);
 
			_view = new IsoView();
			_view.setSize(800, 600);
			_view.centerOnPt(new Pt(200, 200, 0));
			_view.addScene(_scene);
			this.addChild(_view);
 
			_timerCreationBox = new Timer(300, 0);
			_timerCreationBox.addEventListener(TimerEvent.TIMER, _createNewBox);
			_timerCreationBox.start();
 
			this.addEventListener(Event.ENTER_FRAME, _ef);
		}
 
		private function _createNewBox(tEvt:TimerEvent):void {
 
			_box = new IsoBox();
			_box.styleType = RenderStyleType.SHADED;
			var couleur:uint = Math.random() * 0xFFFFFF;
			var alpha:Number = Math.random();
			_box.fills = [new SolidColorFill(couleur, alpha), new SolidColorFill(couleur, alpha), new SolidColorFill(couleur, alpha), new SolidColorFill(couleur, alpha), new SolidColorFill(couleur, alpha), new SolidColorFill(couleur, alpha)];
 
			_box.setSize(_CELL_SIZE, _CELL_SIZE, _CELL_SIZE);
			_box.moveTo(_CELL_SIZE * Math.floor(Math.random() * 10), _CELL_SIZE * Math.floor(Math.random() * 10), 0);
 
			_box.id = "box" + i;
			++i;
 
			_box.addEventListener(MouseEvent.CLICK, _deleteBox);
			_scene.addChild(_box);
		}
 
		private function _deleteBox(mEvt:ProxyEvent):void {
 
			mEvt.proxyTarget.removeEventListener(MouseEvent.CLICK, _deleteBox);
			_scene.removeChildByID(mEvt.target.id);
		}
 
		private function _ef(evt:Event):void {
			_scene.render();
		}
	}
 
}

The _scene.render() function is really important : it updates the scene of all your graphical change !

Later it will be a post on Box2d, stay tuned !

Resize an image and make a thumb !

Hey !
After two months without any article, I’m ready to start again activity on this blog ! For sure, it’s the start of the new school year !

So today, a little script really useful to make a thumb in ActionScript 3 !
EDIT : deleted my previous script, this one coming from Cult Creative is awesome :

/**
* fitImage
* @ARG_object   the display object to work with
* @ARG_width    width of the box to fit the image into
* @ARG_height   height of the box to fit the image into
* @ARG_center   should it offset to center the result in the box
* @ARG_fillBox  should it fill the box, may crop the image (true), or fit the whole image within the bounds (false)
**/
 
private function fitImageProportionally( ARG_object:DisplayObject, ARG_width:Number, ARG_height:Number, ARG_center:Boolean = true, ARG_fillBox:Boolean = true ):Bitmap {
 
    var tempW:Number = ARG_object.width;
    var tempH:Number = ARG_object.height;
 
    ARG_object.width = ARG_width;
    ARG_object.height = ARG_height;
 
    var scale:Number = (ARG_fillBox) ? Math.max(ARG_object.scaleX, ARG_object.scaleY) : Math.min(ARG_object.scaleX, ARG_object.scaleY);
 
    ARG_object.width = tempW;
    ARG_object.height = tempH;
 
    var scaleBmpd:BitmapData = new BitmapData(ARG_object.width * scale, ARG_object.height * scale);
    var scaledBitmap:Bitmap = new Bitmap(scaleBmpd, PixelSnapping.ALWAYS, true);
    var scaleMatrix:Matrix = new Matrix();
    scaleMatrix.scale(scale, scale);
    scaleBmpd.draw( ARG_object, scaleMatrix );
 
    if (scaledBitmap.width > ARG_width || scaledBitmap.height > ARG_height) {
 
        var cropMatrix:Matrix = new Matrix();
        var cropArea:Rectangle = new Rectangle(0, 0, ARG_width, ARG_height);
 
        var croppedBmpd:BitmapData = new BitmapData(ARG_width, ARG_height);
        var croppedBitmap:Bitmap = new Bitmap(croppedBmpd, PixelSnapping.ALWAYS, true);
 
        if (ARG_center) {
            var offsetX:Number = Math.abs((ARG_width -scaleBmpd.width) / 2);
            var offsetY:Number = Math.abs((ARG_height - scaleBmpd.height) / 2);
 
            cropMatrix.translate(-offsetX, -offsetY);
        }
 
        croppedBmpd.draw( scaledBitmap, cropMatrix, null, null, cropArea, true );
        return croppedBitmap;
 
    } else {
        return scaledBitmap;
    }
 
}

An other method coming from Thibault Imbert !

package {
 
	import flash.display.BitmapData;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
 
	/**
	 * @author Maxime Cousinou
	 */
	public class BitmapManager {
 
		public static function reduceBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {
 
			var bmpData:BitmapData = new BitmapData(Math.round(bmp.width * ratio), Math.round(bmp.height * ratio), transparent, 0x00FFFFFF);
			var scaleMatrix:Matrix = new Matrix(bmpData.width / bmp.width, 0, 0, bmpData.height / bmp.height, 0, 0);
			bmpData.draw(bmp, scaleMatrix);
 
			return bmpData;
		}
 
		public static function resizeBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {
 
			var bmpData:BitmapData = new BitmapData(Math.round(bmp.width * ratio), Math.round(bmp.height * ratio), transparent, 0x00FFFFFF);
			var scaleMatrix:Matrix = new Matrix(bmpData.width / bmp.width, 0, 0, bmpData.height / bmp.height, 0, 0);
			var colorTransform:ColorTransform = new ColorTransform();
			bmpData.draw(bmp, scaleMatrix, colorTransform, null, null, true);
 
			return bmpData;
		}
 
		public static function resampleBitmapData(bmp:BitmapData, ratio:Number, transparent:Boolean = true):BitmapData {
 
			if (ratio >= 1) {
				return BitmapManager.resizeBitmapData(bmp, ratio, transparent);
			} else {
				var bmpData:BitmapData = bmp.clone();
				var appliedRatio:Number = 1;
 
				do {
					if (ratio < 0.5 * appliedRatio) {
						bmpData = BitmapManager.resizeBitmapData(bmpData, 0.5, transparent);
						appliedRatio = 0.5 * appliedRatio;
					} else {
						bmpData = BitmapManager.resizeBitmapData(bmpData, ratio / appliedRatio, transparent);
						appliedRatio = ratio;
					}
				} while (appliedRatio != ratio);
 
				return bmpData;
			}
		}
	}
}

Tribute to the Metal’s kings

Yesterday my brother was come back from Romania. And for sure, we speak a lot about music ! Metal music, with many great leader which died recently.

So today it’s my first post on music for a tribute to the dead Metal’s kings !

Ronnie James Dio (ex-Black Sabbath, ex-Rainbow, Dio, Heaven & Hell) :

Peter Steele (ex-Carnivore, Type O Negative) :

Quorthon (Bathory) :

Valfar (Windir) :

And many others… R.I.P.

The Settlers of UTBM

This is my last project at the UTBM. It is based on the Settlers of Catan‘s game. I made this project with two schoolmates, we did it in Java with Swing’s library.

I retain of this project a hard utilization of the Swing’s library : it is really another way of conception than ActionScript 3, you manage all as you want, but that’s not easy to see out of the box.

This is the link of the game, you can find all packages and a .jar to run the game ! We tried to respect the MVC model ! I hope you enjoy !

I will add some news soon 😉

A new Hope

On this Geek Pride Day, I received the result of my examination for the famous Gobelins french school. I’m accepted 🙂
It’s really a good news for me (see references with the title in Star Wars 😉 ) !! I will stop my reading courses at the UTBM in june. The UTBM is an engineering school, but I am really upset about it !
So what is coming ? First of all, I will live in Annecy a beautiful city. After so many years in Belfort, it is welcome ! Secondly I go back in the Multimedia, C/C++ and Java is henceforth finish, I will be specialized in the Internet’s technologies. Thirdly I will be in an apprenticeship, one week at school, one week in an entreprise. That is great !
The training is for two years, so I will not be an engineer one day, but it does not matter.

A new hope, a new beginning, and a famous school !
Now I’m looking for a web agency, and an accomodation !

See ya

The final Countdown

Hey there ! This is a little script to made a simple countdown on 12/12/2012 at 12:12:12. You know, the end of the world 😉
The result.

And 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
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
 
	/**
	 * @author Aymeric
	 */
	public class Countdown extends Sprite {
 
		private var target:Date;
		private var now:Date;
		private var currentTime:Number;
		private var targetTime:Number;
		private var time:TextField;
		private var format:TextFormat; 
 
		public function Countdown() {
 
			target = new Date(2012, 11, 12, 12, 12, 12); // january = 0 -> december = 11
 
			time = new TextField();
			format = new TextFormat("Arial", 30);
			time.textColor = 0xFF0000;
			time.x = stage.stageWidth/2 - 100;
			time.y = stage.stageHeight/2 - 50;
			time.embedFonts = true;
 
			addChild(time);
 
			addEventListener(Event.ENTER_FRAME, refresh);
		}
 
		private function refresh(e:Event):void {
 
			now = new Date();
			currentTime = now.getTime();
			targetTime = target.getTime();
 
			var nbrSeconds:Number = (targetTime - currentTime)/1000;
			var seconds:Number = Math.floor(nbrSeconds%60);
			var minutes:Number = Math.floor(nbrSeconds/60%60);
			var hours:Number = Math.floor(nbrSeconds/3600%24);
			var days:Number = Math.floor(nbrSeconds/3600/24);
 
			var secondText:String;
			var minuteText:String;
			var hourText:String;
 
			if (seconds < 10) {
				secondText = "0" + seconds;
			} else {
				secondText = seconds.toString();
			}
 
			if (minutes < 10) {
				minuteText = "0" + minutes;
			} else {
				minuteText = minutes.toString();
			}
 
			if (hours < 10) {
				hourText = "0" + hours;
			} else {
				hourText = hours.toString();
			}
 
			time.text = days.toString() + " : " + hourText + " : " + minuteText + " : " + secondText;
			time.autoSize = TextFieldAutoSize.LEFT;
			time.setTextFormat(format);
		}
	}
}

I used an EnterFrame to refresh the countdown, but I have hesitated with a Timer which is called each seconds. I think the EnterFrame is better because the Timer depends of user’s computer. If you can highlight me, do not hesitate !