ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Dynamically Changing States | 305

Now that all of the basic programs are built, the real work begins. In the following


section, you will be building several movie clips to represent two spaceships, their


weapons, and a space station with its weapon. Most importantly though, the actions


of the different elements will generate state changes processed by the Observer


design pattern and sent to subscribing combatants.


}

}

//Set the score -- Accumulated score and damage
public function setScore (score:Number,damage:String):void
{
this.score=score;
this.damage=damage;
notifyObserver (score,damage);
}
}
}


Example 8-18. ConcreteObserver.as


package
{
//Concrete Observer
class ConcreteObserver implements Observer
{
public var nomDeGuerre:String;
private var damage:String;
private var score:Number;
private var concreteObserver:Subject;


function ConcreteObserver(concreteObserver:Subject)
{
this.concreteObserver=concreteObserver;
concreteObserver.subscribeObserver(this);
}


//Output to observer
public function passOn( ):String
{
return "Current score: "+score+"\nCurrent damage: "+ damage;
}


//Trap changes in state from subject
public function update(score:Number,damage:String):void
{
this.score=score;
this.damage=damage;
passOn( );
}
}
}


Example 8-17. ConcreteSubject.as (continued)

Free download pdf