0

如何让精灵在被触摸时做某事(具体来说,我希望它的大小增加。)我试过:

private void createSpriteOnTap(float x, float y)
    {
        final Sprite yourSprite = new Sprite(x, y, textureRegionForYourSprite, getVertexBufferObjectManager())
        {           
            @Override
            public boolean onAreaTouched(TouchEvent pSceneTouchEvent, float pTouchAreaLocalX, float pTouchAreaLocalY) 
            {
                //if(pSceneTouchEvent.isActionDown()){
                    if (pSceneTouchEvent.isActionMove())
                    {
                        // while player is touching our sprite
                        setScale(scale += 0.1);
                        setPosition(pSceneTouchEvent.getX() - getWidth() / 2, pSceneTouchEvent.getY() - getHeight() / 2);
                    }

                    if(pSceneTouchEvent.isActionDown())
                            setScale(scale += 0.1);
                return true; 
            }
        };

但是精灵只有在移动或被点击时才会增加大小,而不是被握住。在用户抬起手指之前,如何使其尺寸增加?

4

3 回答 3

1

好吧,如果您在精灵区域内捕获 mousedown 事件,您可以使用 CountDownTimer 例如开始放大精灵。鼠标向上将停止计时器。请参阅:http: //developer.android.com/reference/android/os/CountDownTimer.html

所以在这里你会在鼠标按下时启动它。

CountDownTimer spriteScaleTimer = new CountDownTimer(30000, 1000) {

 public void onTick(long millisUntilFinished) {
     mTextField.setText("seconds remaining: " + millisUntilFinished / 1000);
     // put scaling here
 }

 public void onFinish() {
     mTextField.setText("done!");
     // Max scale size.
 }
 }.start();

然后在鼠标上:

spriteScaleTimer.cancel();
于 2012-06-20T00:12:45.330 回答
1

我已经这样做了 -

@Override
public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
        float pTouchAreaLocalX, float pTouchAreaLocalY) {

    if(pSceneTouchEvent.isActionDown()){
        flagTouch=true;
    }
    if(pSceneTouchEvent.isActionMove()) {
        flagTouch=false;
        setScale(1f);
    }
    if (pSceneTouchEvent.isActionUp()) {
        flagTouch=false;
        setScale(1f);   
    }
    return true;
}

@Override
protected void onManagedUpdate(float pSecondsElapsed) {
     if(flagTouch){
         setScale(1.5f);
     }
}
于 2013-03-13T08:45:00.217 回答
0

我认为以下代码对您有用,因为我已经对其进行了测试并且对我有用。

final Sprite sprite = new Sprite(centerX, centerY, mFaceTextureRegion) {

        @Override
        public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
                float pTouchAreaLocalX, float pTouchAreaLocalY) {

            if(pSceneTouchEvent.isActionDown())
                flagTouch = true;

            if(pSceneTouchEvent.isActionUp())
                flagTouch = false;

            setPosition(pSceneTouchEvent.getX() - getWidth() / 2,
                    pSceneTouchEvent.getY() - getHeight() / 2);
            return true;
        }

        @Override
        protected void onManagedUpdate(float pSecondsElapsed) {
            super.onManagedUpdate(pSecondsElapsed);

            if(flagTouch)
                setScale(scale+=0.1f);
        }
    };
于 2012-06-23T05:40:42.037 回答