Android:使用 SeekBar 更改球的速度
我编写了一个应用程序,基本上在屏幕上显示一个弹跳的球。我想添加一个 SeekBar,当我更改其值时,它会增加球的速度。我在 Ball 类中创建了函数来获取和设置 x 和 y 速度。
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
}
}
问题是,(我认为),只有 fin1 和 fin2 的局部值受到影响,因此球根本不移动。 如何将 fin1 和 fin2 的值传递回 onCreate()?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要将它们传递回 onCreate (无论如何,实际上不可能),您应该只需要在 onProgresschanged() 结束时再次设置球的速度
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()