Forums

ForumsProgramming Forum

setup?

Posted Nov 27, '13 at 3:09pm

alex57834

alex57834

89 posts

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);
}

}

}

 

Posted Nov 27, '13 at 4:28pm

weirdlike

weirdlike

714 posts

Moderator

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

 

Posted Nov 28, '13 at 11:59am

alex57834

alex57834

89 posts

But I don't understand how I would start the class so it starts producing apples.

 

Posted Nov 28, '13 at 5:29pm

weirdlike

weirdlike

714 posts

Moderator

referencing my code you first create an array and a timer event

when the timer fires it does a random calculation

if ( Math.random() &lt 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

 

Posted Nov 29, '13 at 2:29am

alex57834

alex57834

89 posts

Yeah it was add it to the document class but you are good at explaining things thanks and thanks for the link.

 
Reply to setup?

You must be logged in to post a reply!