我有一个illegalStateException:没有设置EntityFactory!用fxgl

时间:2017-07-13 00:20:13

标签: java javafx

这是我的代码: 主:

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()。spawn(" Enemy")它说 .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();
    }

}

有没有人看到有什么问题请帮忙谢谢!

1 个答案:

答案 0 :(得分:1)

您似乎没有包裹。当主类没有包时,注释处理器被禁用。只需将两个类放入同一个包中,处理器就可以选择工厂类。

或者,您可以通过getGameWorld().setEntityFactory(...)手动设置工厂。如果正在使用其他注释,则前一种方法是首选。