AndEngine - scroll sprites up and down

て烟熏妆下的殇ゞ 提交于 2019-12-06 13:23:16
         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.

Hope you are trying to make a scroll bar kind off movement with sprite. Please use this code

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

Hope this helps thanks.

If you want to move it only on Y coordinate you first have to override onAreaTouched and then handle the Y movement while supressing the X movement. Try the following code:

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

Oh, it will work fine if you are using GLES2-AnchorCenter branch!

Hope it helps! :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!