Actionscript - 随机水平移动丢弃对象

时间:2012-03-26 00:37:11

标签: flash animation actionscript

我正在为我的动作类制作类似于Kaboom的游戏

http://www.groovz.com/play/kaboom/

我意识到我甚至没有接近完成它可能很糟糕但是这是我的代码到目前为止:

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

我知道我想使用计时器事件或其他东西来让太阳随机切换方向,但我无法弄明白,建议?

这是一个关于swf看起来像什么的想法(它没有在这个模拟中加载所有东西,铲斗在舞台上并且实际上左右移动了KeyDown):

http://www.shareswf.com/game/24222/kaboom-sun-clone

1 个答案:

答案 0 :(得分:1)

喜欢这样

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