更新主方法中的变量?

发布于 2024-10-15 08:24:22 字数 2773 浏览 2 评论 0原文

我正在用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 技术交流群。

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

发布评论

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

评论(1

柏林苍穹下 2024-10-22 08:24:22

菲利普,如果我们能看到你如何调用那段代码,那将会有很大帮助。事实上,我们看不到 RoadSpeed 的声明位置,也没有任何关于其范围的线索。

但是,假设它是这样的

class Game {
  private int roadSpeed = 200 ; // just an initial value

  public void getSpeed(int collision, int roadSpeed){
    if(collision[12] > x){
      roadSpeed = 150;
      System.out.println("outside");
    } else {
      System.out.println("inside");
      roadSpeed = 100;
    }
    return;
  }

  public static void main(String[] args){
    // do something that sets collision
    getSpeed(collision, roadSpeed);
    // and on with your code

,例如,在这段代码中,您认为您正在设置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

class Game {
  private int roadSpeed = 200 ; // just an initial value

  public void getSpeed(int collision, int roadSpeed){
    if(collision[12] > x){
      roadSpeed = 150;
      System.out.println("outside");
    } else {
      System.out.println("inside");
      roadSpeed = 100;
    }
    return;
  }

  public static void main(String[] args){
    // do something that sets collision
    getSpeed(collision, roadSpeed);
    // and on with your code

In this code, for example, you think you're setting roadSpeed, but it's the inner roadSpeed, not the member variable.

So if this doesn't help, you're going to have to include further hints.

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