I have some code where I create bullets in random positions on the screen and the direction of the bullets head towards the ship. However the bullets don’t keep moving in that direction and stop where the ship is. How can I make it so the bullets keep moving along in it’s linear path? I’m trying to make the bullets go to the edges of the screen border and appear on the other side. for instance if the bullet arrived at the right edge of the screen it would reappear on the left edge and keep going.
package Classes {
import flash.display.MovieClip;
import flash.events.Event;
public class Bullet extends MovieClip {
private var shipX:Number;
private var shipY:Number;
public function Bullet(shipX:Number, shipY:Number) {
this.shipX = shipX;
this.shipY = shipY;
this.x = 600*Math.random();
this.y = 800*Math.random();
this.addEventListener(Event.ENTER_FRAME, contRefresh);
}
private function contRefresh(event:Event):void {
var dx:Number = this.x - this.shipX;
var dy:Number = this.y - this.shipY;
var dr:Number = Math.atan2(dy, dx);
var ax:Number = 2*Math.cos(dr);
var ay:Number = 2*Math.sin(dr);
this.x -= ax;
this.y -= ay;
}
}
}