Highlighted

Counter keeps resetting to 0 at frame one

New Here ,
Feb 11, 2019

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:

  • I'm using ActionScript 3.0
  • The way my game is set up is that every time you click the screen it goes back to frame 1 of a graphic symbol
  • I'm trying to get it where every time you hit the button or it passes frame 1 you get a point
  • My code is still a work in progress so feel free to make fun of it if you wish to

//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;

}

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

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]

TOPICS
ActionScript

Views

134

Likes

Translate

Translate

Report

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

Counter keeps resetting to 0 at frame one

New Here ,
Feb 11, 2019

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:

  • I'm using ActionScript 3.0
  • The way my game is set up is that every time you click the screen it goes back to frame 1 of a graphic symbol
  • I'm trying to get it where every time you hit the button or it passes frame 1 you get a point
  • My code is still a work in progress so feel free to make fun of it if you wish to

//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;

}

Adobe Community Professional
Correct answer by kglad | Adobe Community Professional

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]

TOPICS
ActionScript

Views

135

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Feb 11, 2019 0
Adobe Community Professional ,
Feb 14, 2019

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]

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 14, 2019 0
New Here ,
Feb 18, 2019

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 18, 2019 0
kglad LATEST
Adobe Community Professional ,
Feb 19, 2019

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Feb 19, 2019 0