Hey there… wondering if anyone can help me!
Im making a space game and so far so good however, im currently programming the AI and strangely when i call the ‘shoot laser’ function with ‘setInterval’ the lasers doesnt positon correctly (0, 0) / wrong colour etc, however if i call the function ‘shoot();’ it works perfectly. I know i could just knock up a few incrementing vars to get the same effect but i would like to really get the setInterval working!.. here is my code for that class :}
TY in advance!
import flash.filters.GlowFilter;
class turret extends MovieClip
{
//variables
private var speed:Number = 0.050;
private var angle:Number;
private var dx:Number;
private var dy:Number;
private var colour:Number = 0x0099FF;
private var sDelay:Number = 2000;
private var pCount:Number = 1;
private var accuracy:Number = 5;
private var lspeed:Number = 16;
private var shootem:Object;
private var laser:MovieClip;
private var lcolour:Color;
//constructer
public function turret()
{
this._x = Stage.width/2;
this._y = Stage.height/2;
this.filters = [new GlowFilter(colour, 0.75)];
//prepare sounds
//
//
//
//set intervals
shootem = setInterval(shoot, sDelay);
}
//calculate movement, rotation etc
private function onEnterFrame():Void
{
dx = _root.player._x-this._x;
dy = _root.player._y-this._y;
angle = Math.atan2(dy, dx)*180/Math.PI;
this._rotation = angle;
}
//shoots laser
private function shoot():Void
{
laser = _root.sourceMC.attachMovie("laser", "plaser"+pCount++, _root.sourceMC.getNextHighestDepth());
laser._x = this._x;
laser._y = this._y;
lcolour = new Color(laser);
lcolour.setRGB(colour);
laser.filters = [new GlowFilter(colour, 0.80)];
laser.angle = angle+Math.random()*(accuracy-(accuracy*-1)+1)-accuracy;
laser._rotation = laser.angle;
laser.dx = Math.cos(laser.angle*Math.PI/180)*lspeed;
laser.dy = Math.sin(laser.angle*Math.PI/180)*lspeed;
//move laser
laser.onEnterFrame = function():Void
{
this._x += this.dx;
this._y += this.dy;
//removes lasers off-stage
if (this._x>_parent.player.right+this._width/2) {
this.removeMovieClip();
} else if (this._x<_parent.player.left-this._width/2) {
this.removeMovieClip();
}
if (this._y>_parent.player.bottom+this._width/2) {
this.removeMovieClip();
} else if (this._y<_parent.player.top-this._width/2) {
this.removeMovieClip();
}
};
}
}