PDA

View Full Version : [AS3] Shooting Bullets


TobiasReich
09-14-2009, 10:40 AM
Hello everyone!
I was working on a nice, little game, where I was trying to shoot bullets, arrows, stones or what ever.
My problem is, I'd do this by using tweens for every single bullet. But As much as I know, I can only do "straight" tweens. So the bullet would fly from A to B directly, not in a curve.
I tried something like this:

function shoot():void {
// trace("shooting");
var bullet = new Bullet();
bullet.x = 400;
bullet.y = 100;
stage.addChild(bullet);
var schussBewegung:Tween = new Tween(bullet, "y", None.easeInOut, 90, 150, 100, false);
}

but after all, I don't see a way to define a curve these objects might move on.
Can anyone help me with this little "feature"? :-)
Thanks for your help,
Tobi

TomMalufe
09-14-2009, 10:21 PM
Tween Bad...

Enter Frame Event Good.
Loop Good.

Psudo-physics Good.

//create a class for your bullet and has x and y velocity and position

class Bullet //should probably extend sprite or something so you can show it on stage
//easily... in which case you don't need xpos or ypos.
{
public xpos:Number = 0;
public ypos:Number = 0;
public xvel:Number = 0;
public yvel:Number = 0;
}
// gravity can just be a global constant and that will help us curve the shot.

package
{
import flash.display.Sprite;
import flash.events.Event;
//... other imports

public class Main extends Sprite
{
public var bullets:Array = [];
public var GRAVITY:Number = .3;

public function Main()
{
this.addEventListener(Event.ENTER_FRAME, loop);
}

protected function loop(e:Event):void
{
for(var i:int=0; i<bullets.length; i++)
{
bullets[i].xpos += bullets[i].xvel;
bullets[i].ypos += bullets[i].yvel;
bullets[i].yvel += this.GRAVITY;
}
}
// I guess you are also going to have some function to add bullets to the bullets array.
}
}

At least this much is true for Tweens... generally they are too much for something simple like a bullet and you are going to slow down your simulation by using them. Also, how can you factor in gravity to give it a curve?

newblack
09-15-2009, 01:30 AM
i just wrote an animation library that's all based on animating along a path. there's even a physics path that will give you projectile motion:

http://blog.generalrelativity.org/actionscript-30/grape-animation-library/

TomMalufe
09-15-2009, 03:16 PM
i just wrote an animation library that's all based on animating along a path. there's even a physics path that will give you projectile motion:

http://blog.generalrelativity.org/actionscript-30/grape-animation-library/

I don't know if that's going to be much use though... it would be much harder to calculate the proper curve and then tween through it then it would be to just calculate each step of the projectiles motion with velocity and acceleration.

newblack
09-15-2009, 04:06 PM
no, i'm saying there's a projectile path as part of the library that handles that for you. the user's still required to provide starting position, initial velocity and acceleration vector. under constant acceleration there's an analytical solution to position at any time.

http://code.google.com/p/grape-as3/source/browse/trunk/src/org/generalrelativity/animation/grape/path/PhysicsPath2D.as