PDA

View Full Version : [AS3] Side Scrolling Shooter Firing Problem


Lenster921
09-22-2008, 06:16 PM
I'm trying to make a side scrolling shooter game. I have been able to make the screen scroll across the background and I've made it so that the character stays in the screen and can't go out. The biggest problem I'm having is that when I fire, It comes out of the character at the beginning of the stage and as it starts to scroll across the bullets starting position begin move away from the character. I'll attach the file to show what I'm dealing with. I have a feeling it has to do with hero.mc.x but I don't know how to fix it. package {
import flash.display.*;
import flash.events.*;
import flash.utils.getTimer;
public class MyBullet extends MovieClip {
private var dx:Number; //vertical speed
private var lastTime:int;

public function MyBullet(x,y:Number,speed: Number){
//set start position
this.x = x;
this.y = y;
//get speed
dx = speed;
//set up animation
lastTime = getTimer();
addEventListener(Event.ENTER_FRAME,moveBullet);
}
public function moveBullet(event:Event){
//get time passed
var timePassed:int = getTimer()-lastTime;
lastTime += timePassed;

this.x -= dx*timePassed/1000;

//bullet past top screen
if (this.y <0){
deleteBullet();
}

}
//delete bullet from stage and plane list
public function deleteBullet() {
MovieClip(parent).removeBullet(this);
parent.removeChild(this)
removeEventListener(Event.ENTER_FRAME,moveBullet);
}


}
}

public function fireBullet(){
var b:MyBullet = new MyBullet(hero.mc.x,hero.mc.y,-300);

addChild(b);
bullets.push(b);

}
public function removeBullet(bullet:MyBullet){
for(var i in bullets) {
if (bullets [i] == bullet) {
bullets.splice(i,1);
break;
}
}
}