0

im trying to make a 8 puzzle game and my problem is that when the value of x1 let say 70 of a particular image's x position is incremented until it reaches to 170 and then it will be decremented until it reaches to 70 again, but this decrementation is not working and the image is not moving to its original position anymore but it does shaking only. why is this happening. in order to clarify all this stuff you can check my code below:

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();
        }
        }

    }
}

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);
    }
}

this code is not the actual code i have right now but i posted it here to make all things clear with regards to my problem. thanks in advance

4

1 回答 1

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;       
     }

x1达到 170时会发生什么?你减去 1。下次你进来时,因为x1是 169,所以你加 1,变成 170。等等,无穷无尽

我认为您需要一个变量direction,即 1 或 -1。当您达到 17070 时,反转 的值direction。在所有情况下都使用direction改变x1

x1 += direction;
于 2013-08-08T07:47:47.240 回答