Skip to main content
eimantask9510943
Participant
March 2, 2017
Answered

Using loops in Actionscript 3

  • March 2, 2017
  • 1 reply
  • 3663 views

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;
}
This topic has been closed for replies.
Correct answer Ned Murphy

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

1 reply

kglad
Community Expert
Community Expert
March 3, 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.

eimantask9510943
Participant
March 3, 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.

Ned Murphy
Ned MurphyCorrect answer
Legend
March 3, 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