我是Java的新手,我有基本的编程知识。我最近开始开发游戏(在教程的帮助下)。游戏是RPG。它被设置为使得玩家的化身始终位于屏幕的中心,但是背景滚动。我知道之前已经问过这个问题,但没有一个答案对我有意义。基本上,如何为多个对象创建碰撞检测?我希望它能让我的玩家在撞墙时停止移动。我目前对边界设置有碰撞,但是如何设置房屋,墙壁等的碰撞?我的代码副本如下。先感谢您。另外,我正在使用Slick2D。
package javagame;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.*;
//Need "extends etc." for creating basic screen/state
public class Play extends BasicGameState{
public Rectangle lake1;
Animation avatar, movingUp, movingDown, movingLeft, movingRight;
Image worldMap;
boolean quit = false;
int[] duration = {200,200};
float avatarPositionX = 0;
float avatarPositionY = 0;
float shiftX = avatarPositionX + 250;
float shiftY = avatarPositionY + 180;
public Play(int state){
}
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException{
worldMap = new Image("res/WaterMap.png");
Image[] walkUp = {new Image("res/CharBack.png"), new
Image("res/CharBack.png")};
Image[] walkDown = {new Image("res/CharFront.png"), new
Image("res/CharFront.png")};
Image[] walkLeft = {new Image("res/CharLeft.png"), new
Image("res/CharLeft.png")};
Image[] walkRight = {new Image("res/CharRight.png"), new
Image("res/CharRight.png")};
movingUp = new Animation(walkUp, duration, false);
movingDown = new Animation(walkDown, duration, false);
movingLeft = new Animation(walkLeft, duration, false);
movingRight = new Animation(walkRight, duration, false);
avatar = movingDown;
}
//Takes gc and StateBasedGame and Graphics
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws
SlickException{
lake1 = new Rectangle(132, 277, 87, 54);
worldMap.draw(avatarPositionX, avatarPositionY);
avatar.draw(shiftX, shiftY);
g.drawString("Your X: "+avatarPositionX+"\nYour Y: "+avatarPositionY, 350,
20);
if(quit==true){
g.drawString("Resume (R)", 200, 100);
g.drawString("Main Menu (M)", 200, 150);
g.drawString("Quit (Q)", 200, 200);
if(quit==false){
g.clear();
}
}
}
//Updates images on screen; creates animation
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws
SlickException{
Input input = gc.getInput();
//up
if(input.isKeyDown(Input.KEY_UP)){
avatar = movingUp;
avatarPositionY += delta * .1f;
//+= changed to -=
if(avatarPositionY>180){
avatarPositionY -= delta * .1f;
}
}
//down
if(input.isKeyDown(Input.KEY_DOWN)){
avatar = movingDown;
avatarPositionY -= delta * .1f;
if(avatarPositionY<-497){
avatarPositionY += delta * .1f;
}
}
//left
if(input.isKeyDown(Input.KEY_LEFT)){
avatar = movingLeft;
avatarPositionX += delta * .1f;
if(avatarPositionX>249){
avatarPositionX -= delta * .1f;
}
}
//right
if(input.isKeyDown(Input.KEY_RIGHT)){
avatar = movingRight;
avatarPositionX -= delta * .1f;
if(avatarPositionX<-433){
avatarPositionX += delta * .1f;
}
}
//escape
if(input.isKeyDown(Input.KEY_ESCAPE)){
quit = true;
}
//when menu is up
if(quit == true){
if(input.isKeyDown(Input.KEY_R)){
quit = false;
}
if(input.isKeyDown(Input.KEY_M)){
sbg.enterState(0);
}
if(input.isKeyDown(Input.KEY_Q)){
System.exit(0);
}
}
}
public int getID(){
return 1;
}
}
答案 0 :(得分:0)
在您实际移动角色之前,请检查下一个位置是否为“空白”。在界限内(你实际如何实现这取决于你如何实现你的房屋和墙壁),只有移动角色才能实现。
对于实现,您可能希望迭代每个对象&#39;并检查角色的新位置是否在其中。