我才是Joy
Audacity软件下载

1.12.2声音事件教程

我们今天来为模组中的生物加上声音效果


1.首先我们要制作几个生物的音效文件(.ogg文件),可以用Audacity软件进行转换(mp3->ogg)


resources\assets\你的modid\sounds\entity路径下新建我们的本生物音效包dimi,将所有的音效文件都放入该文件夹:



![cr2.png]()

2.在resources\assets\你的modid中新建一个sounds.json`文件,将我们刚刚的几个音效文件进行注册:



![cr3.png]()

sounds.json


{
  "entity.dimi.ambient1": {
    "category": "entity",
    "subtitle": "entity.dimi.ambient1.sub",
    "sounds": [
      {
        "name": "re8joymod:entity/dimi/ambient1",
        "stream": true
      }
    ]
  },
  "entity.dimi.ambient2": {
    "category": "entity",
    "subtitle": "entity.dimi.ambient2.sub",
    "sounds": [
      {
        "name": "re8joymod:entity/dimi/ambient2",
        "stream": true
      }
    ]
  },
  "entity.dimi.death2": {
    "category": "entity",
    "subtitle": "entity.dimi.death2.sub",
    "sounds": [
      {
        "name": "re8joymod:entity/dimi/death2",
        "stream": true
      }
    ]
  },
  "entity.dimi.hurt1": {
    "category": "entity",
    "subtitle": "entity.dimi.hurt1.sub",
    "sounds": [
      {
        "name": "re8joymod:entity/dimi/hurt1",
        "stream": true
      }
    ]
  },
  "entity.dimi.hurt2": {
    "category": "entity",
    "subtitle": "entity.dimi.hurt2.sub",
    "sounds": [
      {
        "name": "re8joymod:entity/dimi/hurt2",
        "stream": true
      }
    ]
  },
  "entity.dimi.death1": {
    "category": "entity",
    "subtitle": "entity.dimi.death1.sub",
    "sounds": [
      {
        "name": "re8joymod:entity/dimi/death1",
        "stream": true
      }
    ]
  }
}

3.在init包中新建SoundInit类,将我们资源包中所有的音效对应起来:


SoundInit.java


package com.joy187.re8joymod.init;

import net.minecraftforge.registries.RegistryObject;

import com.joy187.re8joymod.Main;

import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;

public class SoundInit {

    public static final DeferredRegister<SoundEvent> SOUNDS = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, Main.MOD_ID);
                                                                            //这个路径与第二步"name"字段对应
    public static final RegistryObject<SoundEvent> ENTITY_DIMI_AMBIENT1 = build("entity.dimi.ambient1");
    public static final RegistryObject<SoundEvent> ENTITY_DIMI_AMBIENT2 = build("entity.dimi.ambient2");
    public static final RegistryObject<SoundEvent> ENTITY_DIMI_HURT1 = build("entity.dimi.hurt1");
    public static final RegistryObject<SoundEvent> ENTITY_DIMI_HURT2 = build("entity.dimi.hurt2");
    public static final RegistryObject<SoundEvent> ENTITY_DIMI_DEATH1 = build("entity.dimi.death1");
    public static final RegistryObject<SoundEvent> ENTITY_DIMI_DEATH2 = build("entity.dimi.death2");
    //...

    private static RegistryObject<SoundEvent> build(String id)
    {
        return SOUNDS.register(id, () -> new SoundEvent(new ResourceLocation(Main.MOD_ID, id)));
    }
}

转到我们的生物类中,将我们的声效与其绑定:


//死亡音效
   @Override
    protected SoundEvent getDeathSound() {
        Random ran = new Random();
        int co = ran.nextInt(3);
        //随机数来决定播放哪一个死亡音效
        if(co==0)
        {
            return SoundInit.ENTITY_DIMI_DEATH1.get();
        }
        else if(co==1) return SoundInit.ENTITY_DIMI_DEATH2.get();
        return SoundEvents.BEE_DEATH;
    }

    //平时音效(玩家站在生物旁边听到的音效)
    @Override
    protected SoundEvent getAmbientSound() {
        Random ran = new Random();
        int co = ran.nextInt(2);
        if(co==0)
        {
            return SoundInit.ENTITY_DIMI_AMBIENT1.get();
        }
        return  SoundInit.ENTITY_DIMI_AMBIENT2.get();
    }

    //受到攻击的音效
    @Override
    protected SoundEvent getHurtSound(DamageSource source) {
        Random ran = new Random();
        int co = ran.nextInt(2);
        if(co==0)
        {
            return SoundInit.ENTITY_DIMI_HURT1.get();
        }
        return SoundInit.ENTITY_DIMI_HURT2.get();
    }

在Main类中的Main函数中添加我们声音事件进行注册:


public class Main {
    public Main() {
        IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();

        ItemInit.ITEMS.register(bus);
        BlockInit.BLOCKS.register(bus);
        EntityInit.ENTITY_TYPES.register(bus);
        //添加这个
        SoundInit.SOUNDS.register(bus);
        MinecraftForge.EVENT_BUS.register(this);
    }
}

4.保存所有文件 -> 进入游戏测试。




麦陈Bingkler
不用代码框简直就是缺德....(小声bb)

Chenxin319
好东西,感谢分享

第一页 上一页 下一页 最后一页