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

Counter keeps resetting to 0 at frame one

New Here ,
Feb 11, 2019 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;

}

TOPICS
ActionScript

Views

274

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
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 14, 2019 Feb 14, 2019

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]

Votes

Translate

Translate
Community Expert ,
Feb 14, 2019 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]

Votes

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
community guidelines
New Here ,
Feb 18, 2019 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.

Votes

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
community guidelines
Community Expert ,
Feb 19, 2019 Feb 19, 2019

Copy link to clipboard

Copied

LATEST

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.

Votes

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
community guidelines