question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Resizing a NineSliceSprite after creation does not work properly when using Animated sprite

See original GitHub issue

OS platform / Browser

Windows 10, Chrome 105

melonJS version

v13.0.0

Bug description

The NineSliceSprite internal properties nss_width and nss_height never gets updated, making it impossible to change the NineSliceSprite size.

Setting the (undocumented) properties nss_width/nss_height instead of width/height seems to work, even if the sprite is animated.

I tried to patch the NineSliceSprite class with something like this:

get width() { return super.width; }
set width(value) {
    super.width = value
    this.nss_width = value;
}

But every frame calls the Sprite.setRegion method, which resets the width/height of the sprite

I think the easiest (but far from ideal) approach is a new method NineSliceSprite.changeSize(w, h) (or something like this) NineSliceSprite.resize can be used, but resize() is already taken by Rect.
Furthermore, it would create even more confusion when trying to set the width/height properties directly (why resize() works but width/height doesn’t?)

The ideal solution is to properly fix the get/set width/height behaviour.

Steps to reproduce the bug

// Create NineSliceSprite with size 32x32
const sprite = new NineSliceSprite(0, 0, {
  image: 'nineslice',
  width: 32,
  height: 32
})

// Change width/height
sprite.width = 128

// NineSliceSprite don't change size
game.world.addChild(sprite)

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
obiotcommented, Aug 10, 2022

13.1.0 was just released ! Followed by a quick 13.1.1 fix but just for date and change tracking 😃

1reaction
obiotcommented, Aug 9, 2022

oh I see… honestly I don’t know how to fix this one quickly… the thing is that the sprite resizing/scaling feature rely on Matrix transformation to display the final sprite “size” and if you look at the Sprite Class code we actually never change the size of the sprite. So I really need to think about this one…

a quick and dirty fix is certainly to override the update method, call the

update(dt) {
    // .... do your stuff
   var dirty = super.update(dt);
   if (dirty) {
        // most likely the sprite frame was changed
        // Change width/height
       sprite.width = sprite.nss_width = 128;
   }
   return dirty;
}

I’m afraid I have nothing better to offer for now, as I know you are on a tight schedule with the game jam !

Read more comments on GitHub >

github_iconTop Results From Across the Web

9-slicing Sprites - Unity - Manual
When a Sprite's Draw Mode is set to Sliced, you can choose to change the size using the Size property on the Sprite...
Read more >
Unity Sprite Scaling For Beginners - 9 Slice Tricks - YouTube
Unity Sprite Scaling for beginnersBe sure to check out my Unity for Complete Beginners course on Udemy here: ...
Read more >
How to slice a sprite sheet in Unity - YouTube
In this video we'll look at how to slide your sprite sheet in Unity. Unity Sprite Sheet Slice shows you everything you need...
Read more >
How to SLICE SPRITE in Unity - YouTube
In this tutorial, I will show you basic methods how to slice sprites in Unity sprite editor. ☆ Hopes you enjoy it. If...
Read more >
9-Slicing Sprites in Unity: How and Why to Do It - YouTube
How to use Unity's built-in 9 - slice utility via the Sprite Editor, and what this help you accomplish. 9 -slicing sprites is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found