Minecraft 1.12.2模组开发(十七) 声音事件

我们本次对于游戏模组中方块、生物的声音事件(SoundEvent)进行设置。

1.util.handlers 新建 SoundsHandler 类

cr1.jpg

在 SoundsHandler.java 中编写 声音事件信息
package com.Joy187.newmod.util.handlers;

import com.Joy187.newmod.util.Reference;

import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraftforge.fml.common.registry.ForgeRegistries;

public class SoundsHandler {
                            //你所要进行设置的声音事件
	public static SoundEvent ENTITY_RA3_AMBIENT,ENTITY_RA3_HURT,ENTITY_RA3_DEATH;
	
	//注册声音事件
	public static void registerSounds() {
		ENTITY_RA3_AMBIENT = registerSound("entity.ra3.ambient");
		ENTITY_RA3_HURT = registerSound( "entity.ra3.hurt");
		ENTITY_RA3_DEATH = registerSound( "entity.ra3.death");
	}
	
	private static SoundEvent registerSound(String name) {
		ResourceLocation location = new ResourceLocation(Reference.Mod_ID, name);
		SoundEvent event = new SoundEvent(location);
		event.setRegistryName(name);
		ForgeRegistries.SOUND_EVENTS.register(event);
		return event;
	}
}

2. 修改 RegistryHandler.java 中的 initRegistries() 函数。添加声音注册信息

	public static void initRegistries() {
		SoundsHandler.registerSounds();
	}

cr2.jpg

3.在Main.java 的 Init() 函数中添加初始化语句。

	@EventHandler
	public static void Init(FMLInitializationEvent event)
	{
		ModRecipes.init();
		ModSpawn.registerSpawnList();
		//添加初始化语句
		RegistryHandler.initRegistries();
	}

CR.jpg

4.在 resource 中新建sounds 包 -> 在sounds包中新建entity -> 在entity包中新建 生物名称 的包(以ra3为例)

cr3.jpg

5.我们可以制作相关音频文件,导出格式要是.ogg文件,放入这个包中

对于ogg文件的转化,可以使用 Audacity格式工厂

cr4.jpg
cr5.jpg
cr6.jpg

6.在resource包中新建sounds.json文件

将我们设置的声音事件在json文件中进行注册:

{
  "entity.ra3.ambient":{
    "category":"entity",
    "subtitle" :"entity.ra3.ambient",
    "sounds":[{ "name": "joymod:entity/ra3/ambient", "stream":true }]
  },
  "entity.ra3.hurt":{
    "category":"entity",
    "subtitle" :"entity.ra3.hurt",
    "sounds":[{ "name": "joymod:entity/ra3/hurt", "stream":true }]
  },
  "entity.ra3.death":{
    "category":"entity",
    "subtitle" :"entity.ra3.death",
    "sounds":[{ "name": "joymod:entity/ra3/death", "stream":true }]
  }
}

7.在我们的生物类中添加三个函数,对音效进行修改:

	@Override
	protected SoundEvent getAmbientSound() {
		return SoundsHandler.ENTITY_RA3_AMBIENT;
	}
	
	@Override
	protected SoundEvent getHurtSound(DamageSource source) {
		return SoundsHandler.ENTITY_RA3_HURT;
	}
	
	@Override
	protected SoundEvent getDeathSound() {
		return SoundsHandler.ENTITY_RA3_DEATH;
	}

cr7.jpg

8.保存所有操作 -> 运行游戏 -> 测试相关音效 -> OK!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jay_fearless

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值