Win an Ipad
  Developer   Tutorials   Breakout   Game sprites
Lesson 0

Game sprites

Those Game sprites that require physics behavior need to be extended from the PhysicsSprite class. It combines the advantages of the Game2D Sprite, b2Body  and Moscrif native Box2D library. All sprites are located in application Sprite directory.

Brick Physics Sprite

Let’s have a look how to define Breakout’s bricks. First of all, you should create a bitmap graphics that include all the brick types. Then you can create a class extended from PhysicsSprite.

Brick types:

Type 1 - When Brick is hit by the ball, it dissapears

Type 2 - When Brick is hit by the ball, it is changed to Brick Type 1

Type 3 - When Brick is hit by the ball, it is changed to Brick Type 2

Type 4 - When Brick is hit by the ball, it is changed to Brick Type 3

Brick frames, starts with brick level 1 and ends with brick level 4 :

include "lib://box2d/physicsSprite.ms"; class Brick : PhysicsSprite
{
    var count;     function init()
    {
        super.init();         // update count
        if (count > 0)
            count += 1;
        else
            // first brick
            count = 1;         this.state = 1;         //image brigs.png
        this.image = res.img.brisks;
        this.frameWidth = 53;
        this.frameHeight = 17;         this.bodyType = #static;
        this.density = 0.0;
        this.friction = 0.0;
        this.bounce = 0.1;         //decoration object
        this._paint = new Paint();
        this._paint.alpha = 0;
        this.show(this);     }     property state(v)
    {
        get return this._state;
        set {
            this.frame = v;
            this._state = v;
        }
    }     function hit()
    {
        if (this.state < 1) {
            count--;
            this.hide(this);
        } else {
            this.state -= 1;
        }
    }
    function hide(obj)
    {   this.scene._bodiesToInactive.push(this);
        var animator = new Animator({
            transition: Animator.Transition.easeOut,    // start up slowly and then quickly speed up at the end of the animation
            duration: 500,                             // length of animation in miliseconds
        });
        animator.addSubject(function(state) {           // state starts from 1.0 to 0.0
            obj._paint.alpha = 255-(state*255).toInteger();
        });
        animator.options.onComplete = function() {
            this super.scene._bodiesToDestory.push(this super);
        }
        animator.play();
    }     function show(obj)
    {
        var animator = new Animator({
            transition: Animator.Transition.easeIn,     // start up slowly and then quickly speed up at the end of the animation
            duration: 500,                             // length of animation in miliseconds
        });
        animator.addSubject(function(state) {           // state starts from 1.0 to 0.0
            obj._paint.alpha = 255-(state*255).toInteger();
        });
       animator.reverse();
    }     function destroy()
    {
        count -= 1;
    } }


Disk Physics Sprite

 

Disk consists of four sprite frames. Frames are changed in short time intervals; which makes nice animation effect. Electric sound effect is added as well.

Disk frames:

include "lib://box2d/physicsSprite.ms"; class Disk : PhysicsSprite
{
    function init()
    {
        super.init();         //image disks.png
        this.image = res.img.disks;
        this.frameWidth = 180;
        this.frameHeight = 51;
        this.shape = null;
        this.shapeType = #polygon;
        this.bodyType = #static;
        this.density = 0.0;
        this.friction = 0.0;
        this.bounce = 0.1;
        this.enableSound = true;         this._loadGameSounds();         this._timer = new Timer(10000, false); // interval make no sense here
        this._timer.start(res.values.electricShockMin + rand(res.values.electricShockGap));
        this._timer.onTick = function(sender) {this super._eletricShock(1)};
    }     property enableSound(v)
    {
        get return this._enableSound
        set this._enableSound = v;
    }     function _eletricShock(frame = 0)
    {
        if (frame == 1 && this.enableSound)
            this._wavElectric.play();         this.frame = frame;
        frame++;         if (frame == 3)
            frame = 0;         this._timer = new Timer(100, false); // interval make no sense here
        if (frame != 1)
            this._timer.start(100);
        else
            this._timer.start(res.values.electricShockMin + rand(res.values.electricShockGap));
        this._timer.onTick = function(sender) {this super._eletricShock(frame)};
    }     function _loadGameSounds()
    {
        // Loading sounds
        if (this.enableSound) {
            this._wavElectric = new AudioPlayer();
            this._wavElectric.openFile(res.sounds.eletric);
        }
    } }


Ball Physics Sprite

 

Ball sprite is a simple physics object with just one frame. X and Y coordinates are recalculated constantly; based on physics world within game process.

Ball image :

include "lib://box2d/physicsSprite.ms"; class Ball : PhysicsSprite
{
    function init()
    {
        super.init();
        //image ball-ingame.png
        this.image = res.img.ball;
        this.radius = 14.0;
        this.shape = null;
        this.shapeType = #circle;
        this.bodyType = #dynamic;
        this.density = 0.0;
        this.friction = 0.0;
        this.bounce = 1.0;
    } }


Level Sprite

 

Level is simple Game2D sprite that does not require a physics behaviour. It is displayed when new level is reached.

Image frames :

 

include "lib://game2d/sprite.ms"; class Level : Sprite
{
    function init()
    {
       super.init();
       this.paint = new Paint();
    }     function show() {
        this._visible = true;
        app.pulse(this);
    }     function hide() {
        var animator = new Animator({
            transition: Animator.Transition.easeIn, // start up slowly and then quickly speed up at the end of the animation
            duration: 1500,                         // length of animation in miliseconds
        });
        animator.addSubject(function(state) {       // state starts from 1.0 to 0.0
           this super.paint.alpha = (state*255).toInteger();
        })
        animator.onComplete = function()
        {
            this super.visible = false;
        }
        animator.reverse();
    }     property visible(v)
    {
        get return this._visible;
        set this._visible = v;
    }
}

 

 

 Game sprites   Game scenes 
Tutorial details and chapters
Technology:
Difficulty:
Completion:
Breakout chapters