2014-11-24 55 views
0

新的AS3和试图让这个游戏机制正常工作。我需要做的是让每只流星在屏幕上出现时立即向左移动,但它们根本不移动。如果有人知道如何解决这个问题,我将不胜感激!我将代码分成两部分,阶段代码和对象(流星)类代码。AS3对象运动

以下是舞台上的代码。

import flash.events.MouseEvent; 
import flash.events.Event; 

var mcShip:Ship; 
var meteor:Meteor; 
var uiTimer:uint = 0; 
var aMeteors:Array = new Array(); 

function InitializeGame():void 
{ 
    mcShip= new Ship(); 
    mcShip.Initialize(100,200); 
    stage.addChild(mcShip); 
    stage.addEventListener(MouseEvent.MOUSE_MOVE, MouseInput); 
    stage.addEventListener(Event.ENTER_FRAME,GenerateMeteors); 
} 

function MouseInput(me_:MouseEvent):void 
{ 
    mcShip.Movement(me_); 
} 

function GenerateMeteors(eGenerate:Event):void 
{ 
    if (0 == ++uiTimer%10) 
    { 
     meteor= new Meteor(); 
     aMeteors.push(meteor); 
     meteor.Initialize(550, 390, 20); 
     stage.addChild(meteor); 

     trace (aMeteors); 
    } 
} 
InitializeGame(); 

下面是对象(流星)代码。

import flash.events.Event; 
var speed:int; 
var aMeteors:Array = new Array(); 


function Initialize(iPosX_:int, iPosY_:int, iSpeed_:int):void 
{ 
    x = iPosX_; 
    y = Math.round(Math.random()* iPosY_) 
    speed = Math.round(Math.random() * iSpeed_); 
    var timer:Timer = new Timer(12) 
    timer.addEventListener(TimerEvent.TIMER,Update); 
    timer.start(); 

} 



function Update(ev_:Event):void 
{ 
    for (var a:int=0; a < aMeteors.length; a++) 
    { 
     aMeteors[a].x -= 1 * speed; 
    } 
} 

所以基本上,我试图让流星在x轴上向左移动。我确定我有很多问题妨碍了它的正确移动,但我无法弄清楚。感谢所有提前的帮助!所有的

回答

1

杉杉:产生一个随机数,使用

Math.random() 

这会产生0和1之间的随机数,要获得0和400之间的数字,你可以通过400,然后乘以这个数使用

Math.round(number) 

要移动的小行星,首先你需要创建一个数组来储存所有英寸

var asteroids:Array = new Array; 

您将需要一个带有事件监听器的计时器来添加它们。

var asteroidAdder:Timer = newTimer([delay],[repetitions or 0 for infinite repetitions]); 
asteroidAdder.addEventListener(TimerEvent.TIMER,addAsteroid); 

你应该让addAsteroid为它创建小行星的功能,并使用它添加到您的数组:

asteroids.push(asteroid); 

你的最后一步是添加其他定时器事件侦听器,移动它们。让它调用一个函数,也许'moveAsteroids'。在这个函数中应该是一个“for”循环,这样的事情:

for (var a:int=0; a<asteroids.length; a++){ 
    asteroids[a].x+=asteroids[a].speed; 
} 

这将通过每个对象的阵列中(小行星[0]然后小行星[1]等)和移动他们的x位置由他们的速度。您可能还想添加检查,看看他们何时离开屏幕边缘。发生这种情况时,您可以通过for循环删除它们:

removeChild(asteroids[a]); //removes the asteroid being checked from the stage 
asteroids.splice(a,1) //remove the asteroid at position 'a' in asteroids from the array 

希望这足以让您顺利。我认为你对制作函数和使用事件监听器有一定的了解。如果您有任何问题,请发表评论。

+0

谢谢,Trex。我仍然在努力解决这个问题,但你的回应正在帮助我! – Eindigen 2014-11-24 09:29:56

+0

我修复了一些问题,Trex,但我更新了原始帖子,因为我无法让流星左移!再次感谢你的帮助。 – Eindigen 2014-11-24 12:20:38

+0

你的移动代码看起来应该可以工作,尽管1 *可能是多余的。你能告诉我当你运行代码时究竟发生了什么吗?流星出现并保持静止吗? – Trex 2014-11-26 00:00:08