Discord
Login
Community
DARK THEME

How do i get sprite width/height by name?

For example i have useless function (just for explaning question)

spriteDraw = function(sprite)
  w = sprtie.width      //Returns 0, but i need actual sprite width
  h = sprite.height     //Also returns 0
  screen.drawSprite(sprite, 0, 0, w, h)
end

draw = function()
  spriteDraw('somesprite')
end

Assuming sprite is a string with the name of the sprite you want to use:

spriteDraw = function(sprite)
  local s = sprites[sprite]
  local w = s.width      
  local h = s.height    
  screen.drawSprite(sprite, 0, 0, w, h)
end

draw = function()
  spriteDraw('icon')
end

Please consider that in latest version of drawSprite you can omit both width and height as parameter so you can just call screen.drawSprite("icon",0,0) to draw at its original size.

@HomineLudens, thanks, but this not work for me... I still get zeros...

As i write - current (here) function is useless in project, need only to explain question :3

I have function which should 'cut' sprite into 9 pieces only having left, right, top & bottom values...

So i need some value to be calculating in middle sections, like:

Example:

Sprite looks like this (| & - is shows divider of areas) and called sprite:

-------------
|.1|...2|..3|   left area is 2x1, middle 4x1 and right is 3x1 (top = 1, left = 2, right = 3 and middle calculated from `9 - 2 - 3`
-------------
|..|....|...|   left is 2x2, middle (calculated) 4x2, right 3x2
|.4|...5|..6|   height here is calculated from `sprite.height - top - bottom`
-------------
|..|....|...|   left is 2x2, middle in 4x2 and right is 3x2
|.7|...8|..9|
-------------

Ist 9x5 sprite (dots as pixels, number also pixels showing area number) `|` & `-` is sprite and areas borders.

And i have this function (just to explain, not working one):

init = function()
  some('sprite', 2, 3, 1, 2)
end

some = function (sprite, left, right, top, bottom)
  w = sprite.width
  h = sprite.height

  //now assign arrays as [width, height] for pieces (9  pieces in total)
  left_top = [left, top]                 //Area 1
  left_bottom = [left, bottom]           //Area 7
  right_top = [right, top]               //Area 3
  right_bottom = [right, bottom]         //Area 9

//Here we `cut` left & right for width from sprite width to get middle parts width
//and top & bottom from sprite height to get middle parts height

  middle_width = w - left - right
  middle_height = h - top - bottom

  left_border = [left, middle_height]    //Area 4
  right_border = [right, middle_height]  //Area 6
  top_border = [middle_width, top]       //Area 2
  bottom_border = [middle_width, bottom] //Area 8

  middle = [middle_width, middle_height] //Area 5
end

After proper work we should get this values:

  w = 9
  h = 5
  left_top = [2, 1]
  left_bottom = [2, 2]
  right_top = [3, 1]
  right_bottom = [3, 2]

  middle_width = 4
  middle_height = 2

  left_border = [2, 2]
  right_border = [3, 2]
  top_border = [4, 1]
  bottom_border = [4, 2]

  middle = [4, 2]

So i need to get actual sprite width & height.

I get why its returns zeros :)

My function (in project) gets sprite.0 as sprite parameter...

Just need to cut .0 (frame) from this and i get actual width & height :)

HomineLudens - thanks again, you help me :3 (and some other who will search this)

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community