Eindigen
Eindigen

Reputation: 11

AS3 Object Movement

new to AS3 and trying to get this game mechanic working properly. What I need to do is get each meteor to move to the left as soon as it appears on the screen, but they don't move at all. If anyone knows how to fix this, I would greatly appreciate it! I split the code into two parts, the stage code and the object (meteor) class code.

Below is the code on the stage.

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();

Below is the object (meteor) code.

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

So essentially, I'm trying to get the meteors to move left on the x axis. I'm sure I have a ton of issues that's preventing it from moving left properly, but I can't figure it out. Thanks for all the help in advance!

Upvotes: 0

Views: 218

Answers (1)

Trex
Trex

Reputation: 118

Firs of all: to generate a random number, use

Math.random()

This generates a random number between 0 and 1. To get a number between 0 and 400, you can multiply this number by 400 and then use

Math.round(number)

To move the asteroids, first you will need to create an array to store them all in.

var asteroids:Array = new Array;

You will need a timer with an event listener to add them.

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

You should make addAsteroid into a function which creates the asteroid, and adds it to your array using:

asteroids.push(asteroid);

Your last step will be to add another timer with event listener to move them. Make it call a function, maybe 'moveAsteroids'. Within this function should be a 'for' loop, something like this:

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

This will go through each object in the array (asteroids[0] then asteroids[1] and so on) and move their x position by their speed. You might also want to add a check for when they go off the edge of the screen. When this happens, you could remove them through the for loop using:

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

Hopefully this is enough to get you on your way. I assumed you have some knowledge about making functions and using event listeners. If you have any problems, just leave a comment.

Upvotes: 1

Related Questions