【问题标题】:OnTouchEvent update value Issue AndroidOnTouchEvent 更新值问题 Android
【发布时间】:2015-10-26 03:13:40
【问题描述】:

朋友们好,
我做了一个游戏,你需要按下一个球,球会增加他的大小。 现在我的问题是,当我调用 onTouchEvent 并调用函数 increaseRadius() 时,它会在手指在屏幕上移动时更新我的​​值,但是如果我不移动手指就将其更新一次。 我希望即使玩家将手指放在同一坐标中,该值也会更新(就像更新的 while 循环一样)。 onTouchListener 只有在手指移动时才能正常工作,否则只能工作一次。

这里是“短”代码:

public boolean onTouchEvent(MotionEvent event)
{
    if(userball.getRadius()<screenWidth/4)
    {
       userball.increaseRadius();
    }
}
public void increaseRadius()
{
    this.radius+=screenWidth*0.002;
}

那些是没有与问题无关的东西的功能。 我将如何改变它,即使球员不移动他的手指,球也会更新并增加他的大小?

我希望玩家的手指在屏幕上(如 while 循环)在同一坐标上的整个时间都会更新该值。

【问题讨论】:

  • 嘿,伙计,感谢您的快速响应,我希望玩家的手指在屏幕上的整个时间都会更新该值(如 while 循环),但如果玩家将手指放在屏幕上屏幕并且不改变任何坐标值更新一次。我不认为 MotionEvent.ACTION_* 是问题。
  • 啊,明白了。我的错。我以为你的意思是它没有在 ACTION_DOWN 上更新。

标签: android geometry touch-event


【解决方案1】:

使用 if 语句检查玩家是否只放手指:

if (event.getAction() == MotionEvent.ACTION_DOWN) {

}

同样,如果你想监听拖动事件,请像这样编写你的 if 语句:

if (event.getAction() == MotionEvent.ACTION_MOVE) {

}

为了让进程在用户手指触摸时一直进行,您可以启动一个线程,如下例所示:

if (event.getAction() == MotionEvent.ACTION_DOWN) {
    IncreaseSizeThread increaseSizeThread = new IncreaseSizeThread();
}

if (event.getAction() == MotionEvent.ACTION_UP) {
    increaseSizeThread.stop(); 
    //A better way would be to change a variable that gets the while loop in     
    //the thread going to false e.g. keepGoing = false;

}

线程的内容可能是:

class IncreaseSizeThread extends Thread {

    public IncreaseSizeThread() {
    }

    void run(){
        while(keepGoing){
            //Size++;
        }

    }
}

【讨论】:

  • 谢谢你救了我!工作!
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-19
  • 1970-01-01
  • 1970-01-01
  • 2023-03-18
  • 2014-09-15
相关资源
最近更新 更多