Skip to main content
Inspiring
March 30, 2016
Question

variable updates in the next frame so life never decreases

  • March 30, 2016
  • 1 reply
  • 502 views

i want my game 2 times trail . i can't replay once my game reaches to 0.

look the below code will never fill the goal and life never decreases . because once it goes to frame2 after executing  game logic in frame1 . the value of life becomes 2 again . it never decreases. Any idea or suggestion to fix the issue ?. 

// frame2

var life:Number=3;

  life=life-1;

  lifetxt.text=String(life);

  trace(life);

  

if(life!=0) {

Replay_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_44);

function fl_MouseClickHandler_44(event:MouseEvent):void

{

  gotoAndStop("frame1");

life=life-1;

  }

}

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
March 30, 2016

If you are repeating this code in each frame then the value will always start as 3 then immediately become 2.  If you want the value to remain what it was already then you need to only declare it once.

var life:Number;

if(life == null) life = 3;

Inspiring
March 30, 2016

Ned Murphy sir

i could not implement as your code . anyhow for better idea i have attached screenshot 

actually my game start is in "frame1" and ends "scoreframe" .. so the life code is in score frame . so it sends the code to gotoAndStop("frame1");

after executing the frame2, 3,4 it comes and score frame again and updates life=3 again . So the values 2 never decreases . any idea to fix the issue ?

Ned Murphy
Legend
March 30, 2016

Yes, I already provided an answer for that.  Do not keep declaring the value = 3 every time you enter a/the frame. Use the code shown below in 1 frame only.  Change the score value wherever you wish but only declare it once and do so without assigning it a value...

var score:Number;     // this declares the variable but it does not have a numeric value yet

if(isNaN(score)) score = 3;  // this checks to see if the score has a numeric value and if not it assigns it one.  If it does, even zero, then it leaves it alone.

This way the code will not reassign a new value to the score each time you move to a frame with score code in it.