I'm trying to get these fireBalls to drop more often, I'm not sure if I'm using Math.random correctly.

Also, for some reason I'm getting a null reference because I think the fireBalls array waits for one to leave the stage before dropping another one?

This is the relevant code:

var sun:Sun=new Sun
var fireBalls:Array=new Array()
var left:Boolean;

function onEnterFrame(event:Event){
    if (left) {
        sun.x = sun.x - 15;
    }else{
        sun.x = sun.x + 15;
    }
    if (fireBalls.length>0&&fireBalls[0].y>stage.stageHeight){ // Fireballs exit stage
        removeChild(fireBalls[0]);
        fireBalls.shift();
    }
    for (var j:int=0; j<fireBalls.length; j++){
        fireBalls[j].y=fireBalls[j].y+15;
        if (fireBalls[j].y>stage.stageHeight-fireBall.width/2){
        }
    }   
    if (Math.random()<.2){ // Fireballs shooting from Sun
        var fireBall:FireBall=new FireBall;
        fireBall.x=sun.x;
        addChild(fireBall);
        fireBalls.push(fireBall);
    }
}
link|improve this question
feedback

2 Answers

They might not be dropping as often as you want to because you're only removing one per enter frame event using this code:

if (fireBalls.length>0&&fireBalls[0].y>stage.stageHeight){ // Fireballs exit stage
    removeChild(fireBalls[0]);
    fireBalls.shift();
}

You might want to use a for loop instead to go through all of the fireballs on every enter frame event like so:

for (var i:int = 0; i < fireBalls.length; ++i)
{
    if (fireBalls.length > 0 && fireBalls[i].y > stage.stageHeight)
    {
        removeChild(fireballs[i]);
    }
}

As far as I can tell, your usage of Math.random() is correct, if you want a new fireball about 1/5th of the time.

link|improve this answer
so I replaced that if statement with the for if you recommended and its still only dropping 1 at a time... which is really strange because this is the same exact code I used for a previous lab with planes dropping bombs and it worked fine... stumped – Eratosthenes Mar 26 at 5:55
" && fireBalls[0] " should be " && fireBalls[i] " – ADB Mar 26 at 17:34
feedback

figured it out

import flash.events.TimerEvent;
import flash.utils.Timer;
var bucket:Bucket=new Bucket
var sun:Sun=new Sun
var fireBalls:Array=new Array()
var livesLeft:Array = new Array()
var left:Boolean;
var timer:Timer = new Timer(1000); // test change direction every second

addChild(bucket)
addChild(sun)
sun.x=sun.width/2
sun.y=50
bucket.x=bucket.width/2
bucket.y=stage.stageHeight-50
bucket.speed=15

stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown)

function onTimer(e:TimerEvent) {
    left = (Math.random() > 0.75);// 50/50 chance of changing direction
}
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

function onKeyDown(event:KeyboardEvent){
    if (event.keyCode==37&&bucket.x>bucket.width/2){ // left arrow key
        bucket.x=bucket.x-bucket.speed;
    }
    if (event.keyCode==39&&bucket.x<stage.stageWidth-bucket.width/2){ // right arrow key
        bucket.x=bucket.x+bucket.speed;
    }
}

stage.addEventListener(MouseEvent.MOUSE_MOVE,followBucket);

function followBucket(event:MouseEvent):void {
bucket.x=mouseX;
}

addEventListener(Event.ENTER_FRAME,onEnterFrame)

function checkForCollisions (){

}

function onEnterFrame(event:Event){
    checkForCollisions.call();
    if (left) {
        sun.x = sun.x - 15;
    }else{
        sun.x = sun.x + 15;
    }
    if (fireBalls.length>0&&fireBalls[0].y>stage.stageHeight){ // Fireballs exit stage
        removeChild(fireBalls[0]);
        fireBalls.shift();
    }
    for (var j:int=0; j<fireBalls.length; j++){
        fireBalls[j].y=fireBalls[j].y+15;
    }   
    if (Math.random()<.2){ // Fireballs shooting from Sun
        var fireBall:FireBall=new FireBall;
        fireBall.x=sun.x;
        fireBall.y=sun.y+sun.width/2;
        addChild(fireBall);
        fireBall.gotoAndStop(1);
        fireBalls.push(fireBall);
    }
    if (sun.x>stage.stageWidth){ // Sun hits right side of stage
        sun.x=0;
    }
    if (sun.x<0){ // Sun hits left side of stage
        sun.x=stage.stageWidth;
    }
}
link|improve this answer
2  
How about explaining the answer too instead of just posting new code. – Byte56 Mar 26 at 16:13
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.