interactive scripting - CGR C/092 - fall 2004
Philip van Allen - v a n a l l e n @ a r t c e n t e r . e d u

room 142, Monday 4:00pm-7:00pm
all materials on this web site © copyright 2004, Philip van Allen
 
week 13a - state management

state management : 

 


example code: stateMgt.dir

In more complex applications, objects in the system need to keep track of their internal state and behave differently depending on that state. For example, in a car, different things work depending on the position of the key:

  • Key OFF: lights work, nothing else works
  • Key AC: lights work, radio and other electrical works, engine off
  • Key STARTER: starter motor turns
  • Key RUNNING: engine runs, everything works

In software, taking care of these different states and corresponding behaviors is called "STATE MANGEMENT". It simply means that the software:

  • keeps track of the current state
  • behaves according to this state
  • and (often) manages the trasitions from one state to the next

state example 1 : 

In this simple example, the sprite upper sprite object pays attention to where it is in the horizontal (.locH) dimension, and shows a different word and plays a different sound depending if it is in the left, middle or right hand side when the user clicks on it.

function mouseDown() {
xPos = sprite(this.spriteNum).locH;
theSprite = sprite("textMessage");

if (xPos < 200) {
// left side
//
theSprite.member = member("more");
sound(1).play(member("seagulls"));
} else if (xPos >= 200 && xPos < 400) {
// middle
//
theSprite.member = member("thanks");
sound(1).play(member("bark"));
} else if (xPos >= 400) {
// right side
//
theSprite.member = member("ouch");
sound(1).play(member("mosquito"));
}
}
function mouseUp () {
   theSprite = sprite("textMessage");
   theSprite.member = member("hello");
}
function mouseLeave () {
   theSprite = sprite("textMessage");
   theSprite.member = member("hello");
}
         
function enterFrame () {
   thisSprite = sprite(this.spriteNum);
   thisSprite.locH += 2;
   if (thisSprite.locH > 600) thisSprite.locH = 0;
} 
  

 

 
state example 2 :  

In this example, the state is managed within the sprite rather than by the sprite's position on the screen. Each time the user clicks, the sprite shows a word and plays a sound corresponding to its current state. It also advances itself to the next state with a user click. So each click causes the sprite to change from one state to the next.

currentState = 1;
member("currentState").text = currentState.toString();
function mouseDown() {
             
   theSprite = sprite("textMessage");
   
   if (currentState == 1) { 
     // left side
     //
     theSprite.member = member("more");
     sound(1).play(member("seagulls"));
   } else if (currentState == 2) {
     // middle
     //
     theSprite.member = member("thanks");
     sound(1).play(member("bark"));
   } else if (currentState == 3) {
     // right side
     //
     theSprite.member = member("ouch");
     sound(1).play(member("mosquito"));
   } 
   currentState++
   if (currentState > 3) currentState = 1;
}
function mouseUp () {
   theSprite = sprite("textMessage");
   theSprite.member = member("hello");
   member("currentState").text = currentState.toString();
}
function mouseLeave () {
   theSprite = sprite("textMessage");
   theSprite.member = member("hello");
   member("currentState").text = currentState.toString();
}
         
function enterFrame () {
   thisSprite = sprite(this.spriteNum);
   thisSprite.locH += 2;
   if (thisSprite.locH > 600) thisSprite.locH = 0;
} 
 

all materials on this web site © copyright 2004, Philip van Allen

top