Win an Ipad
  Developer   Documents   Sprite

Sprite

Sprite class creates game characters and manages their animation and movement. Sprite is an instance of gameObject generic class. Sprite creates default animation from frames in Sprite’s image. Sprite requires an instance of the Bitmap, frame width and frame height. These parameters are important to split the image into frames (according to set frame width and height) and then use them in the animation.

However, developer can change animation by using sequence property. This property takes array of frames indexes that should be used in application (f.e.: this._sprite.sequence = new Array(0, 2); used only in the first and third frame of animation).

Let’s have an example of snakehead sprite. We need to have an image with four frames. Each frame defines direction of the snake (Top, Right, Bottom, Left).

Sprite defines its own Sprite.intersectsBounds method that compares coordinates of the object passed as parameter with current Sprite coordinates. If coordinates intersects it returns true, otherwise false.

To compare absolute X-coordinate and Y-coordinate with current Sprite coordinates, we use method Sprite.intersectsPoint.


include "lib://game2d/sprite.ms"; class Head : Sprite
{
    //load images to static variables     function init()
    {
        super.init();
        //init direction
        this._direction = #right;
        //load image from file
        this.image = "app://gfx/head.png";
        this.frameWidth = 43;
        this.frameHeight = 46;     }         property direction(v)
    {
        get return this._direction;
        set {
            this._direction = v;
            switch (this._direction) {
                case #right : this.frame = 1; break;
                case #left  : this.frame = 3; break;
                case #up    : this.frame = 0; break;
                case #down  : this.frame = 2; break;
            }
        }
    }     }

 Scene   Sprite   TiledLayer 
Write a Comment (0)
Subject
Please complete this mandatory field.
HTML Tags Not Allowed!
Comment
Please complete this mandatory field.