actionscript 3 - Action Script 3, character selection -


i'm making flash game assignment. before start, i'm not asking code, asking see how can make coding efficient. (and @ point can give me hints if wish)

in game player can choose 2 characters. when player chooses first character boolean becomes true , player able play.

but hate have literally put double code.

    private function keyup(e:keyboardevent):void      {          if (e.keycode == 38)         {             if (playprincess)             {                 upkey = false;                 caroline.standstill();             }             else             if (playsirtimmy)             {                 upkey = false;                 timmy.standstill();                          }         }     } 

for example, code can see if player playing timmy, "playsirtimmy" true, , if true , hope code speaks self.

here example.

    private function sirtimmymovement():void      {         if (playsirtimmy)         {             if (rightkey)             {                 timmy.moveright();             }              if (leftkey)             {                 timmy.moveleft();             }                     if (upkey)             {                 timmy.moveup();             }         }      }     private function playprincessmovement():void      {         if (playprincess)         {             if (rightkey)             {                 caroline.moveright();             }              if (leftkey)             {                 caroline.moveleft();             }                     if (upkey)             {                 caroline.moveup();             }         }      } 

now i'm not complaining or moaning, love coding , fun/challenging, want know if there way shorten code, , make efficient.

thank time.

it's that, when comes collision , enemies things become tedios , harder.

edit: martyn

    private var character:player;     private var timmy:sirtimmy;     private var caroline:princess;       public function main()      {         //iniation players         character = new player;         timmy = new sirtimmy;         caroline = new princess;         stage.addeventlistener(event.enter_frame, maingameloop)     }      private function chooseprincess(e:mouseevent):void      {         if (e.target == princessbtn)         {             //playprincess = true;             level1check();             character = caroline;          }     }      private function startlevel1():void      {         if (easymode)         {             //set variables          }          if (medmode)         {             //you askng         }          if (hardmode)         {             //set variables harder!         }           stage.addchild(character);            } 

thank you, works charm!

rather boolean, have single property store character have selected:

private var player:player; 

when select character, update property:

player = timmy; // or caroline 

this way, code needs refer , can far more generalized:

private function movement():void  {     if(player === null)     {         // don't run function if neither character has been selected yet.         return;     }      if(rightkey) player.moveright();     if(leftkey) player.moveleft();           if(upkey) player.moveup(); } 

Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -