2
\$\begingroup\$

So, I found I needed a different type of button for my game, which I am making using Haxe, OpenFL and HaxeFlixel. After sometime fliding with Haxe, I got following button done, which works just fine. It's not the most sophisticated button, but it gets the job done.

class ArrowButton extends FlxGroup
{
    private var spriteUp:FlxSprite;
    private var spriteDown:FlxSprite;
    private var onClick:Void->Void;
    private var x:Float;
    private var y:Float;
    private var traced:Bool;

    public function new(x:Float = 0, y:Float, up = true, onClick:Void->Void = null) 
    {
        super();

        this.x = x;
        this.y = y;
        this.onClick = onClick;

        if (up)
        {
            spriteUp = new FlxSprite(x, y, Assets.getBitmapData("assets/ArrowUp.png") );
            spriteDown = new FlxSprite(x, y, Assets.getBitmapData("assets/ArrowUpPushed.png") );
        }
        else 
        {
            spriteUp = new FlxSprite(x, y, Assets.getBitmapData("assets/ArrowDown.png") );
            spriteDown = new FlxSprite(x, y, Assets.getBitmapData("assets/ArrowDownPushed.png") );
        }
        add(spriteDown);
        add(spriteUp);
        buttonUp();     
    }

    private function buttonDown():Void
    {
        if (pressed != true)
        {
            spriteUp.visible = false;
            spriteDown.visible = true;
            pressed = true;
        }
    }

    private function buttonUp():Void
    {
        if (pressed = true)
        {
            spriteUp.visible = true;
            spriteDown.visible = false;
            pressed = false;
        }
    }

    private function isClicked():Void
    {
        //trace("Mouse X:Y" + FlxG.mouse.screenX + ":" + FlxG.mouse.screenY + ", button X:Y " + x + ":" + y);

        if (onClick != null && FlxG.mouse.screenX > x && FlxG.mouse.screenX < x + 16 && FlxG.mouse.screenY > y && FlxG.mouse.screenY < y + 16)
        {
            buttonUp();
            onClick();
        }
    }

    override public function destroy()
    {
        super.destroy();
    }

    override public function update()
    {
        if(FlxG.mouse.justReleased())
        {
            isClicked();
        }

        if (FlxG.mouse.pressed() && FlxG.mouse.screenX > x && FlxG.mouse.screenX < x + 32 && FlxG.mouse.screenY > y && FlxG.mouse.screenY < y + 32)
        {
            buttonDown();
        }
        else
        {
            buttonUp();
        }


        super.update();
    }

}

Now, I got problem: I can't hide the button. I add this button as a member of another FlxGroup, called systemView. I got several views that I hide and show, depending what menu player is in. So when I move out of systemView, I call function called hideStuff()

private function hideStuff():Void
{
        sideMenu.setAll("visible", false, true);
}

now, this works fine for everything else, EXCEPT my own button. That one it misses, for some reason. Any ideas why this happens and how to fix it?

\$\endgroup\$
1
  • \$\begingroup\$ Fixed the problem by changing it from FlzGroup to FlxSprite, but I would still like to know what went wrong. \$\endgroup\$
    – Mandemon
    Commented Aug 6, 2013 at 2:43

1 Answer 1

2
\$\begingroup\$

Not sure what went wrong there. Just tested that setAll("visible", false) call on a FlxGroup and it seemed to be working fine.

A few other things though:

  • You should not need to do a setAll() for visibility on a FlxGroup at all. FlxGroups have a visible property themselves, because they extend FlxBasic!
  • The button you're trying to implement, having a sprite for the onUp and one for the onDown state, seems extremely similar to FlxButtonPlus. Have a look at that!
  • You don't need to use Assets.getBitmapData() when loading a graphic for a FlxSprite. You can just pass the path to the asset as a String!
  • There's a handy plugin called MouseEventManager which lets you create callbacks for mouse events on a FlxSprite (onUp, onDown, onOver, onOut). Check the demo out here. Please note that the demo code only works with the dev version of flixel currently, and that the plugin is still called MouseInteractionMgr in the haxelib release (2.0.0-alpha.3). Either way, this plugin would help avoid those region checks in the update() function, which could become expensive for a lot of these buttons.

Have fun using HaxeFlixel! If you run into any other issues in the future, it's probably a better idea to post to the official HaxeFlixel forums. You're way more likely to get help there, since the engine is not all that well-known currently.

\$\endgroup\$
1
  • \$\begingroup\$ Thanks for all the tips! Yeah, I love HaxeFlixel. In short time, I managed to make pretty good prototype for my game. Still, Haxe is quite different from C++, which I am used to, but it's much more approachable than Actionscript. \$\endgroup\$
    – Mandemon
    Commented Aug 9, 2013 at 19:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .