android - 如何在 AndEngine 中同时弹跳、移动和跳跃 Sprite

标签 android android-layout box2d andengine

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

这是我的 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;
    }
}

最佳答案

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

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

要开始跳跃,您需要在忍者的物理 body 上调用 applyForce()。您需要忍者和他弹跳的表面产生摩擦才能产生弹跳。根据下面的评论,我将提供一些额外的定义。顺便说一下,这些不是我写的,而是来自 the manual .

密度

The fixture density is used to compute the mass properties of the parent body. The density can be zero or positive. You should generally use similar densities for all your fixtures. This will improve stacking stability. The mass of a body is not adjusted when you set the density. You must call ResetMassData for this to occur.

速度

In kinematics, velocity is the rate of change of the position of an object, equivalent to a specification of its speed and direction of motion. Speed describes only how fast an object is moving, whereas velocity gives both how fast and in what direction the object is moving.

摩擦力

Friction is used to make objects slide along each other realistically. Box2D supports static and dynamic friction, but uses the same parameter for both. Friction is simulated accurately in Box2D and the friction strength is proportional to the normal force (this is called Coulomb friction). The friction parameter is usually set between 0 and 1, but can be any non-negative value. A friction value of 0 turns off friction and a value of 1 makes the friction strong. When the friction force is computed between two shapes, Box2D must combine the friction parameters of the two parent fixtures. This is done with the geometric mean:

float32 friction;

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

So if one fixture has zero friction then the contact will have zero friction.

关于android - 如何在 AndEngine 中同时弹跳、移动和跳跃 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14998787/

相关文章:

java - body.setTransform 在接触监听器(andEngine 和 box2d)中不起作用

c++ - 自动移动一个 Sprite 来捕捉另一个 Sprite

android - transformDexArchiveWithDexMergerForRelease 和 transformDexArchiveWithDexMergerForDebug 任务之间的区别?

java - Android sqlite 列未找到错误

Android 日志 dontpanic

android - TextInputLayout在聚焦和未聚焦状态下如何保持文本颜色相同?

android - 哪种嵌套布局顺序在 Android 中最有效

android - 设置表面 View 的背景颜色

android - 如何实现整个 Activity 的多点触控?

java - 如何将 Sprite 添加到 Box2d Body?