Forums → Programming Forum → setup?
4 | 6038 |
I want to have like apples and oranges and other fruit falling from the ceiling of my game and don't know how to structure it? I don't know how I would start this class or would it have to be structured differently?
package {
import flash.display.MovieClip;
public class FallingObjects extends MovieClip {
public var army:Array;
public var apples:apple;
public var gameTimer:Timer;
public function FallingObjects()
{
army = Array();
var newApple = apple(100, 30);
army.push (newApple);
addChild(newApple);
}
}
}
- 4 Replies
Firstly I see you have declared a timer but haven't imported the timer utilities.
import flash.utils.Timer;
personally I like to use the enterframe method, there are pro's and cons to using the timer vs enterframe, you can make up your own mind
package
{
import flash.display.*;
import flash.utils.*;
public class FallingObjects extends MovieClip
{
private var appleArray:Array;
private var gameTimer:Timer;
function FallingObjects():void
{
appleArray = new Array();
gameTimer = new Timer( 25 );
gameTimer.addEventListener(TimerEvent.TIMER, dropApples);
gameTimer.start();
}
private function dropApples( timerEvent:TimerEvent ):void
{
if ( Math.random() < 0.03 )
{
var randomX:Number = Math.random() * 500;
var apples:apple = new apple();
apples.x = randomX;
apples.y = 30;
appleArray.push(apples);
addChild(apples);
}
for each (apples in appleArray)
{
apples.y++;
}
}
}
}
this should get you started also I did not test
But I don't understand how I would start the class so it starts producing apples.
referencing my code you first create an array and a timer event
when the timer fires it does a random calculation
if ( Math.random() < 0.03 )
if the calculation is below .03 it'll create an apple at a random x location
var randomX:Number = Math.random() * 500;
var apples:apple = new apple();
500 represents the width of the stage, set it to whatever the width of your stage is
appleArray.push(apples);
addChild(apples);
add the apples to the array and add the apple
for each (apples in appleArray)
At the same time it'll check for existing apples
apples.y++;
and move their y coordinates by 1
--------------------------------------------------------------
or maybe you mean in your document class do this
var fruitFall:FallingObjects = new FallingObjects();
addChild(fruitFall);
to add the class?
I have created a start-up kit which shows code on adding/removing classes among other things you should check it out HERE
Yeah it was add it to the document class but you are good at explaining things thanks and thanks for the link.
You must be logged in to post a reply!