0

我用 Java 制作了一个游戏,我的游戏是一个基于状态的游戏,所以它扩展了 StateBasedGame,我想把游戏放在一个网站上,这需要我把它做成一个小程序(除非有其他方法)所以它也必须扩展 JApplet,在尝试扩展多个类并在线阅读之后,我没有运气,并在论坛帖子上读到不可能有多个扩展。现在这是否意味着我不能将我的游戏放到网站上?

到目前为止,这是我的主要课程,它扩展了 StateBasedGame:

    package javagame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Game extends StateBasedGame{

    public static final String gamename = "Croft";
    public static final int menu = 0;
    public static final int play = 1;

    public Game(String gamename){//create window on statup
        super(gamename);//adds title to screen
        this.addState(new Menu(menu));//"this" means get from this class
        this.addState(new Play(play));
    }

    public void initStatesList(GameContainer gc) throws SlickException{
//we need this because we inhereted from StateBasedGame, gc manages behind the scene stuff
        this.getState(menu).init(gc, this);//telling java what states we have
        this.getState(play).init(gc, this);
        this.enterState(menu);//tells java that you want to show menu before play
    }

    public static void main(String[] args) {
        AppGameContainer appgc;//the window for your game
        try{
            appgc = new AppGameContainer(new Game(gamename));//window holding the Game
            appgc.setDisplayMode(640, 360,false);//size, sizetrue would make it full screen //640,360
            appgc.start();//creates the window
        }catch(SlickException e){//built into slick for error handelling
        e.printStackTrace();}
    }

}

编辑1:

html代码:

    <applet code="org.lwjgl.util.applet.AppletLoader" 
        archive="lwjgl_util_applet.jar" 
        codebase="." 
        width="640" height="480">

  <param name="al_title" value="Ham Blaster"> 
  <param name="al_main" value="org.newdawn.slick.AppletGameContainer"> 
  <param name="game" value="org.javagame.Game">

  <param name="al_jars" value="slick.jar, lwjgl.jar, slick.jar"> 

  <param name="al_windows" value="windows_natives.jar"> 
  <param name="al_linux" value="linux_natives.jar"> 
  <param name="al_mac" value="macosx_natives.jar"> 

  <param name="separate_jvm" value="true"> 
</applet> 

我的jar文件名是racegame,我的主类是Game,我的包名是javagame。

错误:ClassNotFoundException org.lwjgl.util.applet.AppletLoader

4

4 回答 4

6

Java 不支持多重继承。对于打字问题,请使用接口。但在这种情况下,你对行为更感兴趣,所以我会使用组合。

public class GameApplet extends JApplet {
  private Game game = new Game();

  public void init() {
    game.foo();
    ...
  }

  ...
}
于 2013-01-07T22:19:09.460 回答
4

您不能从超过 1 个类扩展一个类。有一些方法可以得到你想要的,其中最简单的可能是拥有另一个扩展类JApplet,并让该类使用你的游戏类。这称为组合——当一个类使用一个或多个其他类的实例时。

通常,如果您想从多个来源扩展,您将使用接口,并且implement来自多个来源。但是,这不一样,因为使用接口实际上并不提供功能。它只定义行为,即指定实现类必须实现的方法。

于 2013-01-07T22:19:02.287 回答
2

如果我没记错的话,StateBasedGame该类来自 Slick 2D API。您应该查找有关使用 Slick 2D 制作小程序的信息,例如这篇文章

于 2013-01-07T22:22:59.567 回答
0

您需要将此类封装在一个可以从 Applet 中引导的对象中。

也就是说,Java 不允许扩展多个类,所以您阅读的内容是正确的。

于 2013-01-07T22:20:16.680 回答