fxgl 创建一个场景跳转组件
时间: 2023-10-13 14:06:39 浏览: 50
在 FXGL 中,可以通过 `FXGL.getSceneService().pushScene(targetScene)` 方法来跳转到目标场景。为了方便起见,我们可以创建一个自定义的组件来实现场景跳转功能。
首先,创建一个名为 `SceneSwitchComponent` 的类,继承 `com.almasb.fxgl.entity.component.Component` 类,代码如下:
```java
import com.almasb.fxgl.core.serialization.Bundle;
import com.almasb.fxgl.entity.component.Component;
public class SceneSwitchComponent extends Component {
private String targetScene;
public SceneSwitchComponent(String targetScene) {
this.targetScene = targetScene;
}
@Override
public void onUpdate(double tpf) {
// 空实现,不需要实现 onUpdate 方法
}
@Override
public void onAdded() {
// 点击实体时跳转到目标场景
entity.getViewComponent().addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
FXGL.getSceneService().pushScene(targetScene);
});
}
@Override
public void onRemoved() {
// 移除事件监听器
entity.getViewComponent().removeEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
FXGL.getSceneService().pushScene(targetScene);
});
}
@Override
public void write(Bundle bundle) {
bundle.put("targetScene", targetScene);
}
@Override
public void read(Bundle bundle) {
targetScene = bundle.get("targetScene");
}
}
```
这个组件包含一个 `targetScene` 字段,表示要跳转的目标场景名称。在 `onAdded()` 方法中,我们为实体的 `ViewComponent` 添加了一个鼠标点击事件监听器,当用户点击实体时,就会自动跳转到目标场景。`onRemoved()` 方法用于在移除组件时移除事件监听器。
接下来,在需要使用场景跳转功能的实体上添加 `SceneSwitchComponent` 组件即可。例如,我们可以创建一个名为 `Button` 的实体,代码如下:
```java
import com.almasb.fxgl.entity.Entities;
import com.almasb.fxgl.entity.Entity;
import javafx.geometry.Pos;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
public class Button extends Entity {
public Button(String text, String targetScene) {
Text textNode = new Text(text);
textNode.setFont(Font.font(24));
textNode.setFill(Color.WHITE);
setViewFromNode(textNode);
setAlignment(Pos.CENTER);
addComponent(new SceneSwitchComponent(targetScene));
}
}
```
这个实体包含一个用于显示文本的 `Text` 节点,并且添加了一个 `SceneSwitchComponent` 组件。创建实体时需要传入两个参数,分别是文本内容和目标场景名称。
最后,在游戏场景中添加这个 `Button` 实体,代码如下:
```java
import com.almasb.fxgl.app.scene.GameScene;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.Entities;
public class MyGameScene extends GameScene {
public MyGameScene() {
Entity button = Entities.builder()
.at(400, 300)
.viewFromNode(new Button("跳转到场景2", "scene2"))
.build();
addEntities(button);
}
}
```
这里创建了一个名为 `MyGameScene` 的游戏场景,并且添加了一个 `Button` 实体。当用户点击这个实体时,就会自动跳转到名为 `scene2` 的场景。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)