Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 03-02-2017, 06:29 PM   #1
Registered User
Join Date: Mar 2017
Posts: 3
Default Help with falling objects loop


I want to make a game where you as a player have to avoid objects falling from the sky. I already have the movement code, but i have a question regarding the falling objects.

I have this code:

var numOfObjects:int = 6;
var fallingObjectArray:Array = [];
var speed:Number = 3;

// Add 10 falling objects to the display and to the array
for(var i:int = 0; i < numOfObjects; i++) {
   var fallingObject:Sprite = new Sprite();
   fallingObject.graphics.drawCircle(0, 0, 15);
   fallingObject.x = Math.random() * stage.stageWidth;

addEventListener(Event.ENTER_FRAME, moveDown);

function moveDown(e:Event):void
   // Go through all the objects in the array and move them down
   for each(var fallingObject in fallingObjectArray) {
      fallingObject.y += speed;
      // If the object is past the screen height, remove it from display and array
      if(fallingObject.y+fallingObject.height >= stage.stageHeight) {
         fallingObjectArray.splice(fallingObjectArray.indexOf(fallingObject), 1);
   // Once all objects have fallen off the screen, remove the listener
   if(fallingObjectArray.length <= 0) {
      removeEventListener(Event.ENTER_FRAME, moveDown);
It's perfect for my game, but: in each level i want this code to happen like 5 times. How can i do this? I already copy-pasted it and put it on another frame, but ofcourse it says that the functions already have been declared. (even tho i removed the "var" etc.)

Is there another way to make this code loop every 2 seconds? Thanks a lot.
If things are unclear, tell me.

Not sure if this topic was supposed to be posted in Actionscript 3 or newbie help.
Jiftyj is offline   Reply With Quote

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

All times are GMT. The time now is 10:25 PM.

Follow actionscriptorg on Twitter


Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
Help with falling objects loopHelp with falling objects loop
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.