Author Topic: Rocket launching movement  (Read 796 times)

NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Rocket launching movement
« on: Tue, Jul 6, 2010 »
I'm fairly new to AS3 and Flixel. I've only been practicing AS3 for a few months, and Flixel for a few short weeks. I've created a simple vertical scrolling space-shooter game.

I'm trying to animate a rocket shoot out from the side of the spaceship down at a short angle, and then launch forward. How could I go about doing this? I tried to figure it out and this is as far as I got (this code is in the update() function of my Rocket class):

Code: [Select]
if (!ignited)
{
velocity.y = 50;

if (leftOrRight) {
velocity.x = 50;
} else {
velocity.x = -50;
}

ignited = true;
}


if (ignited)
{
velocity.y = 500;
velocity.x = 0;
}

I'm still very unfamiliar with the movement controls of Flixel. Any suggestions?
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery

Rybar

  • Contributor
  • ****
  • Posts: 279
  • Karma: +0/-0
    • View Profile
Re: Rocket launching movement
« Reply #1 on: Tue, Jul 6, 2010 »
You need a timer.

Code: [Select]
public var counter:Number = 0; //this goes at the top, in your object scope
 
override public function update():void

if (!ignited)
{
velocity.y = 50;

if (leftOrRight) {
velocity.x = 50;
} else {
velocity.x = -50;
}

counter += FlxG.elapsed;
if (counter >= 2) //2 seconds here. set to whatever you need
{
ignited = true;
counter = 0;
}
}

if (ignited)
{
velocity.y = 500;
velocity.x = 0;
}


NateTheGreatt

  • Member
  • **
  • Posts: 83
  • Karma: +0/-0
  • smell the sound
    • View Profile
    • n8bit
Re: Rocket launching movement
« Reply #2 on: Tue, Jul 6, 2010 »
Ah okay, that makes sense. Thank you.

For anyone else with a similar problem: don't forget to include a super.update(). I forgot to include that and it had me stumped for awhile xD.

Here is my fully working class code:
Code: [Select]
package Entities
{
import org.flixel.*;

public class Rocket extends FlxSprite
{

[Embed(source = '../Images/Rocket.png')] private var ImgRocket:Class;

[Embed(source = '../Sounds/rocket.mp3')]
private var soundRocket:Class;

private var ignited:Boolean;
private var leftOrRight:Boolean
private var counter:Number = 0;
private var soundPlayed:Boolean = false;

public function Rocket(x:Number, y:Number, type:Boolean):void
{
super(x, y, ImgRocket);

leftOrRight = type;

ignited = false;
}

override public function update():void
{

if (!ignited)
{

velocity.y = 16;

if (leftOrRight) {
velocity.x = 38;
} else {
velocity.x = -38;
}

counter += FlxG.elapsed*2;
if (counter >= 1)
{
ignited = true;
counter = 0;
}

}

if (ignited)
{
acceleration.y = -500;
//velocity.y = -500;
velocity.x = 0;
}

super.update();

}

}

}
« Last Edit: Tue, Jul 6, 2010 by NateTheGreatt »
"A designer knows he has achieved perfection not when there is nothing left to add, but when there is nothing left to take away."
-Antoine de Saint-Exupery