Andengine触摸事件不起作用

时间:2014-03-29 15:58:02

标签: android andengine

我尝试使用Andengine开发一款简单的Android游戏。但我无法找到如何编码触摸事件部分。我想将汽车的图像向右和向左移动。我到处寻找,但无法找到答案。

这是我的代码

private void loadGfx() {
        // TODO Auto-generated method stub

        BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/"); // give the path of image folder
        bitmapTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 256, 256);// create a bit map to hold the picture and give size according to the image
        iTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(bitmapTextureAtlas, this, "usercar.png", 0,0);
        bitmapTextureAtlas.load();

    }

* 这个方法我已经加载了汽车图像,我在onCreateResources方法中调用了这个方法

** 我尝试了这段代码,但没有为我工作

public void onPopulateScene(Scene pScene,
            OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
        // TODO Auto-generated method stub

        //now set the x,y coordination of the image to display the right position we want
        sPlayer = new Sprite(500,350,iTextureRegion,this.mEngine.getVertexBufferObjectManager()){ // user's car x,y


            // touch event for user's car
            public boolean onAreaTouched(org.andengine.input.touch.TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) 
            {
                this.setPosition(this.getX() + 10 , this.getY() + 10);

                return true;
            };


        };
        //touch  ----------------------------------------------------------
        scene.setTouchAreaBindingOnActionDownEnabled(true);
        scene.registerTouchArea(sPlayer);
        //-----------------------------------------------------------------


        this.scene.attachChild(sPlayer);

        final FixtureDef PLAYER_FIX = PhysicsFactory.createFixtureDef(10.0f, 1.0f, 0.0f);
        Body body = PhysicsFactory.createBoxBody(pWorld, sPlayer, BodyType.DynamicBody, PLAYER_FIX);
        pWorld.registerPhysicsConnector(new PhysicsConnector(sPlayer,body,true,false));



        pOnPopulateSceneCallback.onPopulateSceneFinished();

    }

然而,我想要做的是,根据用户的触摸移动汽车图像。用户应该能够将汽车图像向左和向右拖动。有人可以帮助我!

1 个答案:

答案 0 :(得分:0)

我无法发表评论,这就是我发帖的原因。 试着评论这段代码

 scene.setTouchAreaBindingOnActionDownEnabled(true);

希望它对你有用。

相关问题