1
由於某種原因,您只是繼續向左移動,我無法弄清楚原因。鍵盤輸入讀數不是問題,因爲向任何其他方向移動都可以很好地工作。忽略isClipped()方法,因爲我還沒有完成它。謝謝您的幫助。使用浮油庫移動2D遊戲的問題
主類
package Genisis;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import org.lwjgl.input.Mouse;
import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.tiled.TiledMap;
public class Play extends BasicGameState
{
int mouseX;
int mouseY;
Level testLevel;
boolean[][] blocked;
MainCharacter mc;
public Play(int state)
{
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException
{
gc.setMouseCursor("res/sprites/Cursor.png", 0, 0);
try {
testLevel = new Level(0, 0, "res/maps/testmap.tmx");
} catch (FileNotFoundException e) {
System.out.println("Failed to load map.");
e.printStackTrace();
}
mouseX = Mouse.getX();
mouseY = gc.getHeight() - Mouse.getY();
mc = new MainCharacter(50, 50, new Image("res/sprites/mcg.png"));
}
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws
SlickException
{
testLevel.render(mc.x, mc.y);
mc.render(mouseX, mouseY);
}
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws
SlickException
{
mc.move(gc, testLevel);
mc.render(gc.getWidth()/2, gc.getHeight()/2);
testLevel.render(mc.x, mc.y);
}
public int getID()
{
return 1;
}
public void spawnMC(float spwnMCX, float spwnMCY)
{
testLevel.render(spwnMCX, spwnMCY);
testLevel.render(spwnMCX, spwnMCY);
}
public void spawnOC()
{
//todo
}
}
Player類
package Genisis;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
public class MainCharacter extends Player
{
public MainCharacter(float newX, float newY, Image newSprite) throws SlickException
{
sprite = newSprite;
x = newX;
y = newY;
}
public void move(GameContainer gc, Level map)
{
Input in = gc.getInput();
if(in.isKeyDown(Input.KEY_W))
if(map.isClipped(x, y + 1))
y += 1;
if(in.isKeyDown(Input.KEY_D))
if(map.isClipped(x + 1, y))
x -= 1;
if(in.isKeyDown(Input.KEY_S))
if(map.isClipped(x, y - 1))
y -= 1;
if(in.isKeyDown(Input.KEY_A));
if(map.isClipped(x - 1, y))
x += 1;
}
public void render(float mX, float mY)
{
float xDist = mX - (500 + (sprite.getWidth()/2));
float yDist = mY - (250 + (sprite.getHeight()/2));
double angleToTurn = Math.toDegrees(Math.atan2(yDist, xDist));
sprite.setRotation((float)angleToTurn);
sprite.draw(500, 250);
}
}
水平類
package Genisis;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.tiled.TiledMap;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Level
{
//height of background (in tiles)
public int height;
//width of background (in tiles)
public int width;
public boolean[][] clipped;
public TiledMap map;
public Level(int newHeight, int newWidth, String path) throws
FileNotFoundException, SlickException
{
map = new TiledMap(path);
clipped = new boolean[map.getHeight()][map.getWidth()];
for(int i = 0; i < map.getHeight(); i++)
for(int j = 0; i < map.getWidth(); i++)
{
if("true".equals(map.getTileProperty(map.getTileId(i, j,
0), "blocked", "false")))
clipped[i][j] = false;
}
}
//render map
public void render(float mcX, float mcY)
{
map.render((int)(mcX), (int)(mcY));
}
//return height of level (in pixels)
public int getHeight()
{
return map.getHeight();
}
//return width of level (in pixels)
public int getWidth()
{
return map.getWidth();
}
public boolean isClipped(float charX, float charY)
{
//return clipped[(int)(charX/50)][(int)(charY/50)];
return true;
}
}