Actionscript - 随机水平移动掉落物体
Posted
技术标签:
【中文标题】Actionscript - 随机水平移动掉落物体【英文标题】:Actionscript - Random Horizontal Movement Dropping Objects 【发布时间】:2012-04-09 13:18:14 【问题描述】:我正在为我的动作脚本类制作类似于 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 的概念(在这个模拟中它没有加载所有内容,桶在舞台上并在现实中左右移动 onKeyDown):
http://www.shareswf.com/game/24222/kaboom-sun-clone
【问题讨论】:
【参考方案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;
【讨论】:
感谢这很好用,现在我有最后一个问题:如何让火球更频繁地掉落?我假设我使用 Math.random 不正确?还是等待 1 从数组中删除,然后再删除另一个? 运行您链接到的 swf,我得到一个空引用错误。我认为您代码中其他地方的错误正在使脚本崩溃。 是的,我认为 null 引用是 fireBalls 数组,我在这里提出了一个新问题:***.com/questions/9866132/…以上是关于Actionscript - 随机水平移动掉落物体的主要内容,如果未能解决你的问题,请参考以下文章