How to get widht,height of animated sprite?

SetColidRect doesn't work because w and h = 0 , why ?

local function createMeteorAnime(x, y)

    local tabMeteoriteAnime = {
        "asteroid_sprite1",
        "asteroid_sprite2",
        "asteroid_sprite3"
    }

    tmeteoriteAnime = math.random(#tabMeteoriteAnime)
    MeteorAnime = tabMeteoriteAnime[tmeteoriteAnime]

    if meteorAnimeCount < maxMeteoriteAnime then

        local meteoriteAnime = gfx.sprite.new()
        meteoriteAnime.imagetable = gfx.imagetable.new("images/".. MeteorAnime)
        meteoriteAnime.animation = gfx.animation.loop.new(80, meteoriteAnime.imagetable, true)

        local w, h = meteoriteAnime:getSize()
        meteoriteAnime:setCollideRect(0, 0, w, h)
        --meteoriteAnime.allOverlappingSprites()
        local Xpath = math.random(400)
        local Ypath = -math.random(30) - h
        meteoriteAnime:moveTo(Xpath, Ypath)
        --meteoriteAnime:moveWithCollisions(Xpath, Ypath)
        meteoriteAnime:add()

        print("witdh = "..w.." - height = "..h)

        meteorAnimeCount = meteorAnimeCount + 1

        function meteoriteAnime:collisionResponse(other)

            return gfx.sprite.kCollisionTypeOverlap

        end

ezgif-1-ce4fb15145

thank you for your advise

I don't really understand why you are setting meteoriteAnime.imagetable and meteoriteAnime.animation directly. There is nothing in the documentation that says this is possible. It seems by coincidence that setting the animator directly works, but you are supposed to set it using meteoriteAnime:setAnimator(). I don't think setting the imagetable directly does anything, you should set the image to the first frame of the animation using meteoriteAnime:setImage().

2 Likes

Unfortunately I failed to use the Animator() function.
I was able to solve my problem with:

local meteoriteAnimeSize = meteoriteAnime.imagetable:getImage(1)
local w, h = meteoriteAnimeSize:getSize()
meteoriteAnime:setCollideRect(0, 0, w -4 , h -4)

Bonne soirée.

You can read width and height properties from your sprite directly too. So sprite.width for example. There are also x and y properties you can read.

2 Likes