2015-06-14 71 views
-1

當我按下鍵盤輸入時,我的測試遊戲中的精靈以超高的速度飛離屏幕,我不知道爲什麼。2D java遊戲中的對象(Sprite)異常移動

KeyInput.java:

package com.cosmicluck.miner.framework; 

import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 

import com.cosmicluck.miner.window.Handler; 

public class KeyInput extends KeyAdapter 
{ 
    Handler handler; 

    public KeyInput(Handler handler) 
    { 
     this.handler = handler; 
    } 

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

     for(int i = 0; i < handler.object.size(); i++) 
     { 
      GameObject tempObject = handler.object.get(i); 

      if(tempObject.getId() == ObjectId.Player) 
      { 
       if(key == KeyEvent.VK_D) tempObject.setVelX(5); 
       if(key == KeyEvent.VK_A) tempObject.setVelX(-5); 
      } 
     } 

     if(key == KeyEvent.VK_ESCAPE) 
     { 
      System.exit(1); 
     } 
    } 
    public void keyReleased(KeyEvent e) 
    { 
     int key = e.getKeyCode(); 

     for(int i = 0; i < handler.object.size(); i++) 
     { 
      GameObject tempObject = handler.object.get(i); 

      if(tempObject.getId() == ObjectId.Player) 
      { 
       if(key == KeyEvent.VK_D) tempObject.setVelX(0); 
       if(key == KeyEvent.VK_A) tempObject.setVelX(0); 
      } 
     } 
    } 
} 

改變速度什麼,但對於0使keyPressed物體飛出屏幕。

這裏是我的Player.java:

package com.cosmicluck.miner.objects; 

import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.util.LinkedList; 

import com.cosmicluc.miner.framework.GameObject; 
import com.cosmicluc.miner.framework.ObjectId; 

public class Player extends GameObject 
{ 

private float width = 48, height = 96; 

private float gravity = 0.5f; 
private final float MAX_SPEED = 10; 

public Player(float x, float y, ObjectId id) { 
    super(x, y, id); 
} 

public void tick(LinkedList<GameObject> object) { 

    x += velX; 
    y += velY; 
    if(falling || jumping) 
    { 
     velY += gravity; 

     if(velY > MAX_SPEED) 
      velY = MAX_SPEED; 
    } 
} 

public void render(Graphics g) 
{ 
    g.setColor(Color.BLUE); 
    g.fillRect((int)x, (int)y, (int)width, (int)height); 

    Graphics2D g2d = (Graphics2D) g; 
    g.setColor(Color.RED); 
    g2d.draw(getBounds()); 
    g2d.draw(getBoundsRight()); 
    g2d.draw(getBoundsLeft()); 
    g2d.draw(getBoundsTop()); 
} 

public Rectangle getBounds() { 
    return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int) ((int)y+(height/2)), (int)width/2, (int)height/2); 
} 
public Rectangle getBoundsTop() { 
    return new Rectangle((int) ((int)x+(width/2)-((width/2)/2)), (int)y, (int)width/2, (int)height/2); 
} 
public Rectangle getBoundsRight() { 
    return new Rectangle((int) ((int)x+width-5), (int)y+5, (int)5, (int)height-10); 
} 
public Rectangle getBoundsLeft() { 
    return new Rectangle((int)x, (int)y+5, (int)5, (int)height-10); 
} 

}

GameObject.java:

package com.cosmicluck.miner.framework; 

import java.awt.Graphics; 
import java.awt.Rectangle; 
import java.util.LinkedList; 

public abstract class GameObject 
{ 

protected float x, y; 
protected ObjectId id; 
protected float velX = 0, velY = 0; 
protected boolean falling = true; 
protected boolean jumping = false; 

public GameObject(float x, float y, ObjectId id) 
{ 
    this.x = x; 
    this.y = y; 
    this.id = id; 
} 

public abstract void tick(LinkedList<GameObject> object); 
public abstract void render(Graphics g); 
public abstract Rectangle getBounds(); 

public float getX() 
{ 
    return x; 
} 
public float getY() 
{ 
    return y; 
} 
public void setX(float x) 
{ 
    this.x = x; 
} 
public void setY(float y) 
{ 
    this.y = y; 
} 

public float getVelX() 
{ 
    return velX; 
} 
public float getVelY() 
{ 
    return velY; 
} 
public void setVelX(float velX) 
{ 
    this.velX = x; 
} 
public void setVelY(float velY) 
{ 
    this.velY = y; 
} 

public boolean isFalling() { 
    return falling; 
} 

public void setFalling(boolean falling) { 
    this.falling = falling; 
} 

public boolean isJumping() { 
    return jumping; 
} 

public void setJumping(boolean jumping) { 
    this.jumping = jumping; 
} 

public ObjectId getId() 
{ 
    return id; 
} 

}

Game.java:

package com.cosmicluck.miner.window; 

import java.awt.Canvas; 
import java.awt.Color; 
import java.awt.Graphics; 
import java.awt.image.BufferStrategy; 
import java.util.Random; 

import com.cosmicluc.miner.framework.KeyInput; 
import com.cosmicluc.miner.framework.ObjectId; 
import com.cosmicluc.miner.objects.Player; 

public class Game extends Canvas implements Runnable 
{ 
private static final long serialVersionUID = -905290418752159869L; 

private boolean running = false; 
private Thread thread; 

public static int WIDTH, HEIGHT; 

//Object 
Handler handler; 

Random rand = new Random(); 

private void init() 
{ 
    WIDTH = getWidth(); 
    HEIGHT = getHeight(); 
    handler = new Handler(); 

    handler.addObject(new Player(100, 100, handler, ObjectId.Player)); 

    handler.createLevel(); 

    this.addKeyListener(new KeyInput(handler)); 
} 

public synchronized void start(){ 
    if(running) 
     return; 

    running = true; 
    thread = new Thread(this); 
    thread.start(); 
} 

public void run() 
{ 
    init(); 
    this.requestFocus(); 
    long lastTime = System.nanoTime(); 
    double amountOfTicks = 60.0; 
    double ns = 1000000000/amountOfTicks; 
    double delta = 0; 
    long timer = System.currentTimeMillis(); 
    int updates = 0; 
    int frames = 0; 
    while(running){ 
     long now = System.nanoTime(); 
     delta += (now - lastTime)/ns; 
     lastTime = now; 
     while(delta >= 1){ 
      tick(); 
      updates++; 
      delta--; 
     } 
     render(); 
     frames++; 

     if(System.currentTimeMillis() - timer > 1000){ 
      timer += 1000; 
      System.out.println("FPS: " + frames + " TICKS: " + updates); 
      frames = 0; 
      updates = 0; 
     } 
    } 
} 

private void tick() 
{ 
    handler.tick(); 
} 
private void render() 
{ 
    BufferStrategy bs = this.getBufferStrategy(); 
    if(bs == null) 
    { 
     this.createBufferStrategy(3); 
     return; 
    } 

    Graphics g = bs.getDrawGraphics(); 
    //////////////////////////////////// 
    //Draw here 
    g.setColor(Color.BLACK); 
    g.fillRect(0, 0, getWidth(), getHeight()); 

    handler.render(g); 

    //////////////////////////////////// 
    g.dispose(); 
    bs.show(); 
} 

public static void main(String args[]) 
{ 
    new Window(800, 600, "Miner Madness - Prototype", new Game()); 
} 

}

謝謝你提前!

編輯:固定! 我在製作velX/velY = x,y時,在我的GameObject中出現了一個愚蠢的錯誤。謝謝大家的幫助! :)

+0

調用tick方法的代碼在哪裏?爲什麼tick方法有參數'LinkedList object'? –

+0

動畫這個代碼在'GameObject'中嗎?是來自'GameObject tempObject = handler.object.get(i)的返回對象;'始終是相同的引用? –

+0

@hoyah_hayoh tick方法在我的主Game.java類中被調用,並且通過讓LinkedList一次調用每個遊戲對象。和皮膚持票人,我剛剛在我的帖子中添加了GameObject代碼。 – Cosmicluck

回答

0

您應該將方法參數分配給字段,而不是其他字段。

public void setVelX(float velX) 
{ 
    this.velX = velX; 
} 
public void setVelY(float velY) 
{ 
    this.velY = velY; 
} 
0
public void setVelX(float velX) 
{ 
    this.velX = x; 
} 
public void setVelY(float velY) 
{ 
    this.velY = y; 
} 

您在GameObject中的設置者不正確。

+0

你會如何解決它們?我沒有看到問題 – Cosmicluck