2017-07-13 30 views
0

這是我的代碼: 主營::EntityFactory沒有設置!與fxgl

import com.almasb.fxgl.app.GameApplication; 
import com.almasb.fxgl.core.math.FXGLMath; 
import com.almasb.fxgl.settings.GameSettings; 
import javafx.util.Duration; 

public class Main extends GameApplication 
{ 

    public static void main(String[] args) 
    { 
     launch(args); 
    } 

    @Override 
    protected void initSettings(GameSettings gameSettings) 
    { 
     gameSettings.setTitle("Shooter"); 
     gameSettings.setVersion("1.0"); 
     gameSettings.setHeight(1000); 
     gameSettings.setWidth(1600); 
     gameSettings.setCloseConfirmation(false); 
     gameSettings.setProfilingEnabled(false); 
     gameSettings.setIntroEnabled(false); 
     gameSettings.setMenuEnabled(false); 
    } 

    @Override 
    protected void initGame() 
    { 
     getMasterTimer().runAtInterval(() -> { 

       getGameWorld().spawn("Enemy", 
         FXGLMath.random(0, (int) getWidth() - 40), 
         FXGLMath.random(0, (int) getHeight()/2 - 40) 
       ); 

     }, Duration.seconds(1)); 
    } 
} 

問題是getgamewolrd()產卵( 「敵人」),它說 .IllegalStateException:EntityFactory沒有設置! 這是我的工廠類:

import com.almasb.fxgl.annotation.SetEntityFactory; 
import com.almasb.fxgl.annotation.Spawns; 
import com.almasb.fxgl.ecs.Entity; 
import com.almasb.fxgl.entity.Entities; 
import com.almasb.fxgl.entity.EntityFactory; 
import com.almasb.fxgl.entity.SpawnData; 
import com.almasb.fxgl.entity.component.CollidableComponent; 
import com.almasb.fxgl.entity.control.ProjectileControl; 
import javafx.geometry.Point2D; 
import javafx.scene.paint.Color; 
import javafx.scene.shape.Rectangle; 

@SetEntityFactory 
public class Factory implements EntityFactory 
{ 
    @Spawns("Bullet") 
    public Entity newBullet(SpawnData data) { 
     return Entities.builder() 
       .from(data) 
       .type(EntityTypes.BULLET) 
       .viewFromNodeWithBBox(new Rectangle(10, 2, Color.BLUE)) 
       .with(new CollidableComponent(true)) 
       .with(new ProjectileControl(new Point2D(0, -1), 300)) 
       .build(); 
    } 

    @Spawns("Enemy") 
    public Entity newEnemy(SpawnData data) { 
     return Entities.builder() 
       .from(data) 
       .type(EntityTypes.ENEMY) 
       .viewFromNodeWithBBox(new Rectangle(40, 40, Color.RED)) 
       .with(new CollidableComponent(true)) 
       .build(); 
    } 

} 

沒有人看到什麼錯了,請幫助謝謝!

+0

這似乎是非常具體的'com.almasb.fxgl'庫,我從來沒有聽說過 - 試圖看看你是否可以從開發者支持 –

回答

1

看起來你沒有包裝。當主類沒有包時,註釋處理器被禁用。只需將兩個類放入處理器的相同包中就可以拿到工廠類。

或者,您可以通過getGameWorld().setEntityFactory(...)手動設置工廠。如果有其他註釋正在使用,則前一種方法是首選。

+0

謝謝Almas! –

+0

我把它們全部放在一個包裏,現在仍然給我同樣的錯誤,我可以導入lib庫嗎? –

+0

我修好了我不得不手工製作Factory類的一個實例,就像你說的,哈哈謝謝! –