2014-04-29 7 views
0
나는 게임 내 배경이 모든 시간을 이동해야

.. 내가 'Y'에 변수를 추가 일부 스레드를 사용할 필요가 있음을 알고스크롤 배경, JAVA

이미지의 좌표

내가 뭔가를 시도했지만이 ... 모든 배경이 이유를 이해하지 못할 어떤 이유에 줄무늬 얻을 이동하기 시작

사진 : streaking background

public class Background { 
private BufferedImage image; 
..... 
.... 

public Background(int x, int y) { 
    this.x = x; 
    this.y = y; 

    // Try to open the image file background.png 
    try { 
     BufferedImageLoader loader = new BufferedImageLoader(); 
     image = loader.loadImage("/backSpace.png"); 

    } 
    catch (Exception e) { System.out.println(e); } 

} 

/** 
* Method that draws the image onto the Graphics object passed 
* @param window 
*/ 
public void draw(Graphics window) { 

    // Draw the image onto the Graphics reference 
    window.drawImage(image, getX(), getY(), image.getWidth(), image.getHeight(), null); 

    // Move the x position left for next time 
    this.y +=1 ; 
} 


public class ScrollingBackground extends Canvas implements Runnable { 

// Two copies of the background image to scroll 
private Background backOne; 
private Background backTwo; 

private BufferedImage back; 

public ScrollingBackground() { 
    backOne = new Background(); 
    backTwo = new Background(backOne.getImageWidth(), 0); 

    new Thread(this).start(); 
    setVisible(true); 
} 

@Override 
public void run() { 
    try { 
     while (true) { 
      Thread.currentThread().sleep(5); 
      repaint(); 
     } 
    } 
    catch (Exception e) {} 
} 

@Override 
public void update(Graphics window) { 
    paint(window); 
} 

public void paint(Graphics window) { 
    Graphics2D twoD = (Graphics2D)window; 

    if (back == null) 
     back = (BufferedImage)(createImage(getWidth(), getHeight())); 

    Graphics buffer = back.createGraphics(); 

    backOne.draw(buffer); 
    backTwo.draw(buffer); 

    twoD.drawImage(back, null, 0, 0); 

} 
+0

내부 사용. 둘째, 그래픽 영역이 지워지지 않기 때문에 줄무늬가 보이는 이유는 수동으로 다시 칠하기를 호출하면 캔버스가 지워지지 않기 때문입니다. 대신 update()를 사용해야합니다. –

+0

을 '줄무늬'할 뜻 .. 당신은 아마 (칠 호출해서는 안 모든 – user2922456

+0

우선)가 그이 정말로 무엇을 의미

float scrollFactor = 0.5f; //Moves the background 0.5 pixels up per call of the draw method public void draw(Graphics window) { window.drawImage(image, getX(), getY(), image.getWidth(), image.getHeight(), null); this.y += scrollFactor; } 

답변

1

당신을 D 이 경우 새 스레드를 필요로하며,이 경우에는 작업을 복잡하게 만듭니다. 계속해서 배경의 Y 좌표에 요인을 계속 추가해야합니다. 예를 들어 :

당신은 내가 질문을 편집하고 상황의 그림을 추가
+0

제 2 계급이 필요 없다고 말씀하시는 겁니까? – user2922456

+0

Background 클래스의 두 번째 스레드가 필요하지 않습니다. 기본 게임 스레드에서 백그라운드 클래스의 paint 메서드를 호출하기 만하면됩니다. –

+0

@ user2922456 예, 정확하게. – 1337