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

Using loops in Actionscript 3

New Here ,
Mar 02, 2017 Mar 02, 2017

Hey, I want to make a loop in Actionscript 3 so when I activate a boolean for example, my numberOfCookies would start to add 1 score every second.

My current script if it will help.

var numberOfCookies: Number = 1

circle.addEventListener(MouseEvent.CLICK, plus)

  function plus(e: MouseEvent) {

  score.text = new String(numberOfCookies)

  numberOfCookies += 1

  }

  function init(): void {

  box.addEventListener(MouseEvent.CLICK, shop)

  }

  function shop(event: MouseEvent): void {

  if (numberOfCookies >= 6) {

  numberOfCookies -= 5

  }

  }

  init();

At the moment I have gone as far as doing the loop although it loops it instantly and I cannot make it so it loops it an infinite amount of times. This is the code:

for(numberOfCookies; numberOfCookies<100; numberOfCookies++){
numberOfCookies += 1;
}
TOPICS
ActionScript
3.3K
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

correct answers 1 Correct answer

LEGEND , Mar 03, 2017 Mar 03, 2017

Frame loops involve using gotoAndPlay commands when you reach particular frames.  the looping would be a command to revisit a frame that you just traveled from... such as playing frames 1 thru 5 and then at frame 5 you have a gotoAndPlay(1) command.

ENTER_FRAME loops involve using an ENTER_FRAME event listener.  What you do there is assign an event listener for it and it acts causes the execution to behave as if it is constantly re-entering a frame, at the frame rate of your Flash file, re-execut

...
Translate
Community Expert ,
Mar 02, 2017 Mar 02, 2017

for-loops, while-loops and do-loops execute from beginning to end before anything is updated on stage.

you want to use frame-loop, enterframe loop or timer loop.

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
New Here ,
Mar 03, 2017 Mar 03, 2017

Can you show me an example of a frame-loop in my script? I am still very new with Actionscript there are still new functions that I do not know yet.

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 03, 2017 Mar 03, 2017

Frame loops involve using gotoAndPlay commands when you reach particular frames.  the looping would be a command to revisit a frame that you just traveled from... such as playing frames 1 thru 5 and then at frame 5 you have a gotoAndPlay(1) command.

ENTER_FRAME loops involve using an ENTER_FRAME event listener.  What you do there is assign an event listener for it and it acts causes the execution to behave as if it is constantly re-entering a frame, at the frame rate of your Flash file, re-executing all of the code that is in the listener's event handler.

addEventListener(Event.ENTER_FRAME, doStuffConstantly);

function doStuffConstantly(evt:Event):void {

               // whatever action you define in here will repeat at the frame rate of your file

}

A Timer loop will behave similarly to the ENTER_FRAME except that you control the speed of repitition by the repeat rate you assign to the Timer object.  Look up "AS3 Timer tutorial" thru Google for an example

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
New Here ,
Mar 09, 2017 Mar 09, 2017

That is a very useful reply and so close to perfection! Is there any chance that you could make the script loop every second instead of every frame?

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
Community Expert ,
Mar 09, 2017 Mar 09, 2017
LATEST

a timer loop is capable of coming close to 1 second loops,

var t:Timer=new Timer(1000,0;

t.addEventListener(TimerEvent.TIMER,tF);

t.start();

function tF(e:TimerEvent):void{

trace(getTimer());

}

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