How to resize sprite hitbox (setSize() not working)?



  • I am trying to resize my sprite's hitbox so I can use flixel.util.FlxSpriteUtil to draw new things outside my sprite's original dimensions. I used a makeGraphic with width 50 and height 20 to make my sprite, and I tried resizing it like this:

    _playerCar.setSize(100, 50);

    (_playerCar is my sprite.) But the hitbox stays exactly the same after this, and I still can't draw anything outside my original rectangle. Is there a way to increase the size of the hitbox? Thanks.



  • Hi @CharType

    try updateHitbox() method after setGraphicSize().

    https://api.haxeflixel.com/flixel/FlxSprite.html#setGraphicSize
    https://api.haxeflixel.com/flixel/FlxSprite.html#updateHitbox

    _playerCar.setGraphicSize(100, 50);
    _playerCar.updateHitbox();
    


  • @dean Thanks, but that's not what I'm looking for. I want to resize just the hitbox. setGraphicSize() scales the whole graphic.



  • @chartype

    Oh. Maybe you can use _playerCar.makeGraphic(100, 50, 0x0); to create a transparent rectangle, then draw on it with FlxSpriteUtil.drawRect(_playerCar, 0, 0, 50, 20, 0xFFFFFFFF); and other stuff you want to draw. Just make the makeGraphic() the size that you want to draw on.



  • What you could do is change the sprite size, then update the hitbox to make the hitbox bigger. Shrink the sprite, then don't update the hitbox to keep the bigger size.
    Assuming this was your code.

    _playerCar.loadGraphic("assets/images/car.png", true, 50, 20);
    _playerCar.setGraphicSize(100, 50);
    _playerCar.updateHitbox();
    _playerCar.scale.set(0.5, 0.5);
    // Don't update hitbox after this
    

    That way you'll have twice as much room to play with. I've done a similar thing but the other way round.


Log in to reply
 

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.