actionscript 3 - Moving with keyboard (left and right) in flash -


i trying move avatar left , right in game stay within boundaries of stage (700x500), recieving error stating "public attribute can used inside package" placing public functions in wrong spot?

   package {     import flash.display.movieclip;     import flash.events.event;     import flash.utils.timer;     import flash.events.timerevent;     import flash.events.keyboardevent;     import flash.ui.keyboard;     public class spacevigilantegame extends movieclip      {         public var enemy:enemy;         public var avatar:avatar;         public var gametimer:timer;         public var usemousecontrol:boolean;          public var rightkeyisbeingpressed:boolean;         public var leftkeyisbeingpressed:boolean;          var gamewidth:int = 0;         var gameheight:int = 0;          public function spacevigilantegame()         {   usemousecontrol = false;             leftkeyisbeingpressed = false;             rightkeyisbeingpressed = false;             enemy = new enemy();             addchild( enemy );             avatar = new avatar();             addchild( avatar );              if ( usemousecontrol )             {                 avatar.x = mousex;                 avatar.y = mousey;             }             else             {                 avatar.x = 50;                 avatar.y = 400;             }              gamewidth = stage.stagewidth;             gameheight = stage.stageheight;               gametimer = new timer( 25 );             gametimer.addeventlistener( timerevent.timer, moveenemy );             gametimer.addeventlistener( timerevent.timer, moveavatar );             gametimer.start();             stage.addeventlistener( keyboardevent.key_down, onkeypress );             stage.addeventlistener( keyboardevent.key_up, onkeyrelease );          public function onkeypress( keyboardevent:keyboardevent ):void         {             if ( keyboardevent.keycode == keyboard.right )             {                 rightkeyisbeingpressed = true;             }         }         public function onkeyrelease( keyboardevent:keyboardevent ):void         {             if ( keyboardevent.keycode == keyboard.right )             {                 rightkeyisbeingpressed = false;             }         }         }         public function moveenemy( timerevent:timerevent ):void          {             //enemy.movedownabit();             if(enemy.x+enemy.width+2<=gamewidth)                 {                     enemy.moveright();                 }             else if(enemy.y+enemy.height+2<=gameheight)                 {                     enemy.movedown();                 }             else if(enemy.x-2>=0)                 {                     enemy.moveleft();                 }             else if(enemy.y-2>=0)                 {                     enemy.moveup();                 }          }         public function moveavatar( timerevent:timerevent ):void         {             if ( usemousecontrol )             {                 avatar.x = mousex;                 avatar.y = mousey;             }             else             {                 if ( rightkeyisbeingpressed )                 {                     avatar.moveright();                 }             }         }        } } 

avatar class:

package  {     import flash.display.movieclip;     public class avatar extends movieclip      {         public function avatar()          {             x = 50;             y = 400;          }          public function moveright()         {             x = x + 2;          }          public function moveleft()         {             x = x - 2;         }     } } 

you have 2 functions onkeypress() , onkeyrelease() defined public inside spacevigilantegame() constructor, that's why got error, avoid that, have remove public or, if want put them public, , don't think so, put them out of constructor of class.

hope can help.


Comments

Popular posts from this blog

cakephp - simple blog with croogo -

How to group boxplot outliers in gnuplot -

bash - Performing variable substitution in a string -