Some parts of this article are now obsolete, as Cocktail has changed a lot (2012-08-14).
Cocktail, what is cocktail?!
Cocktail is the name of a new project @ Silex Labs.
This is an article for developers about the haXe library Cocktail, cross platform haXe!
Please use up-to-date Firefox 6, Chrome or Safari, as there are still issues with other browsers.

According to the website in The Labs, Cocktail is a cross-platform library for the haXe programming language. It bridges the gap between all the targets supported by haXe, removing inconsistencies behind a common API. With it, the same code base is used to deploy to these targets: HTML5 + javaScript, Flash, PHP.
It helps multi device application development and should be used by haXe projects requiring cross-target compilation.
And of course, it is released under GPL.
In this alpha1 release, DOM manipulation basis are present, with assets loading, external libraries and native code includes, as well as drawing API. The text can be used, but the text style is not handled yet, so it may be different in js and Flash, as you can see it bellow.
Javascript and Flash version of the clock, try to guess which one is whichIn this article, we will see some demo of cross platform code, and focus on the Cocktail part of the sample code. The first example, the “clock” is very simple, we load png images and manipulate them to display the time it is. The “ski game” sample is a bit more complex, and uses a classic technique in games, where the game is drawn on a screen at each frame – the scree is a canvas in HTML, and a Sprite in Flash, but this is transparent with Cocktail.
press the space bar to play guess what, this is not Flash !Feel free to check the get started with Cocktail article on the haXe wiki for more technical information and getting started with haXe article for more details about haXe.
Links
- Project home
- Cocktail road map and development state
- Cocktail source code
- Forums
- Cocktail wiki on haXe.org
Clock demo
Here is a very basic demo, which displays an analog clock. The code is very simple and uses Cocktail library to load the background image and the clock hand image, and display it rotated, with its center as a register point.
Flash version
Requires Flash Player 10.2.
javascript version
Requires HTML5.
The code
You can take look at the code on github, and you will see that there is NO conditional compilation at all, no #if js nor #if flash…
Here we will show the parts which use the Cocktail API.
1st contact: the imports show some of the classes exposed by Cocktail
import cocktail.resource.ResourceLoaderManager; import cocktail.nativeElement.NativeElementManager; import cocktail.domElement.ImageDOMElement; import cocktail.domElement.TextDOMElement;
These classes will let you manipulate the javascript or Flash DOM seamlessly, load assets and get keyboard events. As you will see it below…
The stage for the game
The “stage” or the root graphical object of our game, will be the root clip in Flash and the body in HTML. Here is how it is declared:
/** * The root of the dom (stage in flash, body in html) */ private static var rootDOMElement:DOMElement;
And initialized:
rootDOMElement = new DOMElement(NativeElementManager.getRoot());
You can notice that the root clip is returned by NativeElementManager::getRoot. Its type is NativeElement, and it is used to create a DOMElement, which is the base type for all graphical objects in Cocktail. So, this instruction, creates a DOMElement which will be used as the root container of our application, it will represent the root Sprite in the Flash version, and the body element in HTML.
The graphical objects
Here are the variables used to store the graphical objects
/** * the background of the analogue clock */ private var _clockBackground:ImageDOMElement; /** * the foreground of the analogue clock */ private var _clockForeGround:ImageDOMElement; /** * the second needle of the clock */ private var _secondNeedle:ImageDOMElement; /** * the minute needle of the clock */ private var _minuteNeedle:ImageDOMElement; /** * the hour needle of the clock */ private var _hourNeedle:ImageDOMElement; /** * the numeric clock */ private var _clockDisplay:TextDOMElement;
Manipulation of the DOM
Here is the code where the graphical assets are loaded…
ResourceLoaderManager.loadImage("assets/second_needle.png", onSecondNeedleLoaded, function(err){});
ResourceLoaderManager.loadImage("assets/hour_needle.png", onHourNeedleLoaded, function(err){});
ResourceLoaderManager.loadImage("assets/minute_needle.png", onMinuteNeedleLoaded, function(err){});
ResourceLoaderManager.loadImage("assets/clock_background.png", onClockBackgroundLoaded, function(err){});
ResourceLoaderManager.loadImage("assets/clock_foreground.png", onClockForegroundLoaded, function(err){});
… and then attached to the DOM
rootDOMElement.addChild(_clockBackground); rootDOMElement.addChild(_secondNeedle); rootDOMElement.addChild(_minuteNeedle); rootDOMElement.addChild(_hourNeedle); rootDOMElement.addChild(_clockForeGround);
The rotation of the images is set in order to reflect the time it is
_secondNeedle.rotation = Math.round(Date.now().getSeconds() * 6); _minuteNeedle.rotation = Math.round(Date.now().getMinutes() * 6); _hourNeedle.rotation = Math.round(Date.now().getHours() * (360/12));
You can notice how simple it is to rotate a DOMElement with setRotation, passing the angle as the 1st argument and the rotation center as a 2d argument.
About the text
The text in cocktail is also handled as a DOMElement. But in this alpha1 release, the text style is not handled yet, so it may not appear the same in js and Flash.
_clockDisplay = new TextDOMElement(); rootDOMElement.addChild(_clockDisplay); ... _clockDisplay.text = Date.now().toString();
Ski demo
Here is the classic little ski game of GM2D, which has modified to use Cocktail and compile in javascript and Flash with exactly the same code.
This example is interesting since it shows how to draw in a DOMElement, and how to use the keyboard events.
Flash version
Requires Flash Player 10.2. Exactly the same as the original version…
javascript version
Requires HTML5. Looks the same ![]()
The code
You can take look at the code on github, and you will see that there is NO conditional compilation at all, no #if js nor #if flash…
Here we will show the parts which use the Cocktail API.
The main classes
import cocktail.resource.ResourceLoaderManager; import cocktail.nativeElement.NativeElementManager; import cocktail.domElement.ImageDOMElement; import cocktail.domElement.GraphicDOMElement; import cocktail.domElement.TextDOMElement; import cocktail.domElement.DOMElement; import cocktail.keyboard.KeyboardData;
These classes will let you manipulate the javascript or Flash DOM seamlessly, load assets and get keyboard events. As you will see it below…
The graphical Elements
Here are the variables used to store the graphical objects.
var mArena:GraphicDOMElement; var mBits:ImageDOMElement;
Note the GraphicDOMElement, this is the one to which we will draw. And we will use the ImageDOMElement as a source, since it will contain this png image:
And here is the code where the GraphicDOMElement is initialized, before we can draw into it:
//the abstract gaphic dom object is instantiated and //its dimensions are set. It will be used as a cross-platform //drawing surface mArena = new GraphicDOMElement(); mArena.width = 640; mArena.height = 480; //it is attached to the root dom object, the higher object //in the DOM tree rootDOMElement.addChild(mArena);
And here is the “magic”, how to draw in a DOMElement, which means draw in a canvas or in a Sprite depending on the target:
//use the drawImage method of the GraphicDOMElement //to copy pixels onto a surface mArena.drawImage(mBits, mPoint, mRect);
Credits
Special Thanks
- GM2D for the classic little ski game tutorial
- Yannick for the code
- Pol for the design
- lexa for the rest






lexa
Leave here your best scores at the ski game ok ?
Ariel Sommeria-Klein
2526!
super travail! je tâcherai de regarder ça de plus près dés que je peux
lexa
Thanks ariel !