Author Topic: Play sound when starting game?  (Read 4195 times)

wrybread

  • Sr. Member
  • ****
  • Posts: 100
    • View Profile
Play sound when starting game?
« on: November 16, 2017, 05:29:31 PM »
I'm trying to play a different sound when I switch selected ROM, switch display and start a ROM. All works well with the below code except starting a ROM. Is the transition not "ToGame"?

Code: [Select]
function fade_transitions( ttype, var, ttime ) {


switch ( ttype ) {
  case Transition.ToNewList:
local Wheelclick = fe.add_sound("sounds/teleport.wav")
Wheelclick.playing=true
return false;
break;
  }

switch ( ttype ) {
  case Transition.ToGame:
  local Wheelclick = fe.add_sound("sounds/boom.wav")
Wheelclick.playing=true
break;
  }

switch ( ttype ) {
  case Transition.ToNewSelection:
  ;case Transition.ToNewList:
local Wheelclick = fe.add_sound("sounds/clicked.wav")
Wheelclick.playing=true
break;
  }


return false;
}

fe.add_transition_callback( "fade_transitions" );



« Last Edit: November 16, 2017, 06:00:33 PM by wrybread »

jedione

  • Hero Member
  • *****
  • Posts: 1135
  • punktoe
    • View Profile
Re: Play sound when starting game?
« Reply #1 on: November 16, 2017, 06:49:30 PM »
it;s because entering a rom is not a wheelclick...

this should work for you. just change the sound file name, but i think you know that.

Code: [Select]
function togame( ttype, var, ttime ) {
 switch ( ttype ) {
   case Transition.ToGame:
     local sound = fe.add_sound("intro.mp3");
sound.playing=true;
  break;
  }
 return false;
}
fe.add_transition_callback( "togame" );
help a friend....

keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Play sound when starting game?
« Reply #2 on: November 16, 2017, 09:20:56 PM »
You can use one switch with multiple case statements as well, to clean up your code and DRY (don't repeat yourself).

wrybread

  • Sr. Member
  • ****
  • Posts: 100
    • View Profile
Re: Play sound when starting game?
« Reply #3 on: November 16, 2017, 11:26:31 PM »
Thanks for the tip on cleaning up the switch statement. I think this is a better way to write it, but I'm still not sure I understand the "break" statements, and whether I need one in each case:

Code: [Select]
function fade_transitions( ttype, var, ttime ) {

switch ( ttype ) {

  case Transition.ToGame:
  local sound = fe.add_sound("sounds/Burgertime_Dies.wav");
sound.playing=true;
break;

  case Transition.ToNewList:
local Wheelclick = fe.add_sound("sounds/Asteroids_Tone02.wav");
Wheelclick.playing=true;
break;

  case Transition.ToNewSelection:
local Wheelclick = fe.add_sound("sounds/clicked.wav");
Wheelclick.playing=true;
break;
  }


return false;
}


fe.add_transition_callback( "fade_transitions" );

But still no joy.

And substituting the whole block for the code that @jedione posted also doesn't work for me.

Any idea why?
« Last Edit: November 16, 2017, 11:47:55 PM by wrybread »

wrybread

  • Sr. Member
  • ****
  • Posts: 100
    • View Profile
Re: Play sound when starting game?
« Reply #4 on: November 17, 2017, 12:52:13 AM »
It works if I change the transition to FromGame (it then plays the sound when I return from the game), so I'm wondering if it's just not giving the sound enough time to play?

Can someone confirm that playing sounds with ToGame works?


keilmillerjr

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1167
    • View Profile
Re: Play sound when starting game?
« Reply #5 on: November 17, 2017, 07:30:11 AM »
It works if I change the transition to FromGame (it then plays the sound when I return from the game), so I'm wondering if it's just not giving the sound enough time to play?

Can someone confirm that playing sounds with ToGame works?

You need to wait until your sound is finished playing before returning the transition function. Look at my FadeToGame plugin. Maybe it help.

jedione

  • Hero Member
  • *****
  • Posts: 1135
  • punktoe
    • View Profile
Re: Play sound when starting game?
« Reply #6 on: November 17, 2017, 07:24:09 PM »
not shure why it dont work for you,  this is what i get with the
same code that i posted....

preview...https://youtu.be/gJMP5VHeAM8

so i would say ToGame is working.
« Last Edit: November 17, 2017, 07:25:46 PM by jedione »
help a friend....

8bitsdeep

  • Full Member
  • ***
  • Posts: 49
    • View Profile
Re: Play sound when starting game?
« Reply #7 on: November 19, 2017, 06:04:28 AM »
Thanks for the tip on cleaning up the switch statement. I think this is a better way to write it, but I'm still not sure I understand the "break" statements, and whether I need one in each case

"break" marks the end of each case. Without it, the code would continue on and execute the next case even if it wasn't a match.  Similar to the closing } on an if statement.