Android:使用 SeekBar 更改球的速度

发布于 2024-12-29 01:46:57 字数 1227 浏览 1 评论 0原文

我编写了一个应用程序,基本上在屏幕上显示一个弹跳的球。我想添加一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2025-01-05 01:46:57

您不需要将它们传递回 onCreate (无论如何,实际上不可能),您应该只需要在 onProgresschanged() 结束时再次设置球的速度

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {       

  finx = x + arg1;
  finy = y +arg1;

  ball.setSpeedX(finx);
  ball.setSpeedY(finy);
}

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);
}
壹場煙雨 2025-01-05 01:46:57
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {       

  finx = x + arg1;
  finy = y +arg1;

  ball.setSpeedX(finx);
  ball.setSpeedY(finy);
  ball.invalidate();
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {       

  finx = x + arg1;
  finy = y +arg1;

  ball.setSpeedX(finx);
  ball.setSpeedY(finy);
  ball.invalidate();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文