如何在AndEngine中同时反弹,移动和跳跃精灵

时间:2013-02-21 09:35:06

标签: android android-layout box2d andengine

我希望以与this video中相同的方式移动精灵。我正在使用AndEngine。如何使用AndEngine Box2D物理扩展来移动那个ninja(精灵),完成移动和跳跃。

这是我的MainActivity1.class:

public class MainActivity1 extends BaseGameActivity {

    Scene scene;
    private TiledTextureRegion mPlayerTextureRegion;
    private BoundCamera mBoundChaseCamera;
    private static final int CAMERA_WIDTH = 1024;
    private static final int CAMERA_HEIGHT = 600;
    private BitmapTextureAtlas mBitmapTextureAtlas;
    private BitmapTextureAtlas mBitmapTextureAtlasp;
    private BitmapTextureAtlas mBitmapTextureAtlashm;
    private TextureRegion mhomeTextureRegion;
    private BitmapTextureAtlas mBitmapTextureAtlas1;
    AnimatedSprite player;
    private Body playerBody;
    private PhysicsWorld mPhysicsWorld;
    private BitmapTextureAtlas sheetBitmapTextureAtlas1;
    private BitmapTextureAtlas sheetBitmapTextureAtlasplaybtn1;
    private TiledTextureRegion mTargetTextureRegionplaybtn1;


    public void onLoadComplete() {
        // TODO Auto-generated method stub

    }

    public Engine onLoadEngine() {
        // TODO Auto-generated method stub
        final Display display = getWindowManager().getDefaultDisplay();
        int cameraWidth = display.getWidth();
        int cameraHeight = display.getHeight();
    //      Toast.makeText(this, "The tile the player is walking on will be highlighted.", Toast.LENGTH_LONG).show();
        this.mBoundChaseCamera = new BoundCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(cameraWidth, cameraHeight), this.mBoundChaseCamera));
    }

    public void onLoadResources() {
        // TODO Auto-generated method stub

        mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,
                TextureOptions.BILINEAR_PREMULTIPLYALPHA);

        mBitmapTextureAtlas1 = new BitmapTextureAtlas(1024, 1024,
                TextureOptions.BILINEAR_PREMULTIPLYALPHA);
        this.sheetBitmapTextureAtlasplaybtn1 = new BitmapTextureAtlas(512, 512);
        mTargetTextureRegionplaybtn1 = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(sheetBitmapTextureAtlasplaybtn1, this,
                        "gfx/gameplay_stormstrike_full.png",  0, 212,1, 1);
    this.mBitmapTextureAtlasp = new BitmapTextureAtlas(512, 512, TextureOptions.DEFAULT);
        this.mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlasp, this, "gfx/storm_strike.png", 0, 0, 1, 1); // 72x128


        mTargetTextureRegion1 = BitmapTextureAtlasTextureRegionFactory
                .createTiledFromAsset(sheetBitmapTextureAtlas1, this,
                        "gfx/kolobok_04_walk.png",  0, 212,12, 1);

        this.mEngine.getTextureManager().loadTexture(this.sheetBitmapTextureAtlas);
        this.mEngine.getTextureManager().loadTexture(this.sheetBitmapTextureAtlas1);
        BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlashm, this, "gfx/loading_ipad.png", 0, 0);
        mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas);
        mEngine.getTextureManager().loadTexture(sheetBitmapTextureAtlasplaybtn1);

        mEngine.getTextureManager().loadTexture(mBitmapTextureAtlasp);
        mEngine.getTextureManager().loadTexture(mBitmapTextureAtlas1);
        mEngine.getTextureManager().loadTexture(mBitmapTextureAtlashm);

    }

    public Scene onLoadScene() {
        // TODO Auto-generated method stub
        this.mEngine.registerUpdateHandler(new FPSLogger());

        scene = new Scene();
        scene.setBackground(new ColorBackground(0, 0, 0.8784f));


        this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 8, 1);
        final Sprite help = new Sprite(50, 70, mProjectileTextureRegionhelp);
        scene.attachChild(help);
        help.setVisible(false);




        player = new AnimatedSprite(100, 200, 150,150,this.mPlayerTextureRegion)
        {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                switch(pSceneTouchEvent.getAction()) {
                    case TouchEvent.ACTION_DOWN:


                     playerBody.setLinearVelocity(10f,-10f);


                        break;
                    case TouchEvent.ACTION_MOVE:




                        break;
                    case TouchEvent.ACTION_UP:

                        break;
                }
                return true;
            }
        };
        scene.attachChild(player);
        scene.registerTouchArea(player);
        projectilesToBeAdded.add(player);





        AnimatedSprite playerbtn = new AnimatedSprite(10, 200, this.mTargetTextureRegionplaybtn1)
        {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                switch(pSceneTouchEvent.getAction()) {
                    case TouchEvent.ACTION_DOWN:

                        playerBody.applyForce(new Vector2(200,-1500), new Vector2(playerBody.getWorldCenter()));






                        break;
                    case TouchEvent.ACTION_MOVE:



                        break;
                }
                return true;
            }
        };
        scene.attachChild(playerbtn);
        scene.registerTouchArea(playerbtn);


        final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(10.0f, 0.5f, 0.5f);
        this.playerBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, this.player, BodyType.DynamicBody, carFixtureDef);

        this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(this.player, this.playerBody, true,false));






        scene.setTouchAreaBindingEnabled(true);

        this.scene.registerUpdateHandler(this.mPhysicsWorld);


        return scene;
    }
}

1 个答案:

答案 0 :(得分:2)

示例视频中发生的运动涉及几个不同的概念。您应该阅读AndEngine示例。特别是,看看这两个:

  • org.andengine.examples.PhysicsExample
  • org.andengine.examples.PhysicsJumpExample

要开始跳转,你需要在忍者的物理主体上调用applyForce()。你需要在忍者和他反弹的表面上产生摩擦以产生反弹。根据下面的评论,我将提供一些额外的定义。顺便说一句,这些不是来自我,而是来自the manual

密度

  

夹具密度用于计算材料的质量属性   父体。密度可以是零或正。你应该一般   对所有灯具使用相似的密度。这将改善   堆叠稳定性。设定时不调整身体的质量   密度。您必须调用ResetMassData才能实现此目的。

速度

  

在运动学中,速度是一个位置的变化率   对象,相当于其速度和方向的规范   运动。速度仅描述物体移动的速度,而   速度给出物体的速度和方向   移动。

摩擦

  

摩擦力用于使物体实际地相互滑动。   Box2D支持静态和动态摩擦,但使用相同的   两者的参数。在Box2D中精确模拟摩擦力   摩擦强度与法向力成正比(这称为   库仑摩擦)。摩擦参数通常设置在0和0之间   1,但可以是任何非负值。摩擦值为0时关闭   摩擦力和值1使摩擦力很强。当摩擦   在两个形状之间计算力,Box2D必须结合摩擦力   两个父灯具的参数。这是用几何学完成的   意味着:

float32 friction;

friction = sqrtf(shape1->friction * shape2->friction);
  

因此,如果一个夹具零摩擦,那么触点将为零   摩擦。