More problems with bullets[SOLVED]


(I'll tell you what I want, what I really really want) #1

Hey again, I’m trying to make my player shoot. Here’s what I’ve done: Bullet:

	public function Bullet(posx:int, posy:int) 
	{
		graphic = new Image(BULLET_ART);
		graphic.scrollY = 2;
		
		x = posx;
		y = posy;
		setHitbox(10, 10);
		
		type = "bullet";
		
	}
	
	override public function update():void 
	{
		y += 300 * FP.elapsed;
	}

Player Update:

		if (Input.check(Key.SPACE))
		{
			var bullet:Bullet = new Bullet(x, y + 5);
			FP.world.add(Bullet());
		}

Not even showing errors, just throwing me a good ol’ white screen, as it usually does when something is wrong. Any help?


(Bora Kasap) #2

your problem may be about something other, but there is a little problem here

var bullet:Bullet = new Bullet(x, y + 5);
FP.world.add(Bullet());

that should be like that in your way

var bullet:Bullet = new Bullet(x, y + 5);
FP.world.add(bullet);

(I'll tell you what I want, what I really really want) #3

That works now. Thank you for the fast response!


(Mike Evmm) #4

Also, use world.add instead of FP.world.add

It’s almost never a good idea to use FP.world for anything but actually setting the world. Don’t reference it to do collisions, or to count entities, or to add or remove entities. The only time you should be using it is when you do FP.world = new MyWorld().


(I'll tell you what I want, what I really really want) #5

thanks @miguelmurca, done :slight_smile:


(I'll tell you what I want, what I really really want) #6

Sry for double post, just bumping.

Another issue I have, ofc after looking for google answer, is:

		if (Input.released(Key.S))
		{
			world.add(bullet);
			bullet.y += 50 * FP.elapsed;
		}
		if (Input.released(Key.W))
		{
			world.add(bullet);
			bullet.y -= 50 * FP.elapsed;
		}
		if (Input.released(Key.A))
		{
			world.add(bullet);
			bullet.x -= 50 * FP.elapsed;
		}
		if (Input.released(Key.D))
		{
			world.add(bullet);
			bullet.x += 50 * FP.elapsed;
		}

I want to shoot in 4 directions, as shown above. Code from my player’s update function.


(Mike Evmm) #7

The key is only released once. That means the condition Input.released(Key.W) is only met once.
I’d suggest (if I got it right) something along these lines:

@Player Update function:

if (Input.released(Key.D))
{
	world.add(bullet);
	bullet.fire(0,50);
}

@Bullet:

private var speedX:Number;
private var speedY:Number;

public function fire(_speedX:Number, _speedY:Number):void
{
      speedX = _speedX; speedY = _speedY;
}

override public function update():void
{
    this.x += speedX; this.y += speedY;
}

(I'll tell you what I want, what I really really want) #8

well that works, but only for W and S. horizontal shot is just stuck where player is. Thanks anyway tho, that shines some light on the problem :slight_smile:

EDIT: No, it does work! Thank you so much @miguelmurca!