2012-11-29 52 views
-1

「我正在做一個簡單的遊戲,對於它的一部分,我希望那裏有敵人,它會攻擊你。要做到這一點,我需要讓他們移動 在這個代碼中,「敵人」只是一個球。 我使用一個名爲「mob1」的對象作爲球的位置,以便以後我可以有多個球。移動一個球由於某種原因非常不好?

(順便說一句,進出口使用Slick-只是櫃面改變任何東西)

我的比賽狀態 -

package Worlds.World1; 

import org.lwjgl.input.Mouse; 
import org.newdawn.slick.Animation; 
import org.newdawn.slick.Color; 
import org.newdawn.slick.GameContainer; 
import org.newdawn.slick.Graphics; 
import org.newdawn.slick.Image; 
import org.newdawn.slick.Input; 
import org.newdawn.slick.SlickException; 
import org.newdawn.slick.state.BasicGameState; 
import org.newdawn.slick.state.StateBasedGame; 

import Main.SimpleMob; 

public class World1A extends BasicGameState{ 
    String mousePosition; 
    Image world; 
    Animation player, playerLeft, playerRight; 
    int[] duration = {200,200}; 
    float playerX; 
    float playerY; 
    float WorldX; 
    float WorldY; 
    float PlayerVisibleScreenX; 
    float PlayerVisibleScreenY; 
    String MovementDirection; 
    SimpleMob mob1 = new SimpleMob(); 
    public World1A(int state){ 
    } 

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{ 
     Image [] WalkingLeft = {new Image("res/Sprites/buckysLeft.png"),new Image("res/Sprites/buckysLeft.png")}; 
     Image [] WalkingRight = {new Image("res/Sprites/buckysRight.png"),new Image("res/Sprites/buckysRight.png")}; 

     playerLeft = new Animation(WalkingLeft, duration, false); 
     playerRight = new Animation(WalkingRight, duration, false); 
     player = playerRight; 
     playerX = 0; 
     playerY = 0; 
     WorldX = 0; 
     WorldY = 0; 
     world= new Image("res/WorldImages/WorldBackGround.png"); 
     mousePosition="null"; 
     MovementDirection = "Not Moved Yet"; 
    } 

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{ 
     world.draw(WorldX, WorldY); 
     g.setColor(Color.white); 
     g.fillOval(WorldX+mob1.getX(), WorldY+mob1.getY(), 50, 50); 
     g.fillRect(WorldX, WorldY+300, 500, 10); 
    } 

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{ 
     mob1.autoEveryThing(delta, playerX, playerY); 
     int posX = Mouse.getX(); 
     int posY = Mouse.getY(); 
     mousePosition = "X: " + posX + "\nY: " + posY; 

     Input input = gc.getInput(); 
     if(input.isKeyDown(Input.KEY_LEFT)){ 
      WorldX += delta * 0.1f; 
      MovementDirection = "Left"; 
      player = playerLeft; 
     }else if(input.isKeyDown(Input.KEY_RIGHT)){ 
      WorldX -= delta * 0.1f; 
      MovementDirection = "Right"; 
      player = playerRight; 
     }else{ 
      MovementDirection = "Not Moving"; 
     } 
    } 


    //DO NOT CHANGE 
    public int getID(){ 
     return 2; 
    } 

} 

而我的暴徒接收機類

package Main; 

import org.newdawn.slick.Graphics; 

public class SimpleMob { 

    //This class shall be used as an object creator. This will randomly move a graphic around, near to a player 
    private float MobX; 
    private float MobY; 
    private int AmountMoved = 0; 
    private boolean MoveRight = true; 
    private boolean MoveLeft; 
    private boolean PlayerNear = false; 
    public boolean PlayerDetected = false; 

    //Used to find the mobs X 
    public float getX(){ 
     return MobX; 
    } 

    //Used to find the mobs Y 
    public float getY(){ 
     return MobY; 
    } 

    //Used to set the mobs X 
    public void setX(float X){ 
     MobX = X; 
    } 

    //Used to set the mobs Y 
    public void setY(float Y){ 
     MobY = Y; 
    } 

    //Used to simply move the mob on its X co-ords 
    public void moveX(int delta){ 
     MobX += delta*0.1f; 
    } 

    //Used to simply move the mob on its Y co-ords 
    public void moveY(int delta){ 
     MobY += delta*0.1f; 
    } 

    public void autoEveryThing(int delta, float playerX, float playerY) { 

     System.out.println(AmountMoved); 
     // If the player has not been spotted the NPC/Mob will move left and 
     // right by 100 Pixels. 
     if (MoveRight == true) { 
      AmountMoved++; 
      MobX += delta * 0.1f; 
      if (AmountMoved == 100) { 
       MoveRight = false; 
       MoveLeft = true; 
       AmountMoved = 0; 
      } 
     } 
     if (MoveLeft == true) { 
      MobX -= delta * 0.1f; 
      AmountMoved++; 
      if (AmountMoved == 100) { 
       MoveRight = true; 
       MoveLeft = false; 
       AmountMoved = 0; 
      } 
     } 
    } 

} 

任何幫助將是好,(或者,如果那裏有一個更正確的/ easyier方式讓小怪)

感謝

+0

試着問一個具體的編程問題,你限制你的問題的範圍,具體的問題。 –

+0

「特定」你究竟是什麼? –

+0

我只看到了代碼的一面牆和你想做什麼的簡短解釋 –

回答

0

的問題可能出在你的autoEveryThing(...)電話System.out.println(...)。這是每秒多次完成的,輸出到控制檯是人們可以做的最密集的事情之一。

+0

不,這不是。本的System.out.println()我加了之後,看它是否是財產以後做的AmountMoved整數,刪除它沒有迪菲昂斯 –

+0

好,取出的System.out.println做去除一些參差不齊的運動,但我仍然相當多。 –

+0

我仍然對你的意思有點混淆:)你的意思不是與時間有關的平滑運動,還是以不同於應有的方式運動? –

相關問題