Copy link to clipboard
Copied
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++){ | ||||
| ||||
} |
1 Correct answer
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
...Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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());
}

