2016-01-24 58 views
1

我正面临一个问题。我已经写下了代码来选择白球,现在我想让我的程序等待另一个触摸在屏幕上。如何让我的程序在Android中等待TouchEvent

此行WhiteBall.ball.getPaint().setColor(Color.BLUE);

球现在我想我的程序等待另一个触摸将定义球的速度选定后,我怎样才能让我的程序等待触动?

public boolean onTouchEvent(MotionEvent e) 
{ 
    int xpos=(int) e.getX(); 
    int ypos=(int) e.getY(); 

    if(WhiteBall.velocity==0) 
     if((xpos>WhiteBall.x-30 && xpos<WhiteBall.x+30) && (ypos>WhiteBall.y-30 && ypos<WhiteBall.y+30)) { 
      WhiteBall.ball.getPaint().setColor(Color.BLUE); 


      // WhiteBall.speed(6); 

     } 

    Log.d("OnTouchPosition X",""+xpos); 
    Log.d("OnTouchPosition Y",""+ypos); 
    return true; 
} 

回答

0

在每次触摸中,根据触摸什么元素,您应设置一个变量。例如,在你的代码:

if (this.color == null) { 
    this.color = Color.BLUE; 
} 

,当其他的“东西”被感动你设定的速度:

else { 
    this.velocity = 6; 
    WhiteBall.ball.getPaint().setColor(this.color); 
    WhiteBall.ball.setSpeed(this.velocity); 
}