0

我正在尝试开发一款游戏,现在还处于早期阶段,但我遇到了一个问题。

我创建了一个角色类并在该类中定义了一个动画,但想在另一个类中使用它,我为动画制作了一个 getter,但在我想使用它的类中,它只是返回 null。getter 和 setter 对整数工作得很好,但我无法弄清楚,任何帮助将不胜感激:)

玩家等级

import org.newdawn.slick.Animation;
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.SpriteSheet;
import org.newdawn.slick.state.StateBasedGame;

public class Player {

int x,y,dx,dy;
Animation currentAnim, DownWalk, LeftWalk;
SpriteSheet down;



public Player(int x, int y) throws SlickException{
    this.x = x;
    this.y = y;
    currentAnim = DownWalk;
}

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

    down = new SpriteSheet("res/Character/DownWalkSpriteSheet",160,160);
    DownWalk = new Animation(down, 200);
    currentAnim = DownWalk;

}

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    Input input = gc.getInput();
}

public void render(GameContainer gc, StateBasedGame arg1, Graphics sbg)
        throws SlickException {
    currentAnim.draw(100,100);
}

public Animation getAnim(){
    return currentAnim;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

}

级别类(我想在其中使用它的类):

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.GameState;
import org.newdawn.slick.state.StateBasedGame;

public class Room1 extends BasicGameState {

Image Room1;
Player player;

public Room1(int state){
}

@Override
public void init(GameContainer gc, StateBasedGame sbg)
        throws SlickException {
    Room1 = new Image("res/Room1.png");
    player = new Player(300,300);
}

@Override
public void render(GameContainer gc, StateBasedGame arg1, Graphics sbg)
        throws SlickException {
    Room1.draw(0,0);

}

system.out.print 是我检查它是否返回 null 的方式

@Override
public void update(GameContainer gc, StateBasedGame sbg, int delta)
        throws SlickException {
    System.out.println(player.getAnim());

}

@Override
public int getID() {
    // TODO Auto-generated method stub
    return 1;
}

}

4

1 回答 1

0

您必须init从 Player 类中调用方法。否则,您仍然会收到null创建的动画对象。

我想在创建 Player 对象后你想调用 init 方法:

@Override
public void init(GameContainer gc, StateBasedGame sbg)
        throws SlickException {
    Room1 = new Image("res/Room1.png");
    player = new Player(300,300);
    player.init(...) // HERE add the init call
}
于 2013-05-22T14:08:06.243 回答