1 Reply Latest reply on Mar 10, 2011 6:21 AM by kglad

    Problem using a Boolean variable

    gherome_gherome Level 1



      I'm writing a game in which the player and 'enemies' fall off the screen when they 'die'. The problem is that I also want to constrain their  movement to within the screen boundaries whenever they have not been killed. To start  I thought I'd go into the class I wrote to handle the player falling off the screen when he's touched by an enemy and create a boolean variable so that Flash would know to let him fall off the screen when killed, but remain constrained on the screen until that point.The problem is that now the player stays on the screen even when I want him to fall off (i.e. after he's been hit).


      The class I wrote to handle the player dying is below, and the boolean variable is called 'playerBeenHit'. Since the player only gets a y acceleration when hit, I tried using the 'ay' variable to control the behavior but with the same results. In troubleshooting I tried to trace(playerBeenHit) on every frame and noticed it would go to 'true' and then set itself back to 'false' for three out of four readouts even after the player was hit. I'm not sure what I'm doing wrong here.












      package  jab.enemy




          import flash.display.MovieClip;


          import flash.events.Event;


          import flash.geom.ColorTransform;


          public class DieLikeAPlayer




              private var _clip1:MovieClip; // the 'player'


              private var _clip2:MovieClip; // the 'enemy' who kills the 'player' on contact.


              private var ay = 0; // vertical acceleration for falling off the screen.


              private var vx = 0; // horizontal initial velocity, 'knockback' from the impact.


              private var vy = 0; // vertical initial velocity, 'knockback' from the impact.


              private var playerBeenHit:Boolean = false;




              public function DieLikeAPlayer(clip1:MovieClip, clip2:MovieClip): void




                  _clip1 = clip1;


                  _clip2 = clip2;


                  _clip1.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame)


                  function onEnterFrame(event:Event):void




                      if (_clip1.hitTestObject(_clip2)) // what happens when the player gets killed.




                          playerBeenHit= true;


                          ay = 3;


                          _clip1.scaleY = -Math.abs(_clip1.scaleY);


                          _clip1.alpha = 0.4;




                          vx = -0.2;


                          vy = 2;


                          _clip1.y += 5;


                          _clip1.rotation = -45








                      // adding y acceleration and x velocity (knockback) from being hit.


                      _clip1.x += vx;


                      _clip1.y += vy;


                      vy += ay;




                      if(playerBeenHit == false)//constrains the player from moving past the  bottom of the screen.




                           if(_clip1.y > 620)


                           {_clip1.y = 620;}




                      if (_clip1.y > 100000) // brings the player 'back to life' after falling 100,000 pixels.




                          ay = vx = vy = 0;


                          _clip1.alpha = 1;


                          _clip1.scaleY = Math.abs(_clip1.scaleY);


                          _clip1.rotation = 0;




                          _clip1.x = 480;


                          _clip1.y = 300;


                          playerBeenHit = 0;