Reputation: 107
I am developing game in android. I want to used to value progress of Progress Bar to do force shot a arrows .And this is my code :
public class TheArrows extends Activity {
ProgressBar myProgressBar;
private int myProgress=0;
private int t=0;
@Override public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
myProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);
app.quabongchuyendong.QuaBongView quaBongView=(app.quabongchuyendong.QuaBongView) findViewById(R.id.qua_bong);
quaBongView.setOnTouchListener(new OnTouchListener() {
@Override public boolean onTouch(View view, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){ t=1; }
if(event.getAction()==MotionEvent.ACTION_MOVE){ t=1; }
if(event.getAction()==MotionEvent.ACTION_UP){ t=0; }
return false;
}
});
new Thread(myThread).start();
}
private Runnable myThread = new Runnable(){
@Override public void run() {
// TODO Auto-generated method stub
while (myProgress<100){
try{
myHandle.sendMessage(myHandle.obtainMessage());
Thread.sleep(100);
} catch(Throwable t){ }
}
}
Handler myHandle = new Handler(){
@Override public void handleMessage(Message msg) {
if(t==1) myProgress++;
myProgressBar.setProgress(myProgress);
}
};
};
public int progressValue(){
return myProgress;
}
}
Although I have myProgress++;
why myProgress
in progressValue()
doesn't change? Please help me.
Upvotes: 0
Views: 339
Reputation: 1242
Try This :
@Override public boolean onTouch(View view, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){ t=1; }
else if(event.getAction()==MotionEvent.ACTION_MOVE){ t=1; }
else if(event.getAction()==MotionEvent.ACTION_UP){ t=0; }
new Thread(myThread).start();
return false;
}
});
and Remove if(t==1) condition in handler Handler
myHandle = new Handler(){
@Override public void handleMessage(Message msg) {
myProgress++;
myProgressBar.setProgress(myProgress);
}
};
};
Upvotes: 0
Reputation: 1242
new Thread(myThread).start(); Should be in OnTouch befaore return statement;
Upvotes: 0