使用 AndEngine 和 Box2D 制作英雄跳跃

发布于 2024-11-25 15:34:37 字数 5011 浏览 0 评论 0原文

我是引擎的新手。目前我想使用 AndEngine 和 Box2D 让我的游戏英雄跳跃。我已经尝试了很多方法来做到这一点。但未能成功。

这是我的代码,用于使角色在触摸屏幕上显示的左右箭头时左右行走。现在我想让它在触摸向上箭头时跳跃。 这是我的代码...

public jump(int pLayerCount, final BaseGameActivity game) {
    super(pLayerCount);
    this.game=game;

    gravity=new Vector2(0,SensorManager.GRAVITY_EARTH);
    physicsWorld=new PhysicsWorld(gravity, false);
    fixture=PhysicsFactory.createFixtureDef(1f, 1f, 1f);

    camera_height=game.getEngine().getCamera().getHeight();
    camera_width=game.getEngine().getCamera().getWidth();
    bottom=new Rectangle(0, camera_height-5,camera_width,3);

    PhysicsFactory.createBoxBody(physicsWorld, bottom,BodyType.StaticBody, fixture);
    this.registerUpdateHandler(physicsWorld);
    this.attachChild(bottom);

    //walking image
       texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        tiletextureRegion=TextureRegionFactory.createTiledFromAsset(texture,game, "images/walk_forward.png",0,90,5,1);
        game.getEngine().getTextureManager().loadTexture(texture);
        walk_forward=new AnimatedSprite(0.0f,160.0f,tiletextureRegion);

        this.attachChild(walk_forward);


    //Up_arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/up_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite up_arrow=new Sprite(30.0f,10.0f,textureRegion){
    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
            float pTouchAreaLocalX, float pTouchAreaLocalY) 
    {   
            left_touch=false;
            right_touch=false;
            up_touch=true;
            return true;            
    }

};


    //Left Arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/left_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite left_arrow=new Sprite(2.0f,34.0f,textureRegion){
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                float pTouchAreaLocalX, float pTouchAreaLocalY) 
        {   
                left_touch=true;
                right_touch=false;
                walk_forward.animate(150);
                return true;            
        }

    };

    //right Arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/right_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite right_arrow=new Sprite(60.0f,34.0f,textureRegion){

        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                float pTouchAreaLocalX, float pTouchAreaLocalY) 
        {   
                right_touch=true;
                left_touch=false;
                //walk_forward.animate(150);
                return true;            
            }
    };

    //Down Arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/down_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite down_arrow=new Sprite(30.0f,60.0f,textureRegion);

    this.attachChild(up_arrow);
    this.attachChild(left_arrow);
    this.attachChild(right_arrow);
    this.attachChild(down_arrow);
    this.registerTouchArea(right_arrow);
    this.registerTouchArea(left_arrow);
    this.registerTouchArea(up_arrow);
    this.setTouchAreaBindingEnabled(true);  


    //jumping image_physics
     texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/jump_up.png",0,90);
        game.getEngine().getTextureManager().loadTexture(texture);
        jump_up=new Sprite(0.0f,220.0f,textureRegion);
        this.attachChild(jump_up);  
        fixture_jump=PhysicsFactory.createFixtureDef(1f, 0f, 1f);
        jump_up_body=PhysicsFactory.createBoxBody(physicsWorld, jump_up,BodyType.DynamicBody, fixture_jump);
        //jump_up_body.fixedRotation=true;
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(jump_up,jump_up_body));  
    //  physicsWorld.clearForces();
        hero_speed = 2; //just test and find values that work well
        hero_max_speed = 4;
        hero_normal = new Vector2(0, 0);
        jump_speed = 16;

}

protected void onManagedUpdate(float pSecondsElapsed)
{
    if(right_touch==true)
    {
        x=x+1f;
        walk_forward.setPosition(x, walk_forward.getY());
    }
    if(left_touch==true)
    {
        x=x-1f;
       walk_forward.setPosition(x, walk_forward.getY());
    }

    if(up_touch==true)
    {
        jump_up_body.applyLinearImpulse(new Vector2(0,  -jump_speed), jump_up_body.getWorldCenter());
        walk_forward.getY()+30);
    }
    super.onManagedUpdate(pSecondsElapsed);
}

I am new to and engine. currently i want to make my game hero jump using AndEngine and Box2D. I have tried a lot of ways to do so. But could not succeed.

Here is my code for making character walk left and right on touching left and right arrows displayed on screen. Now i want to make it jump on touching up arrow.
Here is my code...

public jump(int pLayerCount, final BaseGameActivity game) {
    super(pLayerCount);
    this.game=game;

    gravity=new Vector2(0,SensorManager.GRAVITY_EARTH);
    physicsWorld=new PhysicsWorld(gravity, false);
    fixture=PhysicsFactory.createFixtureDef(1f, 1f, 1f);

    camera_height=game.getEngine().getCamera().getHeight();
    camera_width=game.getEngine().getCamera().getWidth();
    bottom=new Rectangle(0, camera_height-5,camera_width,3);

    PhysicsFactory.createBoxBody(physicsWorld, bottom,BodyType.StaticBody, fixture);
    this.registerUpdateHandler(physicsWorld);
    this.attachChild(bottom);

    //walking image
       texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        tiletextureRegion=TextureRegionFactory.createTiledFromAsset(texture,game, "images/walk_forward.png",0,90,5,1);
        game.getEngine().getTextureManager().loadTexture(texture);
        walk_forward=new AnimatedSprite(0.0f,160.0f,tiletextureRegion);

        this.attachChild(walk_forward);


    //Up_arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/up_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite up_arrow=new Sprite(30.0f,10.0f,textureRegion){
    public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
            float pTouchAreaLocalX, float pTouchAreaLocalY) 
    {   
            left_touch=false;
            right_touch=false;
            up_touch=true;
            return true;            
    }

};


    //Left Arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/left_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite left_arrow=new Sprite(2.0f,34.0f,textureRegion){
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                float pTouchAreaLocalX, float pTouchAreaLocalY) 
        {   
                left_touch=true;
                right_touch=false;
                walk_forward.animate(150);
                return true;            
        }

    };

    //right Arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/right_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite right_arrow=new Sprite(60.0f,34.0f,textureRegion){

        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                float pTouchAreaLocalX, float pTouchAreaLocalY) 
        {   
                right_touch=true;
                left_touch=false;
                //walk_forward.animate(150);
                return true;            
            }
    };

    //Down Arrow
    texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
    textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/down_arrow.png",0,0);
    game.getEngine().getTextureManager().loadTexture(texture);
    Sprite down_arrow=new Sprite(30.0f,60.0f,textureRegion);

    this.attachChild(up_arrow);
    this.attachChild(left_arrow);
    this.attachChild(right_arrow);
    this.attachChild(down_arrow);
    this.registerTouchArea(right_arrow);
    this.registerTouchArea(left_arrow);
    this.registerTouchArea(up_arrow);
    this.setTouchAreaBindingEnabled(true);  


    //jumping image_physics
     texture=new Texture(256,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        textureRegion=TextureRegionFactory.createFromAsset(texture,game, "images/jump_up.png",0,90);
        game.getEngine().getTextureManager().loadTexture(texture);
        jump_up=new Sprite(0.0f,220.0f,textureRegion);
        this.attachChild(jump_up);  
        fixture_jump=PhysicsFactory.createFixtureDef(1f, 0f, 1f);
        jump_up_body=PhysicsFactory.createBoxBody(physicsWorld, jump_up,BodyType.DynamicBody, fixture_jump);
        //jump_up_body.fixedRotation=true;
        physicsWorld.registerPhysicsConnector(new PhysicsConnector(jump_up,jump_up_body));  
    //  physicsWorld.clearForces();
        hero_speed = 2; //just test and find values that work well
        hero_max_speed = 4;
        hero_normal = new Vector2(0, 0);
        jump_speed = 16;

}

protected void onManagedUpdate(float pSecondsElapsed)
{
    if(right_touch==true)
    {
        x=x+1f;
        walk_forward.setPosition(x, walk_forward.getY());
    }
    if(left_touch==true)
    {
        x=x-1f;
       walk_forward.setPosition(x, walk_forward.getY());
    }

    if(up_touch==true)
    {
        jump_up_body.applyLinearImpulse(new Vector2(0,  -jump_speed), jump_up_body.getWorldCenter());
        walk_forward.getY()+30);
    }
    super.onManagedUpdate(pSecondsElapsed);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

虫児飞 2024-12-02 15:34:37

查看 AndEngine 示例,尤其是 物理跳跃示例

本质上来说,要跳跃,您需要设置物理体的线速度。

Vector2 velocity = new Vector2(0, -50); //experiment with the numbers!!
body.setLinearVelocity(velocity);

Take a look at the AndEngine samples especially Physics Jump Example

Essentially to jump, you set the linear velocity of the Physics body.

Vector2 velocity = new Vector2(0, -50); //experiment with the numbers!!
body.setLinearVelocity(velocity);
做个少女永远怀春 2024-12-02 15:34:37

玩家跳跃:

playerBody.setLinearVelocity(new Vector2(playerBody.getLinearVelocity().x+1,-10f));
playerBody.setLinearVelocity(new Vector2(2,playerBody.getLinearVelocity().y));

玩家移动:

playerBody.setLinearVelocity((int)5, 400);              

player jump:

playerBody.setLinearVelocity(new Vector2(playerBody.getLinearVelocity().x+1,-10f));
playerBody.setLinearVelocity(new Vector2(2,playerBody.getLinearVelocity().y));

player move:

playerBody.setLinearVelocity((int)5, 400);              
小糖芽 2024-12-02 15:34:37

首先创建你的精灵,然后将其添加到物理体中。

Animatedsprite yas = new Animatedsprite(XPosition,YPosition, (ITiledTextureRegion) this.yourspriteregion, this.getVertexBufferObjectManager());

Body yasbody;
this.physicsworld.registerPhysicsConnector(new PhysicsConnector(yas, yasbody, true, true));

yasbody.setLinearVelocity(velocity);

First create your sprite and then add it to physics body.

Animatedsprite yas = new Animatedsprite(XPosition,YPosition, (ITiledTextureRegion) this.yourspriteregion, this.getVertexBufferObjectManager());

Body yasbody;
this.physicsworld.registerPhysicsConnector(new PhysicsConnector(yas, yasbody, true, true));

yasbody.setLinearVelocity(velocity);
洛阳烟雨空心柳 2024-12-02 15:34:37

您需要设置实际速度 x 才能正确跳跃。
例子:

playerBody.setLinearVelocity(new Vector2(playerBody.getLinearVelocity().x,-jump_speed));

You need set actual velocity x for to jump correctly.
Example:

playerBody.setLinearVelocity(new Vector2(playerBody.getLinearVelocity().x,-jump_speed));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文