2013-06-25 66 views
0

我需要製作一個系統,在屏幕上繪製一個障礙物,當它滾動屏幕時,另一個將以隨機值(達到某個值的最大值)繪製在屏幕上。如何在第一個對象離屏時繪製另一個對象?

我這裏有一些代碼:

public int xElapsed = 0; 

這僅僅是增加的時候,是多麼的玩家移動。

obstacleHole.paint(g); 

     if(obstacleHole.getX() <= 0){ 
      obstacleHole.paint(g); 
     } 

這是塗料的功能。第一個障礙物被直接塗上,第二個障礙物被滿足。然而,這不是按預期工作。

x = position.nextInt(500 + player.xElapsed * 2); 

這是如何設置障礙物的x座標。 「職位」是一個隨機值生成器。

此代碼不工作,因爲只有一個障礙物出現。我如何解決這個問題,以達到預期的效果?如有必要,我可以提供額外的代碼。

這裏是ObstacleHole類:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.util.Random; 

public class ObstacleHole { 

Player player = new Player(); 
Random position = new Random(); 

int x; 
int y; 
int dx = 1; 
int width = 100; 
int height = 100; 

public ObstacleHole(){ 
    x = position.nextInt(500 + player.xElapsed * 2); 
    y = 250; 
} 

public void move(){ 
    x = x - player.playerSpeed; 
} 

public void paint(Graphics g){ 
    g.setColor(Color.BLACK); 
    g.fillRect(x, y, width, height); 
} 

public Rectangle bounds(){ 
    return (new Rectangle(x, y, width, height)); 
} 

public int getX() { 
    return x; 
} 
} 

Screen.java

import java.awt.Graphics; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 

import javax.swing.JPanel; 
import javax.swing.Timer; 

    public class Screen extends JPanel implements ActionListener, KeyListener{ 

Player player = new Player(); 
ObstacleHole obstacleHole = new ObstacleHole(); 

public Screen(){ 
    addKeyListener(this); 
    setDoubleBuffered(true); 
    setFocusable(true); 
    Timer tick = new Timer(5, this); 
    tick.start(); 
} 

public void actionPerformed(ActionEvent e) { 
    repaint(); 
    player.move(); 
    obstacleHole.move(); 
    System.out.println(player.getXElapsed()); 
} 

public void paint(Graphics g){ 
    super.paint(g); 
     player.paint(g); 
     obstacleHole.paint(g); 

     if(obstacleHole.getX() <= 0){ 
      obstacleHole.paint(g); 
     } 
} 

public void keyPressed(KeyEvent e) { 
    int key = e.getKeyCode(); 

    if(player.jumpReady){ 
     if(key == KeyEvent.VK_UP){ 
      player.dy = -1; 
      player.jumpReady = false; 
     } 
    } 
} 

public void keyReleased(KeyEvent e) { 

} 

public void keyTyped(KeyEvent e) { 

} 


} 

Player.java

import java.awt.Color; 
import java.awt.Graphics; 

public class Player { 

int x; 
int y; 
int dx; 
public int xElapsed = 0; 
public int dy; 
int width = 64; 
int height = 64; 
public int playerSpeed = 3; 
public boolean isMoving = true; 
public boolean hasJumped = false; 
public boolean jumpReady = true; 

public Player(){ 
    x = 150; 
    y = 250; 
} 

public void move(){ 
    x = x + dx; 
    y = y + dy; 

    xElapsed++; 

    if(hasJumped == true){ 
     dy = -1; 
    } 

    if(y == 150){ 
     dy = 1; 
    } 

    if(y == 250){ 
     dy = 0; 
     jumpReady = true; 
    } 
} 

public void paint(Graphics g){ 
    g.setColor(Color.RED); 
    g.fillRect(x, y, width, height); 
} 

public int getX(){ 
    return x; 
} 

public int getY(){ 
    return y; 
} 

public int getXElapsed(){ 
    return xElapsed; 
} 
} 
+0

更多代碼請:D和屏幕截圖當然會有所幫助 – 2013-06-25 21:11:10

+0

說實話,我沒有更多的補充,因爲這幾乎是所有相關的代碼,您希望具體看到什麼?也許我可以提供一些相關的東西。 –

+0

我們可能需要看到足夠的代碼才能理解問題。如果編譯的更好,最好是非常小的,編譯,運行並向我們顯示問題。 –

回答

3

在你的代碼是畫obstacleHole,那麼當x值爲障礙洞小於或等於0,則再次繪製它。你所做的只是向同一個對象的paint()方法發送兩個調用。

如果你想畫第二個,你需要創建另一個對象。或者,也可以在離開後將原始對象移回屏幕上。

這是很難給你當你提供這麼少的情況下示例代碼,但嘗試這樣的事:

MyObject obstacleHoleA = new MyObject(); 
MyObject obstacleHoleB = new MyObject(); 

obstacleHoleA.paint(g); 

if(obstacleHoleA.getX() <= 0){ 
    obstacleHoleB.paint(g); 
} 

或者這樣:

obstacleHole.paint(g); 

if(obstacleHole.getX() <= 0){ 
    obstacleHole.setX(randomValueUpToAMaxOfCertainValue); 
} 

編輯:有很多我會對上面的代碼做很多不同的事情,但它們不在問題的範圍之內。

試試這個你ObstacleHole類:

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.util.Random; 

public class ObstacleHole { 

Player player = new Player(); 
Random position = new Random(); 

int x; 
int y; 
int dx = 1; 
int width = 100; 
int height = 100; 

public ObstacleHole(){ 
    x = getNewPosition(); 
    y = 250; 
} 

public void move(){ 
    x = x - player.playerSpeed; 

    if(x < 0 - width) { 
     x = getNewPosition(); 
    } 
} 

public void paint(Graphics g){ 
    g.setColor(Color.BLACK); 
    g.fillRect(x, y, width, height); 
} 

public Rectangle bounds(){ 
    return (new Rectangle(x, y, width, height)); 
} 

public int getX() { 
    return x; 
} 

private int getNewPosition() { 
    return 200 + position.nextInt(300); 
} 
} 

注意改變構造函數和move()方法,用新的方法getNewPosition沿()。

+0

感謝您的幫助!查看我更新的答案以獲取更多代碼。 –

+0

請參閱我編輯的回覆,瞭解可能對您有所幫助的一些更改。 – MaxAlexander

+0

感謝您的幫助! –

相關問題