更新主方法中的变量?
我正在用java制作一个汽车游戏。道路的速度是在主方法中设置的,但我需要更新速度(汽车出界并被草减慢)。
这是我想要控制速度的 int 发生变化的部分。
if(collision[12] > x)
{
roadSpeed = 150;
System.out.println("outside");
}
else
{
System.out.println("inside");
roadSpeed = 100;
}
这是到目前为止我需要更新的主要方法。
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
final TestRoad road = new TestRoad();
road.timer.setDelay(roadSpeed);
int 更改后,main 方法不会更新计时器延迟。 我看过changelisteners,但我不需要任何按钮或滑块。
如果 int 被声明为
public Boolean start = true;
public int i;
public int x;
public int y;
public int z;
public static int roadSpeed = 100; //<<<<<<<<<<declared here
public int lcolPoint = 0;
public int rcolPoint = 0;
public int colSlot = 0;
public int colOffset = 0;
public int carY;
public int[] collision;
public int[] colBuffer;
private BufferedImage carImg;
private Boolean right=false;
private Boolean left=false;
private Boolean first=true;
public TestRoad(){
this.setFocusable(true);
addKeyListener(this); //THIS IS ADDING THE KEYLISTENER
Color colors = new Color(51,102,0);
setBackground(colors);
//uses setPreferredSize instead of setSize because parent component utilizes a layout manager.
setPreferredSize(new Dimension(500, 500));
collision = new int[500];
colBuffer = new int[2];
for(int i=0; i<500; i++){
collision[i] = 0;
}
timer = new javax.swing.Timer(25, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addPoint();
carMove(); //<<<<< called here
repaint();
}
});
carmove -
public void carMove()
{
if(right==true)
x+=8;
if(left==true)
x-=8;
colBuffer[0] = collision[1];
collision[1] = lcolPoint;
collision[colSlot] = lcolPoint;
for(int z=2; z<21; z++){
colBuffer[1] = collision[z];
collision[z] = colBuffer[0];
colBuffer[0] = colBuffer[1];
}
if(collision[12] > x)
{
roadSpeed = 100;
System.out.println("outside");
}
else
{
System.out.println("inside");
roadSpeed = 50;
}
}
I am making a car game in java. The speed of the road is set in the main method, but I need to update the speed (the car going out of bounds and being slowed by grass).
Here is the part were the int that I want to control the speed is changed at.
if(collision[12] > x)
{
roadSpeed = 150;
System.out.println("outside");
}
else
{
System.out.println("inside");
roadSpeed = 100;
}
Here is the main method up to the point that I need to update.
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable() {
public void run() {
final TestRoad road = new TestRoad();
road.timer.setDelay(roadSpeed);
After the int is changed the main method does not update the timer delay.
I have looked at changelisteners but I don't want any buttons or sliders.
were the int is declared in were it is called
public Boolean start = true;
public int i;
public int x;
public int y;
public int z;
public static int roadSpeed = 100; //<<<<<<<<<<declared here
public int lcolPoint = 0;
public int rcolPoint = 0;
public int colSlot = 0;
public int colOffset = 0;
public int carY;
public int[] collision;
public int[] colBuffer;
private BufferedImage carImg;
private Boolean right=false;
private Boolean left=false;
private Boolean first=true;
public TestRoad(){
this.setFocusable(true);
addKeyListener(this); //THIS IS ADDING THE KEYLISTENER
Color colors = new Color(51,102,0);
setBackground(colors);
//uses setPreferredSize instead of setSize because parent component utilizes a layout manager.
setPreferredSize(new Dimension(500, 500));
collision = new int[500];
colBuffer = new int[2];
for(int i=0; i<500; i++){
collision[i] = 0;
}
timer = new javax.swing.Timer(25, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
addPoint();
carMove(); //<<<<< called here
repaint();
}
});
carmove -
public void carMove()
{
if(right==true)
x+=8;
if(left==true)
x-=8;
colBuffer[0] = collision[1];
collision[1] = lcolPoint;
collision[colSlot] = lcolPoint;
for(int z=2; z<21; z++){
colBuffer[1] = collision[z];
collision[z] = colBuffer[0];
colBuffer[0] = colBuffer[1];
}
if(collision[12] > x)
{
roadSpeed = 100;
System.out.println("outside");
}
else
{
System.out.println("inside");
roadSpeed = 50;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
菲利普,如果我们能看到你如何调用那段代码,那将会有很大帮助。事实上,我们看不到 RoadSpeed 的声明位置,也没有任何关于其范围的线索。
但是,假设它是这样的
,例如,在这段代码中,您认为您正在设置
roadSpeed
,但它是内部 < code>roadSpeed,而不是成员变量。因此,如果这没有帮助,您将必须添加更多提示。
Phillip, it would help a whole lot if we could see how you're calling that piece of code. As it is, we can't see where
roadSpeed
is declared nor have we any clue to its scope.But, let's say it was something like this
In this code, for example, you think you're setting
roadSpeed
, but it's the innerroadSpeed
, not the member variable.So if this doesn't help, you're going to have to include further hints.