PDA

View Full Version : [AS3] Shooting bullets from moving object


hnicky
09-22-2011, 06:00 PM
I am developing a game in AS 3.0. The issue i have is it keeps shooting from the last position and angle instead of updating it when i move the hero with arrow keys. Can someone please help me with updating its shooting (both x, y and angle) after i move it with arrow keys. Perhaps direct me to an example online? or show me code example. Thank you.

SWF File - Sky Raid game - wwwdotfreewebsdotcom/hundalraj/gamesdothtml
Arrow Keys to move and Mouse Click to shoot.

[as3]

if (fireBulletFlag && delay_completed)
{

var ang = outer_mc.rotation;
delay_completed = false;
delay.addEventListener(TimerEvent.TIMER, on_delay_over);
delay.start();

for (var i:uint = 0; i < bullets_per_time; i++) // bullets fired by hero
{

var dist_x:Number = outer_mc.x + game_container.x - mouseX;
var dist_y:Number = outer_mc.y + game_container.y - mouseY;
bullet = new Bullet_mc(enemy, outer_mc.x, outer_mc.y, Math.atan2(dist_y, dist_x));
game_container.addChild(bullet); // add bullet here

}
[as3]

Here outter_mc is the hero MovieClip.

hnicky
11-14-2011, 05:29 AM
How I am moving the Player -

public function move_player():void - Function Called From OuterMovieClip class
{

xspeed *=friction;
yspeed *= friction;

x += xspeed;
y += yspeed;
// preventing spaceship to fly off the stage
if (x>750) {

x=750;
}
if (x<10) {
x=10;
}
if (y>850) {
y=850;
}
if (y<10) {
y=10;
}
}

How I am move the Container (background) -

public function move_container(player):void - From Game_container_mc class
{
x = 320 - player.x; // x of container
y = 240 - player.y; // y of container

if (x>0)
{
x=0;
}

if (x<640*-1)
{
x=-640;
}

if (y>0)
{
y=0;
}
if (y<400*-1)
{
y=-400;
}
}