0

我正在使用 Slick2d 制作我的第一款游戏,但在尝试使用 Animation 时遇到了麻烦。

我想要做的是拥有一个主要的实体类以及移动和渲染组件。我想要的是当玩家按下箭头键时,该方向的步行动画开始播放,当他们不按下时,动画停止。

当我第一次测试组件时,我只为不同的方向使用了一个图像,它工作得很好。这只会让我更加困惑。只要我不按下任何箭头键,就会出现游戏画面。当我这样做时,正确的(方向)精灵动画开始并且位置正确改变,但是当它到达最后一帧时,游戏窗口关闭并且我得到一个错误。这就是它在控制台上告诉我的内容:

Wed Mar 21 13:20:47 PDT 2012 INFO:Slick Build #274
Wed Mar 21 13:20:47 PDT 2012 INFO:LWJGL Version: 2.0b1
Wed Mar 21 13:20:47 PDT 2012 INFO:OriginalDisplayMode: 1366 x 768 x 32 @60Hz
Wed Mar 21 13:20:47 PDT 2012 INFO:TargetDisplayMode: 750 x 600 x 0 @0Hz
Wed Mar 21 13:20:48 PDT 2012 INFO:Starting display 750x600
Wed Mar 21 13:20:48 PDT 2012 INFO:Use Java PNG Loader = true
Wed Mar 21 13:20:49 PDT 2012 INFO:Controllers not available
Wed Mar 21 13:20:52 PDT 2012 ERROR:null
java.lang.NullPointerException
at bunny.component.render.RenderComponent.render(RenderComponent.java:22)
at bunny.entity.Entity.render(Entity.java:177)
at bunny.game.BunnyGame.render(BunnyGame.java:56)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:681)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
at bunny.game.BunnyGame.main(BunnyGame.java:66)
Wed Mar 21 13:20:52 PDT 2012 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:684)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318)
at bunny.game.BunnyGame.main(BunnyGame.java:66)

由于渲染似乎有问题,我检查以确保在使用精灵的任何地方它都不会仍然试图将其视为图像而不是动画。我还确保渲染具有正确的参数,调用 draw,当它仍然不起作用时,我尝试让它只渲染当前帧,因为我知道渲染适用于 Image。这些都没有让我的问题消失。

我已经在网上搜索了很长时间,现在我很确定问题可能出在我的运动组件或渲染组件上。事情是,我发现唯一能解决这个问题的教程是http://slick.cokeandcode.com/wiki/doku.php?id=entity_tutorial

这是我的渲染组件类:

public class RenderComponent extends Component {    
Image image;

public RenderComponent(String id)
{
    this.id = id;
}

public void render(GameContainer gc, StateBasedGame sb, Graphics gr) 
{
    Vector2f pos = owner.getPosition();
    image.draw(pos.x, pos.y);
}

@Override
public void update(GameContainer gc, StateBasedGame sb, int delta) {
    image = owner.getDirection().getCurrentFrame();
}
}

这是我的箭头键运动课程:

public class ArrowKeyMovement extends Component{

float direction;
float speed;

public ArrowKeyMovement( String id )
{
    this.id = id;
}

public float getSpeed()
{
    return speed;
}

@Override
public void update(GameContainer gc, StateBasedGame sb, int delta)
{
    // 0 - up, 1 - down, 2 - left, 3 - right
    Input input = gc.getInput();
    Vector2f position = owner.getPosition();

    if (input.isKeyDown(Input.KEY_UP))
    {
        owner.setSprite(0);
        if (owner.getDirection().isStopped()) {
            owner.getDirection().restart();
        }
        owner.getDirection().update(delta);
        position.y -= delta * 0.2f;
    }
    else if (input.isKeyDown(Input.KEY_DOWN))
    {
        owner.setSprite(1);
        if (owner.getDirection().isStopped()) {
            owner.getDirection().restart();
        }
        owner.getDirection().update(delta);
        position.y += delta * 0.2f;
    }
    else if (input.isKeyDown(Input.KEY_LEFT))
    {
        owner.setSprite(2);
        if (owner.getDirection().isStopped()) {
            owner.getDirection().restart();
        }
        owner.getDirection().update(delta);
        position.x -= delta * 0.2f;
    }
    else if (input.isKeyDown(Input.KEY_RIGHT))
    {
        owner.setSprite(3);
        if (owner.getDirection().isStopped()) {
            owner.getDirection().restart();
        }
        owner.getDirection().update(delta);
        position.x += delta * 0.2f;
    }
    else 
    {
        if (!(owner.getDirection().isStopped())){
            owner.getDirection().stop();
        }
    }

    owner.setPosition(position);
}
}

在我的 Entity 类中,我像这样调用 RenderComponent:

public void render(GameContainer gc, StateBasedGame sb, Graphics gr)
{
    if(renderComponent != null)
        renderComponent.render(gc, sb, gr);
}

对不起,我对我的问题含糊不清,但我发誓我真的试图自己弄清楚。如果我没有提供足够的信息,请告诉我,我会提供更多信息。感谢您的时间!如果你有更详细的教程,我真的很感激。

4

1 回答 1

0

你确定图像在调用它的 render() 方法之前在你的 RenderComponent 类中被初始化,它使用图像变量?

换句话说,您确定在调用render() 方法之前,在 RenderComponent 中至少调用了一次 update() 方法吗?

于 2012-12-21T09:03:40.447 回答