2013-01-07 55 views
0

我製作了一個使用Java的遊戲,我的遊戲是一個基於狀態的遊戲,所以它擴展了StateBasedGame,我想把遊戲放在一個網站上,這需要我將它變成一個小程序(除非有另一種方式),所以它也必須擴展JApplet,在嘗試擴展多個類並在線閱讀之後,我沒有運氣,並且在論壇帖子上閱讀,它不可能有多個擴展。現在這是否意味着我無法將我的遊戲放到網站上?Java上的多個擴展

這裏是我的主類,到目前爲止,它延伸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();} 
    } 

} 

EDIT1:

的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,我的主類是遊戲中,我的軟件包名稱是javagame。

錯誤:ClassNotFoundException的 org.lwjgl.util.applet.AppletLoader

+0

這是什麼狀態?你解決了這個問題嗎? – tsm

回答

4

你不能有一個類從1個多類擴展。有辦法得到你想要的東西,其中最簡單的方法是讓另一個類擴展JApplet,並讓該類使用你的遊戲類。這就是所謂的組合 - 當一個類使用一個或多個其他類的實例時。

通常情況下,如果要從多個源擴展,則可以使用來自多個源的接口和implement。但是,這不一樣,因爲使用接口實際上並不提供功能;它只定義行爲,即指定實現類必須實現的方法。

6

Java不支持多重繼承。對於輸入問題,請使用接口。但在這種情況下,你對行爲更感興趣,所以我會用組合。

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

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

    ... 
} 
0

您需要將此類封裝在可從Applet內引導的對象中。

也就是說,Java不允許擴展多個類,所以你讀的是正確的。

2

如果我沒有錯,StateBasedGame類來自Slick 2D API。您應該查找有關使用Slick 2D製作小程序的信息,例如this post

+0

謝謝你的鏈接和回答我的問題,我已經做了鏈接說和使用的代碼,但是當我嘗試運行我的Java遊戲時,我得到以下錯誤:ClassNotFoundException org.lwjgl.util.applet.AppletLoader,可否請你幫我解決這個問題?我在原始文章中添加了我的html代碼和有關我的應用程序的信息。 –

+0

你可能錯過了一個Jar文件。你檢查了關於部署的鏈接嗎? http://ninjacave.com/appletloader – madth3

+0

感謝您的快速回復和鏈接,我讀了它並理解AppletLoader實際上是什麼,但在開始解釋簽名的地方有點困惑,我從這裏下載了lwjgl http: //lwjgl.org/download.php是否需要擔心簽名?另外,我有點困惑,因爲我沒有更多的jar文件,我爲我的遊戲製作了一個,其他的和教程中的一樣,做更新版本的lwjgl需要更多的jar文件,如果是的話,會怎樣我找到了這個jar文件? –