Shooting mechanic (harpoon)

77 views
Skip to first unread message

Apôtre Zara

unread,
Jan 27, 2016, 4:44:53 PM1/27/16
to HaxeFlixel
Hi,

I want to create a weapon similar to a harpoon : when the player hit the "Shoot" button, the projectile is "growing" toward the top of the screen at a fixed speed. When it reach a specific size, it disappear. The player can't move while the projectile is growing.
Well, it's like the weapon mechanic in http://www.bubbletrouble2.net/.

So I've got a Player class, which implement a group of projectiles like that :
projectiles = new FlxTypedGroup<Projectile>(maxProjectiles);        
var projectile:Projectile;
for (i in 0...maxProjectiles) {
    projectile
= new Projectile();
    projectiles
.add(projectile);
    projectile
.kill();
}
add
(projectiles);

and a method "onShoot" which is called when the button is pressed :
    private function onShoot():Void
   
{
        isShooting
= true;

       
var projectile = projectiles.recycle();
        projectile
.x = sprite.x + (sprite.width - projectile.width) / 2;
        projectile
.y = sprite.y - projectile.height / 2;
        projectile
.fire();
       
       
        projectiles
.forEachAlive(function(currentProjectile:Projectile) {
           
if (currentProjectile.height == 10) {
                isShooting
= false;
                currentProjectile
.kill();
           
}
       
});

   
}

The flag "isShooting" is used to block all keyboard input.

Here is my Projectile class :
package;

import flixel.FlxSprite;
import flixel.util.FlxTimer;

class Projectile extends FlxSprite
{
   
private var speed:Float = 0.2;
   
private var timer:FlxTimer;
   
   
public function new()
   
{
       
super();
       
        timer
= new FlxTimer();
       
        makeGraphic
(5, 1, 0xffff0000);
   
}
   
   
override public function update():Void
   
{
       
super.update();
        height
= timer.elapsedLoops;
        scale
.set(1, height);
   
}
   
   
public function fire():Void
   
{
        timer
.start(speed, 0);
   
}
}

The idea here is to grow the rectangle's height every 0.2s, and when the height reach 10, the player can move again.
The problem is that onShoot is a "oneshot" function. It's called once. The height is then about 1, so the condition to allow the player to move again is never triggered.
How would you handle that ?
Thank you very much, and sorry about the "poor" english.

Bogwog

unread,
Jan 28, 2016, 3:54:46 AM1/28/16
to HaxeFlixel
Just move the height checking code to the harpoon's update function, and expose a setter for the player's isShooting variable so the projectile can update it once it's own height reaches 10. You'll also need to pass a reference to the player to all your projectiles.

Apôtre Zara

unread,
Jan 28, 2016, 4:13:07 PM1/28/16
to HaxeFlixel
Perfect ! Tank you very much !

Apôtre Zara

unread,
Jan 28, 2016, 5:18:52 PM1/28/16
to HaxeFlixel
So here is my "onShoot" function now :
    private function onShoot():Void
   
{
        isShooting
= true;
       
       
var projectile = projectiles.recycle();

        trace
(projectile);

        projectile
.x = sprite.x + (sprite.width - projectile.width) / 2;
        projectile
.y = sprite.y - projectile.height / 2;
        projectile
.fire();
   
}

And I created a function "shootComplete" which is called by the projectile :
    public function shootComplete(projectile:Projectile):Void
   
{
        isShooting
= false;
        projectile
.x = 0;
        projectile
.y = 0;
        projectile
.height = 1;
        projectile
.kill();
   
}

And my projectile class :
package;

import flixel.FlxSprite;
import flixel.util.FlxTimer;
import Player;

class Projectile extends FlxSprite
{
   
private var speed:Float = 0.01;
   
private var timer:FlxTimer;
   
private var player:Player;
   
   
public function new(player:Player)
   
{
       
super();
       
this.player = player;

        timer
= new FlxTimer();
       
        makeGraphic
(5, 1, 0xffff0000);
   
}
   
   
override public function update():Void
   
{
       
super.update();
        height
= timer.elapsedLoops;
        scale
.set(1, height);

       
if (timer.loopsLeft == 0) {
            player
.shootComplete(this);
       
}
   
}
   
   
public function fire():Void
   
{
        timer
.start(speed, 100);
   
}
}

It's working : the harpoon is launched, grows and disappear when reaching the specified height.
However, when I recycle it, there's a glitch : for a fraction of second, the new harpoon appears with its former height. The fact that I reset the height before killing it doesn't change anything.


Le jeudi 28 janvier 2016 09:54:46 UTC+1, Bogwog a écrit :

Alejandro Ramallo

unread,
Jan 28, 2016, 8:22:00 PM1/28/16
to haxef...@googlegroups.com

How are you resetting the height? By calling something like


projectile.height = 0;

?

Because if you are, then it's possible that, when you call kill() right after that line, the object's graphic never gets a chance to update the graphic since it is being immediately removed from the scene. Then, when you finally revive it by calling recycle() does whatever function that is in charge of updating the graphic get called as usual (but not before being rendered on screen with the old height)

I say that it's "possible" because I'm not familiar with how HaxeFlixel's kill()/revive() mechanics work. Maybe someone who knows more about HaxeFlixel can give you a better answer, but try this:

Instead of kill()'ing it, make it invisible by calling:

projectile.visible = false;
 
in place of kill()



--
HaxeFlixel Development Community
See our github https://github.com/haxeflixel/ and our documentation http://haxeflixel.com/documentation/
---
You received this message because you are subscribed to a topic in the Google Groups "HaxeFlixel" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/haxeflixel/saUFTUxANrE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to haxeflixel+...@googlegroups.com.
Visit this group at https://groups.google.com/group/haxeflixel.
To view this discussion on the web visit https://groups.google.com/d/msgid/haxeflixel/3e824721-12eb-41e8-a171-99012a94decb%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Apôtre Zara

unread,
Jan 31, 2016, 1:17:29 PM1/31/16
to HaxeFlixel
It seems to be working :)
Thank you very much !
Reply all
Reply to author
Forward
0 new messages