Hallo zusammen,
Ich entwickle seit einiger Zeit meine Spiele mit PureMvc As3.
Leider bin ich mir nicht ganz sicher, wie man den GameLoop am besten integriert.
Im Moment habe ich einen EnterFrame im ApplicationMediator View, der eine ON_ENTER_FRAME Notification sendet und damit einen Controller GameLoopCommand auslöst. Der GameLoopController berechnet die erste Zeit mit getTime(), fragt dann die Collisions ab und bewegt den Player. Danach berechnet er die vergangene Zeit und speichert diese in den TimeProxy.
Ich bekomme nur bei dieser Methode oft framedrops, daher habe ich die Befürchtung, dass dieser Weg zu rechenintensiv ist.
Meine Frage ist ob das so s.u. überhaupt dem MVC Prinzip entspricht? oder doch eher der EnterFrame TICK aus dem TimerProxy kommen sollte und intressierte Mediator die TICK Notification verarbeiten lassen, statt alles im Controller zu verarbeiten.
PHP-Code:
facade.registerCommand(ApplicationFacade.ON_ENTER_FRAME, GameLoopCommand);
Im Main View den EnterFrame Bang adden
PHP-Code:
public class ApplicationMediator extends Mediator implements IMediator
{
public static const NAME:String = "ApplicationMediator";
public function ApplicationMediator( viewComponent:Sprite)
{
trace(this+" construct() "+viewComponent);
super( NAME, viewComponent);
}
override public function listNotificationInterests():Array
{
return [
ApplicationFacade.START_GAME
]
}
override public function handleNotification(notification:INotification):void
{
switch( notification.getName() )
{
case ApplicationFacade.START_GAME:
viewComponent.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
break;
}
}
private function onEnterFrame(e_:Event):void {
sendNotification(ApplicationFacade.ON_ENTER_FRAME);
}
}
}
Im Controller Zeit berechnen, Controller für die Gamelogic aufrufen und Zeit speichern
PHP-Code:
public class GameLoopCommand extends SimpleCommand implements ICommand{
override public function execute(notification:INotification):void
{
var _timeProxy:TimeProxy = facade.retrieveProxy(TimeProxy.NAME) as TimeProxy;
// zeit die seit dem letzten aufruf des controllers vergangen ist
if (_timeProxy.lastTime == 0) _timeProxy.lastTime = getTimer();
var timeDiff:int = getTimer()-_timeProxy.lastTime;
_timeProxy.lastTime += timeDiff;
//Game Logic Check Walls, Move Hero, Move Enemy etc...
sendNotification(ApplicationFacade.MOVE_HERO, timeDiff);
sendNotification(ApplicationFacade.CHECK_COLLISIONS);
//Zeit im Proxy speichern
_timeProxy.time=getTimer();
}
}
Zeit im Model von ms in String wandeln und den View updaten
PHP-Code:
public class TimeProxy extends Proxy implements IProxy
{
public static const NAME:String = "TimerProxy";
private var _time : Number;
private var _lastTime:int=0;
public function TimeProxy()
{
super ( NAME);
}
public function set time(ms_:int):void
{
_time=ms_;
if(_time<=0)
sendNotification(ApplicationFacade.GAME_OVER);
else
sendNotification(ApplicationFacade.UPDATE_TIME, timeString(_time));
}
public function get time():int
{
return _time;
}
public function get lastTime():int{
return _lastTime;
}
public function set lastTime(ms_:int):void{
_lastTime=ms_;
}