I am desperate for an answer!!!!
-
Hi there, can you please show me or direct me to a tutorial on how to make my player shoot bullets from three different angles (0,90,180). I have tried to do this my self but it on shot at 180 degree. Please help.
-
You need to get the angle of direction, so if you have the start x/y you can add -1, 0 or 1 to get the dx/dy (direction) you want it to go. To go UP, dx is the same as x, dy is y - 1. That's the point straight up from starting point.
something like this.
public function shoot(x:Int, y:Int, direction:String, speed:Int):Void { var dx = x; var dy = y; switch(direction) { case "left": dx += -1; dy += 0; case "up": dx += 0; dy += -1; case "right": dx += 1; dy += 0; } var rangle = radianAngle(x, y, dx, dy); velocity.set(Math.cos(rangle) * speed, Math.sin(rangle) * speed); } private function radianAngle(x1:Float, y1:Float, x2:Float, y2:Float):Float { return Math.atan2(y2 - y1, x2 - x1); }
This is just to give you the math needed for the getting the angle. You should change the code to work with your code.
-
Haxeflixel has a lot of really nice helper functions for things like this.
https://api.haxeflixel.com/flixel/math/FlxVelocity.html#velocityFromAngleimport flixel.math.FlxVelocity;
...
var angle = 90;
var speed = 1500;
bullet.velocity = FlxVelocity.velocityFromAngle(angle, 1500);