前言
??对于一个游戏来说,音效也是必不可少的一环,自然也不能少了。这章我们加入背景音乐以及一些音效。
音频资源
??首先在resource目录下创建assets/music以及assets/sounds目录。resource/assets/music目录下用来存放我们的MP3资源文件,assets/sounds目录用来存放wav资源文件。我们将bgm.mp3放入music目录下,eat.wav放入sounds目录下。
编码
首先完成BGM
??BGM需要在游戏启动后就开始播发,所以我们需要在游戏引擎初始化完成之后就开始执行。所以我们来重写onPreInit方法,该方法在引擎加载完成之后执行。
@Override
protected void onPreInit() {
loopBGM("bgm.mp3");
}
紧接着完成音效部分
??在我们的Player对象碰撞到Gold对象时,我们需要一个奖励音效。
@Override
protected void initPhysics() {
getPhysicsWorld().addCollisionHandler(new CollisionHandler(EntityType.PLAYER,EntityType.GOLD){
@Override
protected void onCollisionBegin(Entity player, Entity gold) {
gold.removeFromWorld();
inc("integral",+1);
spawn("Gold", FXGLMath.random(0,getAppWidth()-50),FXGLMath.random(0,getAppHeight()-50));
play("eat.wav");
}
});
onCollisionBegin(EntityType.PLAYER, EntityType.ENEMY,(player, enemy)->{
inc("integral",-1);
});
}
项目目录
完整代码
import com.almasb.fxgl.app.GameApplication;
import com.almasb.fxgl.app.GameSettings;
import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.physics.CollisionHandler;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import javafx.util.Duration;
import java.util.Map;
import static com.almasb.fxgl.dsl.FXGL.*;
import static com.almasb.fxgl.dsl.FXGL.onCollisionBegin;
public class HelloWorldApp extends GameApplication {
private Entity player;
@Override
protected void onPreInit() {
loopBGM("bgm.mp3");
}
@Override
protected void initSettings(GameSettings settings) {
settings.setTitle("Hello world");
settings.setWidth(600);
settings.setHeight(600);
settings.setMainMenuEnabled(true);
settings.setGameMenuEnabled(true);
}
@Override
protected void initGame() {
FXGL.getGameWorld().addEntityFactory(new DemoEntityFactory());
player = FXGL.spawn("Player",FXGL.getAppWidth()/2,FXGL.getAppHeight()/2);
spawn("Gold", 200,200);
run(()->{
var x = FXGLMath.random(0,getAppWidth()-50);
var y = FXGLMath.random(-50,0);
spawn("Enemy",x,y);
},Duration.seconds(1));
}
@Override
protected void initInput() {
onKey(KeyCode.W,"上",()->{
player.translateY(-5);
});
onKey(KeyCode.S,"下",()->{
player.translateY(+5);
});
onKey(KeyCode.A,"左",()->{
player.translateX(-5);
});
onKey(KeyCode.D,"右",()->{
player.translateX(+5);
});
}
@Override
protected void initPhysics() {
getPhysicsWorld().addCollisionHandler(new CollisionHandler(EntityType.PLAYER,EntityType.GOLD){
@Override
protected void onCollisionBegin(Entity player, Entity gold) {
gold.removeFromWorld();
inc("integral",+1);
spawn("Gold", FXGLMath.random(0,getAppWidth()-50),FXGLMath.random(0,getAppHeight()-50));
play("eat.wav");
}
});
onCollisionBegin(EntityType.PLAYER, EntityType.ENEMY,(player, enemy)->{
inc("integral",-1);
});
}
@Override
protected void initGameVars(Map<String, Object> vars) {
vars.put("integral",0);
}
@Override
protected void initUI() {
var scoreText = getUIFactoryService().newText("", Color.PINK, 18);
scoreText.textProperty().bind(getip("integral").asString("分数: %d"));
addUINode(scoreText, 10, 30);
}
public static void main(String[] args) {
launch(args);
}
}
总结
??至此,入门应该没有什么问题了。觉得有用的话点个赞或者关注一下呗。
完整项目
https://github.com/lhDream/lhDreamGameDemo/tree/master/HelloWorld
|