Q:

Actionscript - Random Horizontal Movement Dropping Objects

I'm making a game similar to Kaboom for my actionscript class
http://www.groovz.com/play/kaboom/
I realize I'm not even close to done and its probably shitty but this is my code so far:
var bucket:Bucket=new Bucket
var sun:Sun=new Sun
var fireBalls:Array=new Array()
var livesLeft:Array=new Array()
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 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();
    sun.x=sun.x+15;
    if (fireBalls.length>0&&fireBalls[0].y>stage.stageHeight-20){ // 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);
    }
    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;
    }
}

I know I want to use a timer event or something to get the sun to switch directions randomly but I can't quite figure it out, suggestions?
this is an idea of what the swf looks like (its not loading everything in this simulation, the bucket is on the stage and moves left and right onKeyDown in reality):
http://www.shareswf.com/game/24222/kaboom-sun-clone

A:

Like so
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.5);// 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-20){ // 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);
    }
    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;
    }
}

