Copy link to clipboard
Copied
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;
}
}
Copy link to clipboard
Copied
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;
Copy link to clipboard
Copied
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 ?
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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;
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now