
Controller Class
import mx.events.EventDispatcher;
import net.tolgaozdemir.games.slotmachine.data.SlotMachine;
import net.tolgaozdemir.games.slotmachine.view.SlotView;
class net.tolgaozdemir.games.slotmachine.data.SlotMachineData extends EventDispatcher{
private var slot:SlotMachine;
private var dispatchEvent:Function;
public var removeEventListener:Function;
public var addEventListener:Function;
public var onRoll:Function;
public function get Slot():SlotMachine {
if(this.slot == null) this.roll();
return this.slot;
}
public function set Slot(value:SlotMachine) {
this.slot = value;
}
public function SlotMachineData() {
EventDispatcher.initialize(this);
this.addEventListener(this, "onRoll");
this.Slot = new SlotMachine();
}
public function roll():Void{
this.Slot.A = Math.round(Math.random()*3+1);
this.Slot.B = Math.round(Math.random()*4+1);
this.Slot.C = Math.round(Math.random()*5+1);
var LUCKY:Number = 5;
this.Slot.win = 0;
if( this.Slot.A==this.Slot.B && this.Slot.B==this.Slot.C && this.Slot.A==3){
this.Slot.win = 50 + this.Slot.bet;
}else if( this.Slot.A==this.Slot.B && this.Slot.B==this.Slot.C){
this.Slot.win = 20 + this.Slot.bet;
}else if( (this.Slot.A==LUCKY) || (this.Slot.B==LUCKY) || (this.Slot.C==LUCKY)){
this.Slot.win = 2 + this.Slot.bet;
}else {
this.Slot.win = this.Slot.win - this.Slot.bet;
}
this.Slot.score = this.Slot.score + this.Slot.win;
dispatchEvent({type:"onRoll", target:this});
}
}
When we look at the controller class, we see that it chances and updates the data and dispatch the event “onRoll” then the view will listen this event and update itself.
