Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

variable updates in the next frame so life never decreases

Contributor ,
Mar 29, 2016 Mar 29, 2016

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;

  }

}

TOPICS
ActionScript
457
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 30, 2016 Mar 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 30, 2016 Mar 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 ?

yes.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 30, 2016 Mar 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Mar 30, 2016 Mar 30, 2016
LATEST

this code shows my dynamic textField "NAN"

var life:Number;

lifetxt.text=String(life);

if(isNaN(life)) life=3;

{

refresh_btn.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler_44);

function fl_MouseClickHandler_44(event:MouseEvent):void

{

gotoAndStop("ClassRoom");

life=life-1;

}

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines