在AndEngine中,如何拖动未被触摸时会掉落的物体?

时间:2012-06-16 18:56:57

标签: android andengine physics

我是AndEngine的新手并且有点麻烦。我想在屏幕上拖动一个精灵,当手指从精灵中抬起时,让物体落到屏幕的底部。每次触摸屏幕(但不是精灵)时,都会在该点创建一个新的精灵。我试图结合每次触摸时创建形状的PhysicsExample,然后让它们落下,TouchDragExample可以让你在屏幕上拖动一个精灵。我剩下的是在最后一个精灵被创建的精灵,并立即被移动到触摸屏幕的位置。然后每个精灵都有自己的“引力场”或其他东西。它们不会掉落,而是尽可能远离彼此。此外,在创建纹理区域时,我将在图像(0,0)处留下图像。如何将图像加载到纹理区域,但不将其显示在屏幕上?我的代码如下:

public class TestGFXActivity extends SimpleBaseGameActivity implements IAccelerationListener, IOnSceneTouchListener{
    /** Called when the activity is first created. */
    private Camera camera;
    private static final int Camera_Width = 512;
    private static final int Camera_Height = 512;
    int centerX, centerY;


    private BitmapTextureAtlas backgroundTextureAtlas;
    private ITextureRegion backgroundTextureRegion, circleTR;
    private PhysicsWorld mPhysicsWorld;
    private Scene scene;
    private static final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

    @Override
    public EngineOptions onCreateEngineOptions() {
        // TODO Auto-generated method stub
        camera = new Camera(0,0,Camera_Width,Camera_Height);
        EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED,
                new FillResolutionPolicy(), camera);

        return engineOptions;
    }

    @Override
    protected void onCreateResources() {
        // TODO Auto-generated method stub
        backgroundTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(),
                512,512,TextureOptions.DEFAULT);
        backgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "bluelavabackground2.png",0,0);
        circleTR = BitmapTextureAtlasTextureRegionFactory.createFromAsset(backgroundTextureAtlas, this, "bubble2.png",0,0);
        backgroundTextureAtlas.load();
    }

    @Override
    protected Scene onCreateScene() {
        this.mEngine.registerUpdateHandler(new FPSLogger());
        centerX = (int) ((Camera_Width - this.backgroundTextureRegion.getWidth())/2);
        centerY = (int) ((Camera_Height - this.backgroundTextureRegion.getHeight())/2);

        scene = new Scene();
        this.scene.setOnSceneTouchListener((IOnSceneTouchListener) this);
        scene.setBackground(new Background(1,1,1));
        this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false); //Sets x and y acceleration 

        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_Height, 2, vertexBufferObjectManager);
        final Rectangle left = new Rectangle(0, 0, 2, Camera_Height, vertexBufferObjectManager);
        final Rectangle right = new Rectangle(Camera_Height - 2, 0, 2, Camera_Width, 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.scene.attachChild(ground);
        this.scene.attachChild(roof);
        this.scene.attachChild(left);
        this.scene.attachChild(right);

        this.scene.registerUpdateHandler(this.mPhysicsWorld);




        final Sprite background = new Sprite(centerX,centerY,this.backgroundTextureRegion, getVertexBufferObjectManager());
        scene.attachChild(background);
        return scene;
    }

    @Override
    public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
        final Sprite face;
        final Body body;
        if(this.mPhysicsWorld != null) {
            if(pSceneTouchEvent.isActionDown()) {
                face = new Sprite(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), this.circleTR, this.getVertexBufferObjectManager()) {
                    @Override
                    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                        this.setPosition(pSceneTouchEvent.getX() - this.getWidth() / 2, pSceneTouchEvent.getY() - this.getHeight() / 2);
                        return true;
                    }
                };
                body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

                face.setScale((float) .25);
                scene.attachChild(face);
                scene.registerTouchArea(face);
                scene.setTouchAreaBindingOnActionDownEnabled(true);
                this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
                return true;
            }else if(pSceneTouchEvent.isActionDown()){
                scene.setTouchAreaBindingOnActionDownEnabled(false);
            }
        }
        return false;
    }

    @Override
    public void onAccelerationAccuracyChanged(AccelerationData pAccelerationData) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onAccelerationChanged(AccelerationData pAccelerationData) {
        final Vector2 gravity = Vector2Pool.obtain(pAccelerationData.getX(), pAccelerationData.getY());
        this.mPhysicsWorld.setGravity(gravity);
        Vector2Pool.recycle(gravity);
    }

    public void onResumeGame() {
        super.onResumeGame();

        this.enableAccelerationSensor(this);
    }

    public void onPauseGame() {
        super.onPauseGame();

        this.disableAccelerationSensor();
    }
}

1 个答案:

答案 0 :(得分:1)

尝试使用onClick(not in onSceneTouchEvent)注册触摸区域。要执行此操作,请执行onClickLisener。为了让你的精灵掉落你可以轻松使用PhysicsBox(只需设置重力)。或者,您可以选择创建PhysicsHandler并设置速度。