Passing data between views and models in Flex MVC framework -


i have never used flex/actionscript before excuse me if i'm asking obvious have been working on 3 days (includes searching google , stackoverflow) before writing post.

i have been asked modify game written in flex 4 made mini-ioc-based mvc framework. 2 mods have been asked make game on screen, displays final score , difficulty selection on introduction screen. have made game on screen, , managed controller bring when game timer runs out.

so now, game structure goes:

intro view -> game view -> game on view ^ | |__________ (retry?) ________| 

the first problem getting score passed maingame actionscript file game on screen.

things have tried:

  • importing maingame in gameoverviewbase , calling maingame.score variable in gameoverview mxml file.

-edit!!! = above method works if change score variable in maingame constant, if remains variable, controller won't load gameoverview , game sits @ empty maingame view.

  • making function adds new score variable in gameoverviewbase whenever player scores during game.
  • passing score parameter gameoverview when maingame ends

controller.loadgameover(score);

this seemed logical way go it. followed function through other components of game setting loadgameover take integer parameter until got main game actionscript file:

public function loadgameover(score:int) : void { loadview(gameoverview); } 

the loadview function (shown below) stuck because can't see pass 'score' parameter. looks this:

private function loadview(viewclass:class, modelclass:class = null) : void { var view:view = new viewclass(); if(!view) throw new error("could not load view"); viewcontainer.removeallelements(); viewcontainer.addelement(view); view.controller = this; } 

the second problem difficulty selection on introduction screen. have done 3 buttons (easy, normal, hard) in mxml file , every button in actionscript:

protected function oneasybuttonclick() : void { set = "easy" controller.loadmaingame(set); } 

once again, end @ above loadview function.

to sum up: need know how pass data between views , models. if that's not ideal method, open other methods think better.

thank you!

ben

p.s. can send source code :)

you don't specify mvc framework you're using helpful. however, score should property of model , model data should accessible view either directly, perhaps via binding (thanks weltraumpirat), or via intermediary class.

i suggest have @ of existing view classes , try figure out how fed model data. can use approach data need view.

[edit]:

the maingame property not being set on gameoverview instance you're unable access score property either through binding or through trace. loadview method of controller class accepts model class reference uses construct new model instance used new view. unfortunately no use gameoverview needs instance of maingame created maingameview (and contains current score).

i don't know if following fits philosophy of framework you're using. however, change loadview method accept instance of model rather class reference, , create , cache reference instance of maingame when controller instantiated. way can pass same model reference both maingameview , gameoverview when these created.

 public class whackamolebase extends application implements igamecontroller { public var viewcontainer:group; private var maingame:maingame public function whackamolebase() : void { super(); // create , cache instance of main game model maingame = new maingame(); addeventlistener(flexevent.creation_complete, oncreationcomplete); } public function loadintroduction() : void { loadview(introductionview); } public function loadmaingame() : void { loadview(maingameview, maingame); } public function loadgameover() : void { // use same instance of maingame maingameview // has been using model gameoverview loadview(gameoverview, maingame); } // accept model instance rather class reference private function loadview(viewclass:class, model:model = null) : void { //create new instance of supplied view var view:view = new viewclass(); if(!view) throw new error("could not load view"); //clear previous views in container , add viewcontainer.removeallelements(); viewcontainer.addelement(view); //property based dependency injection view.controller = this; //there may or may not model required //requested view; check , instantiate appropriately if(model) { //give model reference controller //and link view model model.controller = this; view.model = model; } } private function oncreationcomplete(event:flexevent) : void { //when application first created, want show introductory view loadview(introductionview); } } 

Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -