【发布时间】:2016-01-28 19:53:33
【问题描述】:
几天前,我开始制作一款“简单”的 2D 游戏。我试图用“实体系统”来做到这一点。我得到了类“GameObject”,它扩展了所有其他对象,如树木、敌人(史莱姆)等。所有这些游戏对象都存储在一个名为“gameObjects”的数组列表中。然后我使用 for 循环遍历列表中的所有对象并调用它们的基本函数,如 update() 和 draw()。直到现在一切正常,即使我不是 100% 确定为什么。问题是由于某种原因我不能对碰撞做同样的事情。
我知道这个话题在这里讨论过很多次,但即使过了很多天我也无法解决这个问题。有人能帮助我吗?另外,我为我的英语道歉。
游戏类:
public class Game extends BasicGame
{
public Game()
{
super("Game");
}
public void init(GameContainer gameContainer) throws SlickException
{
World.init();
}
public void update(GameContainer gameContainer, int delta) throws SlickException
{
World.update();
}
public void render(GameContainer gameContainer, Graphics g) throws SlickException
{
World.draw(g);
}
}
游戏对象类:
public abstract class GameObject
{
protected void update()
{
}
protected void draw()
{
}
}
树类:
public class Tree extends GameObject
{
public float x, y;
private static Image tree;
public Tree(float x, float y)
{
this.x = x;
this.y = y;
tree = Resources.miscSheet.getSprite(2, 0);
}
public void draw()
{
tree.draw(x, y)
}
}
史莱姆类:
public class Slime extends GameObject
{
public static float x;
public static float y;
private static Animation slimeAnim;
public Slime(int x, int y)
{
this.x = x;
this.y = y;
// My own method for loading animation.
slimeAnim = Sprite.getAnimation(Resources.slimeSheet, 0, 0, 5, 300);
}
public void update()
{
// *Random movement here*
}
public void draw()
{
slimeAnim.draw(x, y);
}
}
世界级:
public class World
{
public static List<GameObject> gameObjects = new ArrayList<GameObject>();
public static void init()
{
Tree tree = new Tree(0, 0);
Tree tree2 = new Tree(200, 200);
Slime slime = new Slime(80, 80);
gameObjects.add(tree);
gameObjects.add(tree2);
gameObjects.add(slime);
}
public static void update()
{
for (int i = 0; i < gameObjects.size(); i++)
{
GameObject o = gameObjects.get(i);
o.update();
}
}
public static void draw(Graphics g)
{
g.setBackground(new Color(91, 219, 87));
for (int i = 0; i < gameObjects.size(); i++)
{
GameObject o = gameObjects.get(i);
o.draw();
}
}
}
主类:
public class Main
{
public static AppGameContainer container;
public static void main(String[] args) throws SlickException
{
container = new AppGameContainer(new Game());
container.setDisplayMode(1024, 600, false);
container.setShowFPS(false);
container.start();
}
}
我删除了我之前的所有碰撞尝试,并跳过了一些其他不必要的事情。我现在如何实现碰撞,例如树木和史莱姆之间的碰撞?
【问题讨论】: