Introduction

    Scene

    The Scene (represented by ) is a special object which is at the root of the scene tree. In hxd.App it is accessible by using the variable. You will need to add your objects to the scene before they can be displayed. The Scene also handles events such as clicks, touch, and keyboard keys.

    1. class Myapp extends hxd.App
    2. {
    3. override private function init():Void
    4. {
    5. super.init();
    6. s2d; // the current scene
    7. var myscene = new h2d.Scene(); // create a new scene
    8. setScene(myscene); // set it as the current scene
    9. var myobj2 = new h2d.Object();
    10. s2d.addChild(myobj2);
    11. var myobj = new h2d.Object(s2d);// add myobj to s2d by passing s2d as parameter
    12. }
    13. }

    An Image (represented by ) is an image resource loaded from the filesystem. It has methods to convert itself into a tile or texture (see below).

    1. var myimage = hxd.Res.img.myImage;
    2. // will load myImage.png/jpg/jpeg/gif from <your project folder>/res/img/
    3. var mytile = myimage.toTile();

    Tile

    Tile Pivot

    By default a tile pivot is to the upper left corner of the part of the texture it represents. The pivot can be moved by modifying the (dx,dy) values of the Tile. For instance by setting the pivot to (-tile.width,-tile.height), it will now be at the bottom right of the Tile. Changing the pivot affects the way bitmaps are displayed and the way local transformations (such as rotations) are performed.

    A Bitmap (represented by h2d.Bitmap) is a 2D object that allows you to display a unique Tile at the sprite position.

    1. var mybitmap = new h2d.Bitmap(myimagetile);
    2. s2d.addChild(mybitmap);
    1. bmp.tile.dx = -50;
    2. bmp.tile.dy = -50;

    Pixels

    Pixels (represented by hxd.Pixels) are a picture stored in local memory which you can modify and access its individual pixels. In Heaps, before being displayed, pixels needs to be turned into a Texture.

    A Texture (represented by ) whose per-pixel data is located in GPU memory. You can no longer access its pixels or modify it in an efficient way. But it can be used to display 3D models or 2D pictures.

    1. var mytex = myimage.toTexture();

    Example

    1. class Main extends hxd.App {
    2. var bmp : h2d.Bitmap;
    3. // allocate a Texture with red color and creates a 100x100 Tile from it
    4. // create a Bitmap object, which will display the tile
    5. // and will be added to our 2D scene (s2d)
    6. bmp = new h2d.Bitmap(tile, s2d);
    7. // modify the display position of the Bitmap sprite
    8. bmp.x = s2d.width * 0.5;
    9. bmp.y = s2d.height * 0.5;
    10. }
    11. // on each frame
    12. override function update(dt:Float) {
    13. // increment the display bitmap rotation by 0.1 radians
    14. bmp.rotation += 0.1;
    15. }
    16. static function main() {
    17. new Main();
    18. }