Copy link to clipboard
Copied
Hello, I'm new to coding in Adobe Animate and I'm currently trying to make a counter for a simple game I'm making for goofs. Anyways the code below is what I'm using for a counter at the moment it works if there is only one frame but not if the frame goes on for more then that because if it does then the counter will reset to 0 instead of keeping the older value when it goes back to frame one in the test window. I would appreciate it a lot if anyone could tell me why this happens and if you could direct me in the right direction to solving this. Thank you so much.
extra info that might help:
//code starts below this\\
import flash.events.MouseEvent
//This is I initiated the score variable
var score:Number = 0;
//This I belive add a listen event to "Button" for when it clicks
Button.addEventListener(MouseEvent.CLICK, addPoint);
//This I belive is what makes the score apper on screen
function addPoint(event:MouseEvent)
{
scoretext.text = new String (score);
score = score + 1;
}
to limit code execution to once only:
var alreadyExected:Boolean
if(!alreadyExecuted){
alreadyExecuted=true;
// code you want to limit
var score:Numbter=0;
}
[moved from ActionScript 1 and 2 to ActionScript 3]
Copy link to clipboard
Copied
to limit code execution to once only:
var alreadyExected:Boolean
if(!alreadyExecuted){
alreadyExecuted=true;
// code you want to limit
var score:Numbter=0;
}
[moved from ActionScript 1 and 2 to ActionScript 3]
Copy link to clipboard
Copied
Hey, I really appreciate the reply it worked like a charm! for anyone else who wants to know here my new edited code, it works for games that expands to multiple frames.
//code starts here\\
import flash.events.MouseEvent
var alreadyExecuted:Boolean;
if(!alreadyExecuted)
{
alreadyExecuted=true;
// The code you want to limit
Button.addEventListener(MouseEvent.CLICK, addPoint);
function addPoint(event:MouseEvent)
{
scoretext.text = new String (score);
score = score + 1;
}
var score:Number = 0;
}
also if you could possibly explain how limiting the code made this work I would greatly appreciate it but you don't half to tho if you don't want to.
Copy link to clipboard
Copied
you're welcome.
p.s. your assignment of the variable score to zero is executed each time the playhead enters the frame with that code, unless it's in that if-statement. the button listener and named function definition is not re-executed so those don't actually need to be in the if-statement.