首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PhysicsHandler未按预期工作- AndEngine

PhysicsHandler未按预期工作- AndEngine
EN

Stack Overflow用户
提问于 2012-05-24 11:31:06
回答 1查看 1.3K关注 0票数 1

对于AndEngine来说,Guys是一个完全的新手。我想使用物理处理程序,并查看了和引擎的示例。因此,在我的例子中,我有一个“球”精灵,我可以通过触摸来移动,以及第二个“方形”动画精灵,我希望它能弹跳到墙上。但它只是刚刚从屏幕上消失。帮帮我..。

代码语言:javascript
复制
    package com.Escapo;

    import org.anddev.andengine.engine.Engine;
    import org.anddev.andengine.engine.camera.Camera;
    import org.anddev.andengine.engine.handler.physics.PhysicsHandler;
    import org.anddev.andengine.engine.options.EngineOptions;
    import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
    import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
    import org.anddev.andengine.entity.scene.Scene;
    import org.anddev.andengine.entity.scene.Scene.IOnSceneTouchListener;
    import org.anddev.andengine.entity.scene.background.ColorBackground;
    import org.anddev.andengine.entity.sprite.AnimatedSprite;
    import org.anddev.andengine.entity.sprite.Sprite;
    import org.anddev.andengine.entity.util.FPSLogger;
    import org.anddev.andengine.input.touch.TouchEvent;
    import org.anddev.andengine.opengl.texture.TextureOptions;
    import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
    import    org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
    import org.anddev.andengine.opengl.texture.region.TextureRegion;
    import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;
    import org.anddev.andengine.ui.activity.BaseGameActivity;

    import android.view.Display;



    public class EscapoActivity extends BaseGameActivity {

private Camera mCamera;
private Scene mMainScene;

//main ball variables
private BitmapTextureAtlas mBitmapTextureAtlas;
private TextureRegion mPlayerTextureRegion;
private Sprite player;
//square
private BitmapTextureAtlas mBitmapTextureAtlas2;
private static final float DEMO_VELOCITY = 100.0f;
private TiledTextureRegion mFaceTextureRegion;
private static int cameraWidth;
private static int cameraHeight;
private Sprite square;
@Override
public void onLoadComplete() {


}

@Override
public Engine onLoadEngine() {

    final Display display = getWindowManager().getDefaultDisplay();

        cameraWidth = display.getWidth();

        cameraHeight = display.getHeight();



        mCamera = new Camera(0, 0, cameraWidth, cameraHeight);



        return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE,

            new RatioResolutionPolicy(cameraWidth, cameraHeight), mCamera));

}

@Override
public void onLoadResources() {



            mBitmapTextureAtlas = new BitmapTextureAtlas(512, 512,

                TextureOptions.BILINEAR_PREMULTIPLYALPHA);

            //square
            mBitmapTextureAtlas2 = new BitmapTextureAtlas(512, 512,

                    TextureOptions.BILINEAR_PREMULTIPLYALPHA);


            BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");



            mPlayerTextureRegion = BitmapTextureAtlasTextureRegionFactory

                .createFromAsset(this.mBitmapTextureAtlas, this, "ball.png",

                0, 0);

            //square
            this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas2, this, "square.png", 0, 0, 1, 1);

            mEngine.getTextureManager().loadTextures(mBitmapTextureAtlas,mBitmapTextureAtlas2);




}

@Override
public Scene onLoadScene() {

     mEngine.registerUpdateHandler(new FPSLogger());



         mMainScene = new Scene();

         mMainScene

             .setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));


         final int PlayerX =(int) ((mCamera.getWidth() - mPlayerTextureRegion

                 .getWidth()) / 2);
         final int PlayerY = (int) ((mCamera.getHeight() - mPlayerTextureRegion

             .getHeight()) / 2);



         player = new Sprite(PlayerX, PlayerY, mPlayerTextureRegion) {

                 public boolean onAreaTouched(TouchEvent pSceneTouchEvent,float pTouchAreaLocalX, float pTouchAreaLocalY) {

                    this.setPosition(pSceneTouchEvent.getX()- this.getWidth() / 2, pSceneTouchEvent.getY()- this.getHeight() / 2);




                     return true;

                 }

             };
             mMainScene.registerTouchArea(player);

                mMainScene.setTouchAreaBindingEnabled(true);

         mMainScene.attachChild(player);
         //square
         final Ball ball = new Ball(2, 2, this.mFaceTextureRegion);
         final PhysicsHandler physicsHandler = new PhysicsHandler(ball);
         ball.registerUpdateHandler(physicsHandler);
         physicsHandler.setVelocity(DEMO_VELOCITY, DEMO_VELOCITY);

         mMainScene.getLastChild().attachChild(ball);

         return mMainScene;

}


private static class Ball extends AnimatedSprite {
    private final PhysicsHandler mPhysicsHandler;

    public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion) {
            super(pX, pY, pTextureRegion);
            this.mPhysicsHandler = new PhysicsHandler(this);
            this.registerUpdateHandler(this.mPhysicsHandler);
    }

    @Override
    protected void onManagedUpdate(final float pSecondsElapsed) {
            if(this.mX < 0) {
                    this.mPhysicsHandler.setVelocityX(DEMO_VELOCITY);
            } else if(this.mX + this.getWidth() > cameraWidth) {
                    this.mPhysicsHandler.setVelocityX(-DEMO_VELOCITY);
            }

            if(this.mY < 0) {
                    this.mPhysicsHandler.setVelocityY(DEMO_VELOCITY);
            } else if(this.mY + this.getHeight() > cameraHeight) {
                    this.mPhysicsHandler.setVelocityY(-DEMO_VELOCITY);
            }

            super.onManagedUpdate(pSecondsElapsed);
    }
 }




 } 

我刚刚试着集成了移动球的例子http://code.google.com/p/andengineexamples/source/browse/src/org/anddev/andengine/examples/MovingBallExample.java,我哪里错了?

EN

回答 1

Stack Overflow用户

发布于 2012-08-04 00:39:47

我不确定PhysicsHandler是否能解决你的问题,因为我认为它只是为了帮助你移动你的实体。

尝试对AndEngine进行Box2d扩展。

在AndEngine示例中,有PhysicsJumpExample.java (在启动器中称为“结合物理和触摸”),它基本上是让对象在触摸时跳跃,所以我猜它接近你想要的效果。

下面是该文件中的一些代码,用于创建墙:

代码语言:javascript
复制
//onCreateScene
    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();
    final Rectangle ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2, vertexBufferObjectManager);
    final Rectangle roof = new Rectangle(0, 0, CAMERA_WIDTH, 2, vertexBufferObjectManager);
    final Rectangle left = new Rectangle(0, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);
    final Rectangle right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT, vertexBufferObjectManager);

    final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
    PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

    this.mScene.attachChild(ground);
    this.mScene.attachChild(roof);
    this.mScene.attachChild(left);
    this.mScene.attachChild(right);

要使物体跳跃,只需将速度设置为负重力即可。重力会在它最终达到0后将其拉回:

代码语言:javascript
复制
final Vector2 velocity = Vector2Pool.obtain(this.mGravityX * -50, this.mGravityY * -50);
    faceBody.setLinearVelocity(velocity);
    Vector2Pool.recycle(velocity);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10730768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档