实操示例:在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);
}
}
上一篇: 轻松探索ASM字节码:玩转底层编程技术
推荐阅读
-
实操示例:在Java中运用ChannelMatcher类
-
实操示例:在Java中运用WebSocketClientProtocolHandler类
-
实操演示:Java中Cosine相似度计算类的运用示例
-
实操示例:利用Java ConverterManager类的运用方法
-
实操示例:在JavaFX中运用FXGL类
-
实操指南:Java Web Service 中 Feature 类的运用示例
-
实操指南:Java FeatureCollection类的运用示例
-
实操示例:在Java中运用MySQLTransactionRollbackException类
-
实操指南:Java中SqoopOptions类的运用示例
-
实操示例:在Java中运用Quality类的方法与技巧