欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

实操示例:在JavaFX中运用FXGL类

最编程 2024-07-23 21:17:23
...

实例1: init

import com.almasb.fxgl.app.FXGL; //导入依赖的package包/类
@Override
public void init() {
    timeBar = new ProgressBar(false);
    timeBar.setHeight(50);
    timeBar.setTranslateX(0);
    timeBar.setTranslateY(100);
    timeBar.setRotate(-90);
    timeBar.setFill(Color.GREEN);
    timeBar.setLabelVisible(false);
    timeBar.setMaxValue(PacmanApp.TIME_PER_LEVEL);
    timeBar.setMinValue(0);
    timeBar.setCurrentValue(PacmanApp.TIME_PER_LEVEL);
    timeBar.currentValueProperty().bind(FXGL.getApp().getGameState().intProperty("time"));

    root.getChildren().addAll(timeBar);

    labelScore.setFont(FXGL.getUIFactory().newFont(24));
    labelTeleport.setFont(FXGL.getUIFactory().newFont(24));
}
 

实例2: onUpdate

import com.almasb.fxgl.app.FXGL; //导入依赖的package包/类
@Override
public void onUpdate(Entity entity, double tpf) {

    speed = tpf * 60 * 2;

    Point2D velocity = nextWaypoint.subtract(position.getValue())
            .normalize()
            .multiply(speed);

    position.translate(velocity);

    if (nextWaypoint.distance(position.getValue()) < speed) {
        position.setValue(nextWaypoint);

        if (!waypoints.isEmpty()) {
            nextWaypoint = waypoints.remove(0);
        } else {

            FXGL.getEventBus().fireEvent(new EnemyReachedGoalEvent());
        }
    }
}
 

实例3: spawnTower

import com.almasb.fxgl.app.FXGL; //导入依赖的package包/类
@Spawns("Tower")
public Entity spawnTower(SpawnData data) {
    TowerDataComponent towerComponent;
    try {
        towerComponent = FXGL.getAssetLoader()
                .loadKV("Tower" + data.get("index") + ".kv")
                .to(TowerDataComponent.class);

    } catch (Exception e) {
        throw new RuntimeException("Failed to parse KV file: " + e);
    }

    return Entities.builder()
            .type(TowerDefenseType.TOWER)
            .from(data)
            .viewFromNode(new Rectangle(40, 40, data.get("color")))
            .with(new CollidableComponent(true), towerComponent)
            .with(new TowerControl())
            .build();
}
 

实例4: onUpdate

import com.almasb.fxgl.app.FXGL; //导入依赖的package包/类
@Override
public void onUpdate(Entity entity, double tpf) {
    grid.applyExplosiveForce(velocity.magnitude() / 60 * 18, bbox.getCenterWorld(), 80 * 60 * tpf);

    if (bbox.getMinXWorld() < 0) {
        spawnParticles(0, bbox.getCenterWorld().getY(), 1, FXGLMath.random(-1.0f, 1.0f));

    } else if (bbox.getMaxXWorld() > FXGL.getApp().getWidth()) {
        spawnParticles(FXGL.getApp().getWidth(), bbox.getCenterWorld().getY(), -1, FXGLMath.random(-1.0f, 1.0f));

    } else if (bbox.getMinYWorld() < 0) {
        spawnParticles(bbox.getCenterWorld().getX(), 0, FXGLMath.random(-1.0f, 1.0f), 1);

    } else if (bbox.getMaxYWorld() > FXGL.getApp().getHeight()) {
        spawnParticles(bbox.getCenterWorld().getX(), FXGL.getApp().getHeight(), FXGLMath.random(-1.0f, 1.0f), -1);
    }
}