Differences

This shows you the differences between the selected revision and the current
version of the page.


wmebook:ch10 2007/12/10 19:56 wmebook:ch10 2008/01/10 14:12 current
Line 35: Line 35:
  * **Game.SetMusicVolume(volume);** sets new volume //(SetMusicChannelVolume(channel,volume)//   * **Game.SetMusicVolume(volume);** sets new volume //(SetMusicChannelVolume(channel,volume)//
-There are more methods but for now we'll be happy with those. Now what's the difference from using channel version? You can use more music tracks at the same time, you can crossfade them (one goes volume down while the other volume up for seamless transition).+There are more methods but for now we'll be happy with those. Now what's the difference from using channel versions? You can use more music tracks at the same time, you can crossfade them (one goes volume down while the other volume up for seamless transition).
Open the **scene_init.script** of the Dream scene and modify the beginning. Open the **scene_init.script** of the Dream scene and modify the beginning.
Line 58: Line 58:
We've first of all used the music channel 0 for the music playing, but we've also set the track to //looping// track by providing **true** as a looping value. We've first of all used the music channel 0 for the music playing, but we've also set the track to //looping// track by providing **true** as a looping value.
-For crossfading we use **Game.MusicCrossfade(channel1,channel2,time, swap);** which is a method decreasing volume of channel1 and increasing volume of channel2 in time. Swap means that what was in the channel1 gets to channel2 and vice versa so you after the crossfade operate with channel1 for the new track. Note that this is automatically **true**, so if you don't want the swap you have to specify **false** as the last parameter. +For crossfading we use **Game.MusicCrossfade(channel1,channel2,time, swap);** which is a method decreasing volume of channel1 and increasing volume of channel2 in time. Swap means that what was in the channel1 gets to channel2 and vice versa so new track is - after the crossfade - in channel1. Note that this is automatically **true**, so if you don't want the swap you have to specify **false** as the last parameter.
In this light, we'll open the Street scene and modify **scene_init.script** at the bottom to read: In this light, we'll open the Street scene and modify **scene_init.script** at the bottom to read:
Line 123: Line 123:
Let's leave music for a while and look at the sound designing. We've already seen how to trigger sound in the sprite editor and also that we can assign sounds to the entities in the Scene Editor. But here we'll look at ways how to use sounds from the scripts. Let's leave music for a while and look at the sound designing. We've already seen how to trigger sound in the sprite editor and also that we can assign sounds to the entities in the Scene Editor. But here we'll look at ways how to use sounds from the scripts.
 +
 +Create a folder called **sounds** in our trusty data folder and copy the resource sounds to it.
With sounds we have apart from volume also another possibility - we can adjust their panorama((by panorama I mean position in the stereo channels)) by attaching them to positioned entities. Open the scene Street in the Scene Editor and create a sprite entity called Fun. Turn off Interactive for it and shift it to position 1, 370. Also be sure that you have Sound Panning option set on for that entity. With sounds we have apart from volume also another possibility - we can adjust their panorama((by panorama I mean position in the stereo channels)) by attaching them to positioned entities. Open the scene Street in the Scene Editor and create a sprite entity called Fun. Turn off Interactive for it and shift it to position 1, 370. Also be sure that you have Sound Panning option set on for that entity.
Line 129: Line 131:
<code script> <code script>
 +var e = Scene.GetNode("Fun");
 +
 +e.PlaySound("sounds\drnci.ogg");
 +
 +for (var x=1; x<900;x=x+1)
 +{
 +  e.X = x;
 +  Sleep(5);
 +}
 +this.StopSound();
</code> </code>
 +As we could easily expect, we can play sound through **PlaySound** (similary to PlayMusic) and stop it through **StopSound** (Similary to StopMusic), we can **PauseSound** or **ResumeSound** - we've seen it before and the logic is the same as in the music but here we're doing more magic.
 +First we get a Node (our newly created sprite entity Fun) and attach sound to it. Then we move the entity on the screen and sound follows the position on the screen so it runs from left to right.
 +If we assigned any imagery to the sound, we could easily have a passing car followed by its sound. Neat, isn't it?
 +
 +But we've here uncovered something even more substantial! Sounds can be attached to any kind of object. So we can have Game.PlaySound, Scene.PlaySound, actor.PlaySound, entity.PlaySound etc. We are not limited by anything else than performance issues. This way you can make your game sound incredibly alive.
 +
 +Another very useful method is **IsSoundPlaying()** (which returns true or false - we'll make use of it soon enough).
 +
 +Just for the record as a very advanced feature we can set a realtime ambience for the sound, we have triplet of the functions:
 +
 +**SoundFXEcho**, **SoundFXReverb** and **SoundFXNone**, but usage is beyond the scope of this book. For inquisitive minds, it can be great to have one sound and then adding realtime ambience through WME rather than having multitude of sounds.
 +
 +But back to our modest needs. As a demonstration we'll handle a door. There are three states - locked door, unlocking the door and open door squeak.
 +
 +First we have to modify scene_init.script a bit to throw away again our moving sound and make the music lower as it's too loud.
 +
 +<code script>
 +if(!StateStreet.Visited)
 +{
 +  StateStreet.Visited = true;
 +  Game.PlayMusicChannel(1,"music\voyages.ogg",true);
 +  Game.SetMusicChannelVolume(1,50);
 +  Game.MusicCrossfade(0,1,6000);
 +}
 +
 +//End of the file.
 +</code>
 +
 +Then in the **door.script** we adjust the sounds:
 +
 +<code script>
 +#include "scripts\base.inc"
 +
 +global DoorUnlocked;
 +
 +on "LeftClick"
 +{
 +  Game.Interactive = false;
 +  actor.GoTo(695,344);
 + 
 +  if (!DoorUnlocked)
 +  {
 +    this.PlaySound("sounds\locked.ogg");
 +    while (this.IsSoundPlaying()) Sleep(1);
 +    actor.Talk("Rats! The door is locked. What should I do now?");
 +  }
 +  else
 +  {
 +    Game.PlaySound("sounds\doorsqueak.ogg");
 +    Game.ChangeScene("scenes\room\room.scene"); 
 +  }
 +  Game.Interactive = true;
 + 
 +}
 +
 +on "Lockpick"
 +{
 +  Game.Interactive = false;
 +  actor.GoTo(695,344);
 + 
 +  this.PlaySound("sounds\unlock.ogg");
 +  while (this.IsSoundPlaying()) Sleep(1);
 + 
 +  DoorUnlocked = true;
 +  Game.DeleteItem("Lockpick");
 +  actor.Talk("Ha! I've unlocked the door. The lockpick broke apart in the process though.");
 + 
 +  Game.Interactive = true;
 +}
 +</code>
 +
 +Starting from beginning we read:
 +
 +<code script>
 +  if (!DoorUnlocked)
 +  {
 +    this.PlaySound("sounds\locked.ogg");
 +    while (this.IsSoundPlaying()) Sleep(1);
 +    actor.Talk("Rats! The door is locked. What should I do now?");
 +  }
 +</code>
 +
 +We play sound of the locked door and wait for the sound to end so the player can comment on the locked door.
 +
 +<code script>
 +  else
 +  {
 +    Game.PlaySound("sounds\doorsqueak.ogg");
 +    Game.ChangeScene("scenes\room\room.scene"); 
 +  }
 +</code>
 +
 +Now this might need a bit of explaining. Everything in the Scene terminates with the ChangeScene and the sound would have been cut off if we attach it to door. But Game object lasts for the whole game and by attaching our sound to Game object, we make sure, that the sound is played while the scene is being changed. This also creates a great joining moment soundwise because the scene change suddenly becomes more vivid.
 +
 +And lastly our good old unlocking sound.
 +
 +<code script>
 + this.PlaySound("sounds\unlock.ogg");
 + while (this.IsSoundPlaying()) Sleep(1);
 +</code>
 +
 +We again wait for the sound to end and then go on. Remember this construction, you'll often use it! Of course you can also make sound loop exactly the same way as you loop music. Full function looks like **PlaySound(filename, Looping, Loop Start);**
 +
 +Sound design is quite a hard work. You can easily tell, that sounds from the resources pack don't match the environment at all. They are actually direct rip offs from Ghost in the Sheet, but for demonstrating principles they're more than suitable. Don't forget that propper sound design is at least 60% of atmosphere. Don't forget to add good ambient sounds (sounds of the night town would be good for the street for example with some crickets, ocassional dog barking etc.) and be very precise. With good sounds your game immediately jumps up in quality.
 
wmebook/ch10.1197312984.txt.gz · Last modified: 2007/12/10 19:56 by metamorphium
Recent changes RSS feed Creative Commons License Driven by DokuWiki