-6

I am new to 'andengine' and I developing simple game like 'mario' so I want to jump and move that character in a proper way.

Please give me some ideas, I use velocity and impulse but that is not working properly.

4

2 回答 2

2

AndEngineExamples project contains several sample games which are similar to your application. You can download APK and tryout first before digging into the code.

AndEngineExamples

于 2013-05-30T13:33:43.897 回答
1

Here's how I did it (my interface to my Player class):

public void moveLeft() {
    Vector2 velocity = Vector2Pool.obtain(-speed, body.getLinearVelocity().y);
    body.setLinearVelocity(velocity);                   
    Vector2Pool.recycle(velocity);

    int offset = isInjured ? 4 : 0;     
    if (!isAnimationRunning()) {
        setCurrentTileIndex(2 + offset);
        animate(new long[]{200, 200}, 2 + offset, 3 + offset, 1, animationListener);    
    }
}

public void moveRight() {
    Vector2 velocity = Vector2Pool.obtain(speed, body.getLinearVelocity().y);
    body.setLinearVelocity(velocity);               
    Vector2Pool.recycle(velocity);  

    int offset = isInjured ? 4 : 0; 
    if (!isAnimationRunning()) {
        setCurrentTileIndex(0 + offset);
        animate(new long[]{200, 200}, 0 + offset, 1 + offset, 1, animationListener);        
    }
}

public void stop() {
    Vector2 velocity = Vector2Pool.obtain(0, body.getLinearVelocity().y);
    body.setLinearVelocity(velocity);                   
    Vector2Pool.recycle(velocity);

    stopAnimation();

    int offset = isInjured ? 4 : 0;
    if(getCurrentTileIndex() % 4 > 1 ) 
        setCurrentTileIndex(2 + offset);
    else 
        setCurrentTileIndex(0 + offset);
}

public void stopJump() {
    if(jumpsLeft > 0) {
        Vector2 velocity = Vector2Pool.obtain(body.getLinearVelocity().x, body.getLinearVelocity().y / 5);
        body.setLinearVelocity(velocity);                   
        Vector2Pool.recycle(velocity);  
    }
}

public void jump() {
    if(jumpsLeft > 0) {
        Vector2 impulse = Vector2Pool.obtain(0, body.getMass() * -12);
        body.applyLinearImpulse(impulse, body.getWorldCenter());                
        Vector2Pool.recycle(impulse);   
        jumpsLeft--;
    }
}

Then, I created buttons for moving left, right, jumping, etc. In the AreaTouched event, for action down, I do player.jump(). For action up, I do player.stopJump(). The code for the move_left and move_right buttons is pretty much the same.

于 2013-05-30T19:24:13.557 回答