How to code the sequence of states in a game/app

Having come from a Flash AS2 background with a significant amount of my work being educational games that contain significant elements of voice over sequences, I could do with advice on how to structure my code without the use of a timeline (apologies to those not familiar with a timeline).

For example, one such game I am porting has a sequence as follows:


Step 1:
Play audio file 0010
Character wave hands
Display another character in a tree

Step 2: to be actioned when audio file 0010 has finished playing
Play audio file 0020
Character stamp feet
Hide character in tree
Play sound effect 1

Step 3: to be actioned when audio file 0020 has finished playing

Play audio file 0030
Character point
Display button

This sequence continues for another 12 steps before we get to the actual game interaction and at any point the user can click a help button to return to the beginning of the sequence.

So my question is basically how would you recommend coding such a flow of events.

I am thinking it could be done with a large switch statement that branches based on a state variable whereby I pass the next state to the play audio function so it can change the state when the audio is complete.

1
2
3
4
5
6
7
8
9
10
if state == "0010" then
 
 playAudio("0010", "0020")
 animateCharacter("character1", "wave")
 animateCharacter("character2", "show")
 
elseif state == "0020"
 
 
end

@mediakitchen, glad that you have a very good presented question, unfortunately....

you have answered your own question, read up on OpenAL audio.play

you will find callbacks that you can use that can be triggered on completion of playing a sound file, that will be the trigger for your next sound file and so on...

You *can* have a state engine that changes state on completing certain tasks. This is the best way you can achieve a series of things. You will also have to determine the element/point that triggers the change of state.

cheers,

?:)

Thanks JayantV

I am pleased that I was going in the right direction - it just seemed quite lengthy to have such a huge if else statement. I suppose I could use an associative array with the state ID as the index too.

I did try researching this and came across alot of articles about stuff like finite state machines and MVC design patterns so wanted to be sure I wasn't missing out on some tricks with regards setting off on the right footing.

Thanks for the reply :)

there are many ways of dealing with that. Now I cannot recommend one over the other as it is a matter of preference.

You definitely do not need a MVC, but I can give you a hint on what I would do...

1
2
3
4
5
6
7
local function performNextStep(stepIndex)
  if stepIndex == 1 then
   doStep1()
  else
   doStep2()
  end
end

Thanks a million JayantV - that looks like a good starting point.

Thank you for the swift assistance:)

views:1224 update:2011/10/18 8:54:01
corona forums © 2003-2011