actionscript 3 - Send variables back to the constructor function (AS3) -


i hope can ask in way make sense.

i'm not sure if title question need do. here goes nothing.

i'm attemping create game, i'm creating 2d array sets out "map".

stage.addchild(new grid5(stage,g1a,20,20)); 

so stage stagereference, g1a "grid1array" , x , y. inside grid5 run loops check values of in array, if finds 2 creates new "player"

player sets on map , adds keyboard event listeners, in game there 2 or 3 maps, , therefore there 2 or 3 players (1 on each map). fine, works, players need spot on map, , when set boolean true. each player has it's own boolean... need find way check 3 players booleans set true know game finished.

so have function called finished(). problem is, if put in player class, can check it's own boolean, , means when 3 of players gets end, game end. thought need pass result of boolean constructor , loop through players, i'm not sure how reference them created in grid class.

fist way

how stage add event listener(or can add event listen on parent of player).

stage.addeventlistener("finish", onplayerfinish);  private function onplayerfinish(e:event):void {     var player:object = e.target; } 

how player dispatch event

player.dispatchevent("finish", true, false); 

assume got class named main control things.

  1. when create player, dispatch event inform new player had been add, in main can use dictionary keep players, dic[player1] = false, dic[player2] = false, false means player hasn't finished, true means finished.

  2. when 1 player finisheds, can dispath event inform played has finished, , set dic[targetplayer] = true. loop check dic , game finishes when values true.

a link bubble bubble flow

another way

use global variable save player state(not elegant way). , can update global variable through set function, check player state when update function called.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -