Question

i try to develop a simple game with android using Andengine. but i can't find how to code the touch event part. i want to move an image of a car to the right and left. i have searched for everywhere but couldn't found the answer.

here is my code

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();

    }

* with this method i have loaded the car image and i have called this method in the onCreateResources method

** and i tried this code but didn't work for me

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();

    }

however what i want to do is, move the car image according to user's touch. user should be able to drag the car image to the left and right. could someone please help me!

Was it helpful?

Solution

I can't comment so thats why I am posting as answer. try comment this code

 scene.setTouchAreaBindingOnActionDownEnabled(true);

Hope it will work for you.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top