如何让sprite在被触摸时做某事(具体来说,我希望它的大小增加。)我试过了:
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;
}
};
但精灵只有在移动或被轻敲时才会增大,而不会被持有。在用户抬起手指之前,如何使其尺寸增大?
答案 0 :(得分:1)
如果你在sprite区域内捕获一个mousedown事件,你可以使用CountDownTimer来开始放大sprite。鼠标向上会停止计时器。 请参阅:http://developer.android.com/reference/android/os/CountDownTimer.html
所以在这里你可以在mousedown上开始。
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();
然后onMouseUp:
spriteScaleTimer.cancel();
答案 1 :(得分: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);
}
}
答案 2 :(得分: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);
}
};