递增图像的x位置然后将其递减在java中不起作用

joh*_*els 0 java image increment decrement

我试图制作一个8益智游戏,我的问题是,当x1的值让一个特定图像的x位置的70增加直到它达到170然后它将递减直到它再次达到70,但这个减少没有工作,图像不再移动到原来的位置,但它只是颤抖.为什么会发生这种情况.为了澄清所有这些内容,您可以查看下面的代码:

run.java

public class run extends Applet implements Runnable{
    public Image im;    
    public int x1=70, y1=70;
    public int x2=170, y2=70;
    public int x3=270, y3=70;
    public int x4=70, y4=170;
    public int x5=170, y5=170;
    public int x6=270, y6=170;
    public int x7=70, y7=270;
    public int x8=170, y8=270;
    public int x9=270, y9=270;

    public Image offscreen;
    public Graphics d;
    public BufferedImage container;
    public BufferedImage[] tile = new BufferedImage[10];
    private File loadThis;

     public void combi1(){
         if(x1<170 && x1>=70){
             x1+=1; y2+=1; x5-=1; y4-=1;
             return;       
         }
         if(x1>=70){
             x1-=1; y2-=1; x5+=1; y4+=1;
             return;       
         }

     }


    public void run(){

        try{
            URI imageurl = getClass().getResource("container.png").toURI();
            loadThis = new File(imageurl);
            container = ImageIO.read(loadThis);            
            for(int i=0; i<10; i++){
                if(i>=1){
                    URI tileurl = getClass().getResource("tile" + i +".png").toURI();
                    loadThis = new File(tileurl);
                    tile[i] = ImageIO.read(loadThis);
                }
            }                     
            }catch (IOException e1){
                e1.printStackTrace();
            } catch (URISyntaxException e) {
                System.out.println(e);
            }

        while(true){                  

            combi1();

            repaint();           

        try{
            Thread.sleep(2);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

AIprojects.java

public class AIprojects extends run{
    public void init(){
        setSize(450,450); 

        Thread th = new Thread(this);
        th.start();
        offscreen = createImage(450,450);
        d = offscreen.getGraphics();
    }
    public void paint(Graphics g){
        d.setColor(Color.black);        
        d.fillRect(0, 0, 450, 450);
        d.drawImage(container, 52, 52,340,340, this);
        d.drawImage(tile[9], x9, y9, this);
        d.drawImage(tile[1], x1, y1, this);
        d.drawImage(tile[2], x2, y2, this);
        d.drawImage(tile[3], x3, y3, this);
        d.drawImage(tile[4], x4, y4, this);
        d.drawImage(tile[5], x5, y5, this);
        d.drawImage(tile[6], x6, y6, this);
        d.drawImage(tile[7], x7, y7, this);
        d.drawImage(tile[8], x8, y8, this);

        g.drawImage(offscreen, 0, 0, this);
    }
    public void update(Graphics g){
        paint(g);
    }
}
Run Code Online (Sandbox Code Playgroud)

这段代码不是我现在的实际代码,但是我在这里发布了一些关于我的问题的所有事情.提前致谢

Bri*_*new 5

特定

    if(x1<170 && x1>=70){
         x1+=1; y2+=1; x5-=1; y4-=1;
         return;       
     }
     if(x1>=70){
         x1-=1; y2-=1; x5+=1; y4+=1;
         return;       
     }
Run Code Online (Sandbox Code Playgroud)

x1点击170 时会发生什么?你减去1.下次你进来,从x1现在开始是169,你加1,使它成为170. 依旧无限.

我认为你需要一个变量direction,即1或-1.当你达到170 70时,反转的值direction.在所有情况下使用direction改变x1从而

x1 += direction;
Run Code Online (Sandbox Code Playgroud)