emo-framework in android move on collision of sprites with physics

Posted by KaHeL on Game Development See other posts from Game Development or by KaHeL
Published on 2012-09-13T04:39:54Z Indexed on 2012/09/13 9:51 UTC
Read the original article Hit count: 351

Filed under:

I'm developing my first ever game for Android where I'm still learning about using of framework. To begin I made two sprites of ball where one ball is movable by dragging and another one is just standing on it's place on load. Now I've already added the collision listener for both sprites and as tested it's working properly. Now what I need to learn is on how can I add physics on both sprites where when they collide the standing sprite will move based on the physics and bounce around the screen. It would be best if you teach it to me step by step since I'm a little slow on this.

Here's my nut so far:

local stage = emo.Stage();

class Okay_1 {
    sprite = null;
    spriteok = null;
    dragStart = false;
    angle = 0;

// Called when the stage is loaded
    function onLoad() {

        print("Level_1 is loaded!");

        // Create new sprite and load 'f1.png'
        sprite = emo.Sprite("f1.png");
        sprite.moveCenter(stage.getWindowWidth()  * 0.5,
                   stage.getWindowHeight() * 0.5);
        sprite.load();

       spriteok = emo.Sprite("okay.png")
       spriteok.setWidth(100);
       spriteok.setHeight(100);
       spriteok.load();

       // Check if the coordinate (X=100, Y=100) is inside the sprite
        if (spriteok.contains(100, 100)) {
        print("contains!");
        }
        // Does the sprite collides with the other sprite?
        if (spriteok.collidesWith(sprite)) {
        print("collides!");
        }

    }

function onMotionEvent(ev) {
    if (ev.getAction() == MOTION_EVENT_ACTION_DOWN) {
                // Moves the sprite at the position of motion event
                angle = sprite.getAngle();
                sprite.remove();
                sprite = emo.Sprite("f2.png");
                sprite.load();
                sprite.rotate(angle);
                sprite.moveCenter(ev.getX(), ev.getY());
                sprite.rotate(sprite.getAngle()+10);

                // Check if the coordinate (X=100, Y=100) is inside the sprite
                if (sprite.contains(sprite.getWidth(), sprite.getHeight())) {
                print("contains!");
                }
                // Does the sprite collides with the other sprite?
                if (sprite.collidesWith(spriteok)) {
                print("collides!");
                }

                dragStart = true;
    }else if (ev.getAction() == MOTION_EVENT_ACTION_MOVE) {
         if (dragStart) {
                // Moves the sprite at the position of motion event
                sprite.moveCenter(ev.getX(), ev.getY());
                sprite.rotate(sprite.getAngle()+10);

                // Check if the coordinate (X=100, Y=100) is inside the sprite
                if (sprite.contains(sprite.getWidth(), sprite.getHeight())) {
                print("contains!");
                }
                // Does the sprite collides with the other sprite?
                if (sprite.collidesWith(spriteok)) {
                print("collides!");
                }
            }
    }else if (ev.getAction() == MOTION_EVENT_ACTION_UP ||
                   ev.getAction() == MOTION_EVENT_ACTION_CANCEL) {
            if (dragStart) {
                // change block color to red
                dragStart = false;
                angle = sprite.getAngle();
                sprite.remove();
                sprite = emo.Sprite("f1.png");
                sprite.load();
                sprite.moveCenter(ev.getX(), ev.getY());
                sprite.rotate(angle);

                // Check if the coordinate (X=100, Y=100) is inside the sprite
                if (sprite.contains(sprite.getWidth(), sprite.getHeight())) {
                print("contains!");
                }
                // Does the sprite collides with the other sprite?
                if (sprite.collidesWith(spriteok)) {
                print("collides!");
                }
            }
        }
}    


// Called when the stage is disposed
    function onDispose() {
        sprite.remove(); // Remove the sprite
        print("Level_1 is disposed!");
    }
}

function emo::onLoad() {
    emo.Stage().load(Okay_1());
}

© Game Development or respective owner

Related posts about android