I have written an app that basically shows a bouncing ball on the screen. I want to add a SeekBar that increases the velocity of the ball when I change its value. I made functions in my Ball class to get and set the x and y velocities.
package perseus.gfx.test;
import everything;
public class GfxActivity extends Activity implements OnSeekBarChangeListener {
ViewGroup.LayoutParams vg = new ViewGroup.LayoutParams(-1, -1);
double velx, vely;
double x, y;
double finx, finy;
SeekBar velo;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Ball ball = new Ball(this);
setContentView(R.layout.main);
addContentView(ball, vg);
tv = (TextView)findViewById(R.id.test);
velo = (SeekBar)findViewById(R.id.vel);
velo.setOnSeekBarChangeListener(this);
x = ball.getSpeedX();
y = ball.getSpeedY();
ball.setSpeedX(finx);
ball.setSpeedY(finy);
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
finx = x + arg1;
finy = y +arg1;
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
//nothing
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
//nothing
}
}
The problem is, (I think), only the local values of fin1 and fin2 are affected, and so the ball doesn't move at all. How do I pass the values of fin1 and fin2 back to onCreate()?
You don't need to pass them back to onCreate (not really possible, anyhow), you should just need to set the ball's speed again at the end of onProgresschanged()
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
finx = x + arg1;
finy = y +arg1;
ball.setSpeedX(finx);
ball.setSpeedY(finy);
}