ForumsProgramming Forumshooting bullets in AS3

3 5493
deadbolt742
offline
deadbolt742
3 posts
Nomad

Ok so I have some stuff written up and I have it so the main character moves to the keys, and is always looking at the mouse. Although when I try to shoot using stage.addEventListener(MouseEvent.CLICK, ...) the bullet remains at 0,0. I made a bullet class to take the characters x,y and rotation also speed. I found out if I keep increasing speed the radius around 0,0 increases in the way the character aims but its not firing from his x,y just the 0,0. Any help would be much appreciated.

  • 3 Replies
AbnormalIdiot
offline
AbnormalIdiot
110 posts
Nomad

Use code that would be something like this:
on your character's class:

var bullet = new Bullet(x, y) // or any other x and y values you want the bullet to start at.
stage.addChild(bullet)

and in the bullet class:

public function Bullet(startingX, startingY):void
{
x = startingX;
y = startingY;
}

deadbolt742
offline
deadbolt742
3 posts
Nomad

public function fireBullet(event:MouseEvent)
{
var d:Bullet = new Bullet(hero.x, hero.y, hero.rotation, 6);
stage.addChild(d);
}


I had this written out, which I thought would work but it doesn't for some reason.

This is in my character class.
public function Hero(stageRef:Stage)
{
this.stageRef = stageRef;

//starting position for the hero
this.x = stageRef.stageWidth/2;
this.y = stageRef.stageHeight/2;
}
deadbolt742
offline
deadbolt742
3 posts
Nomad

Whoops sorry for the pointless post.

I just saw in my bullet class that I had a = instead of a += for my movement of the bullet. Well thanks a lot for the help, but I might be needing some help if I can't figure out how to get a good hit collision system to work.

Showing 1-3 of 3