2

I am a beginner andengine user and I need your help.

I have created a class MySprite extending sprite and I want the sprite to move up and down on the y - coordinate when I slide with the finger on the Screen by touching only the sprite.

I have tried to achieve this by implementing IScrollDetectorListener and IonSceneTouchListener but the Problem is: I can touch anywhere and my sprites moves.

I would be glad if someone could help.

For more Details just comment :)

4

3 回答 3

1
         Sprite mySprite = new Sprite(x, y, textureRegion, this.getVertexBufferObjectManager()) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {

 this.setPosition(x, y);
            //Insert Code Here
            return true;
             }};

        // dont forgot to register your touch area i.e 
        mScene.registerTouchArea(mySprite);
    // Hoping it may help you.
于 2014-02-17T05:34:39.740 回答
0

如果您只想在 Y 坐标上移动它,您首先必须覆盖 onAreaTouched,然后在抑制 X 移动的同时处理 Y 移动。试试下面的代码:

Sprite mSprite = new Sprite(mX, mY, mTexture, this.mEngine.getVertexBufferObjectManager()) {                        
                    @Override
                    public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                        this.setPosition(this.getX(), pSceneTouchEvent.getY());                         
                        return true;
                    }
                };
                this.mScene.attachChild(mSprite);
                this.mScene.registerTouchArea(mSprite);

哦,如果您使用 GLES2-AnchorCenter 分支,它会正常工作!

希望能帮助到你!:)

于 2014-02-18T00:27:45.157 回答
0

希望您正在尝试使用精灵制作滚动条的运动。请使用此代码

Initial scrollbarPosition = scrollbar.getX(); //fix this position

final Sprite scrollbar= new Sprite(centerX, centerY, this.scrollbarTextureRegion, this.getVertexBufferObjectManager()) {
            @Override
            public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
                this.setPosition(scrollbarPosition, pSceneTouchEvent.getY() - this.getHeight() / 2);
                return true;
            }
        };

scene.attachChild(scrollbar);
scene.registerTouchArea(scrollbar);
scene.setTouchAreaBindingOnActionDownEnabled(true);

希望这有帮助谢谢。

于 2014-02-17T05:46:07.827 回答