2014-02-10 40 views
0

當我點擊球時,這段代碼給球在屏幕上移動所有方向。我的要求是,當我點擊暫停按鈕時,我想暫停球?對於那個我需要添加按鈕的地方以及暫停球的代碼是什麼?當按鈕點擊時,我該如何暫停球

公共類MainActivity擴展活動{

static TextView editText; 
static int score=0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main);  
    editText=(TextView) findViewById(R.id.editText1); 
    //editText.setText(""+score); 
    Button button=(Button) findViewById(R.id.button1); 
    button.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      new AnimatedView(MainActivity.this).pauseClicked(); 
     } 
    }); 
} 
public static void setCount(int count) { 

    //new MainActivity().onCreate(null); 
    score=count; 
    editText.setText(""+score); 
} 

}

Animatedview.java

public class AnimatedView extends ImageView{ 


static int count=0; 
private Context mContext; 
int x = 130; 
int y = 470; 
private float a,b; 
private int xVelocity = 8; 
private int yVelocity = 8; 
private Handler h; 
private final int FRAME_RATE = 25; 
BitmapDrawable ball; 
boolean touching; 
boolean dm_touched = false; 
float move=3; 
int bm_x = 0, bm_y = 0, bm_offsetx, bm_offsety,bm_w,bm_h; 
boolean paused; 
private Paint line, ball1, background; 
static int click=0; 
public AnimatedView(Context context) { 
    super(context); 
} 
public AnimatedView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    mContext = context; 
    h = new Handler(); 
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 
    int bgColor = getResources().getColor(R.color.game_bg); 
    String bgColorStr = prefs.getString("bgColor",""); 
    if(bgColorStr.length() > 0) { 
     bgColor = Color.parseColor(bgColorStr); 
    } 
    int lineColor = getResources().getColor(R.color.line); 
    String lineColorStr = prefs.getString("wallColor",""); 
    if(lineColorStr.length() > 0) { 
     lineColor = Color.parseColor(lineColorStr); 
    } 
    int ballColor = getResources().getColor(R.color.position); 
    String ballColorStr = prefs.getString("ballColor",""); 
    if(ballColorStr.length() > 0) { 
     ballColor = Color.parseColor(ballColorStr); 
    } 

    line = new Paint(); 
    line.setColor(lineColor); 
    ball1 = new Paint(); 
    ball1.setColor(ballColor); 
    background = new Paint(); 
    background.setColor(bgColor); 
    setFocusable(true); 
    this.setFocusableInTouchMode(true); 
} 


@Override 
public void buildDrawingCache() { 
    // TODO Auto-generated method stub 
    super.buildDrawingCache(); 
} 
private Runnable r = new Runnable() { 
    @Override 
    public void run() { 
     //Log.e("game","run called"); 
     if(touching = true) 
     invalidate(); 
    } 
}; 


@Override 
protected void onDraw(Canvas c) { 
    //Log.e("game","ondraw called"); 
    //int z= c.getHeight()/2; 
    //Log.e("game","z is"+z); 

    BitmapDrawable ball = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ball); 
    if (x<0 && y <0) { 
     //x = this.getWidth()/2; 
     y = c.getHeight()/2; 


    } else { 
     x += xVelocity; 
     y += yVelocity; 
     if ((x > this.getWidth() - ball.getBitmap().getWidth()) || (x < 0)) { 
      xVelocity = xVelocity*-1; 
     } 
     if (y >(this.getHeight() - ball.getBitmap().getHeight()) ||y <0) { 
      yVelocity = yVelocity*-1; 
     } 
    } 
    c.drawBitmap(ball.getBitmap(), x, y, null); 
     if(touching){ 
     // Log.e("game","iftouch called called"); 
     h.postDelayed(r, FRAME_RATE);  
     bm_w=ball.getBitmap().getWidth(); 
     bm_h=ball.getBitmap().getHeight(); 
     } 
    } 

public void pauseClicked() 
{ touching=true; 
    Log.d("s","pause called"); 
    if (touching) { 
     // paused = true; 
     Log.d("s","touhcing called"); 
     h.removeCallbacks(r); 
     touching = false; 
    } else { 
     touching = true; 
    } 
} 
@Override 
public boolean onTouchEvent(MotionEvent event) { 
    // Log.d("game","ontouch called"); 
    int touchType = event.getAction(); 

    switch(touchType){ 
     case MotionEvent.ACTION_MOVE: 
      a = event.getX(); 
      b = event.getY(); 
      touching = true; 

      /* if (dm_touched) { 
       x = (int) a - bm_offsetx; 
       y = (int) b - bm_offsety; 
      }*/ 
      //invalidate(); 
      break; 

     case MotionEvent.ACTION_DOWN: 
      //x and y give you your touch coordinates 
       a = event.getX(); 
       b = event.getY(); 
       touching = true; 
      /* if (touching) { 
        // paused = true; 
        h.removeCallbacks(r); 
        touching = false; 
       } else { 
        touching = true; 
       }*/ 
       //Log.d("game","action_down called"); 
       Log.e("s",""+ a); 
       Log.e("s",""+ b); 
       Log.e("s",""+ x); 
       Log.e("s",""+ y); 
       Log.e("s",""+ bm_w); 
       Log.e("s",""+ bm_h); 
       if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
        count++; 
       MainActivity.setCount(count); 
        //invalidate(); 
        Log.i("score",""+count); 

       } 
       if (dm_touched) { 
        if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
        move+=2; 
        //x = (int) a - bm_offsetx; 
        y = (int) (yVelocity*-1); 

        //invalidate(); 

       }} 
       // dm_touched = true; 
     case MotionEvent.ACTION_UP: 
      a = event.getX(); 
       b = event.getY(); 
      if(a>x+20&&a<330&&b<=y+320&&b>y){ 
       click++; 

      invalidate(); 
      touching=true;} 
      if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
       Log.e("game","clicked"); 
      } 

      default: 

       dm_touched = true; 
      } 

      return true; 
} 

@Override 
public void destroyDrawingCache() { 

    count=0; 
    super.destroyDrawingCache(); 
} 

}

+0

我想這能幫助你 http://stackoverflow.com/questions/15776645/thread-isnt-pausing-when-pause-button-is-clicked –

回答

1

您可以暫停球的時候用戶在屏幕上,如果再碰它適合您的要求。 對於您可以在您的MotionEvent.ACTION_DOWN事件實現這樣的:

case MotionEvent.ACTION_DOWN: 
       // x and y give you your touch coordinates 
       a = event.getX(); 
       b = event.getY(); 
       if (touching) { 
        // paused = true; 
        h.removeCallbacks(r); 
        touching = false; 
       } else { 
        touching = true; 
       } 
       Log.e("s", "" + a); 
       Log.e("s", "" + b); 
       Log.e("s", "" + x); 
       Log.e("s", "" + y); 
       Log.e("s", "" + bm_w); 
       Log.e("s", "" + bm_h); 
       if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
        count++; 

        Log.i("score", "" + count); 

       } 
       if (dm_touched) { 
        if ((a > x) && (a < bm_w + x) && (b > y) && (b < bm_h + y)) { 
         move += 2; 

         y = yVelocity * -1; 

        } 
       } 

所以,每當屏幕上的球用戶觸摸下一次將被暫停there.And從Action.MOVE事件刪除touching = true;

希望它能幫助你。

+0

出色的工作,感謝üandim,確定圖像時停止用戶再次觸摸屏幕上相同的代碼我怎樣才能使用按鈕 – kumar

+0

你可以把按鈕放在屏幕上的任何地方。觸摸作爲一個全局變量和onClick的按鈕,你可以把條件從Handler中刪除回調。把必要的變量作爲全局變量。 – AndiM

+0

我做了同樣的事情,你說什麼,我在我的mainactivity.java添加按鈕頂部的屏幕,並在動畫視圖中寫入pauseclicked方法,從Handler中刪除回調的條件。但是當我點擊暫停按鈕系統崩潰時,我更新了總代碼在上面的問題,請檢查它,並非常感謝你的迴應 – kumar

相關問題