Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Sprite SetClip Questions — Gideros Forum

Sprite SetClip Questions

ChrisFChrisF Member
edited February 2016 in General questions
Ok first the issue, then the code snippet.
I am working on a new game and as part of that I need scale and clip a large number of sprites.
I first do the calculations to work out the new size and what the scale factor is.
I then work out if it overlaps other things or not based upon it's new size and then want to clip it if needed.

However the setClip function seems to work PRIOR to the setScale meaning I need to work out the clip amount on the prescaled image, then scale it which seems overlay laborious.

Code speaks louder than words so here is it illustrated (using the bird sprites that come with gideros). Do I really need to work clipping out on the PRE scaled dimensions ??
-- load texture, create bitmap from it and set as background
local background = Bitmap.new(Texture.new("sky_world.png"))
stage:addChild(background)
 
 
-- bird_black_01 = 100 w x 44 h
local bird1 = Bitmap.new(Texture.new("bird_black_01.png"))
local bird2 = Bitmap.new(Texture.new("bird_black_01.png"))
local bird3 = Bitmap.new(Texture.new("bird_black_01.png"))
local bird4 = Bitmap.new(Texture.new("bird_black_01.png"))
 
-- draw bird 1
bird1:setPosition(0,0)
 
-- draw bird 2, clip to half width and half height
bird2:setPosition(150,0)
bird2:setClip(0,0,50,22)
 
-- draw bird 3, scale to 50%
bird3:setPosition(300,0)
bird3:setScale(0.5)
 
-- now bird 4, scale to 50% and then apply the same clip
-- as the image is now half the size the clip should have zero effect as the image is now 50x22, but it has the exact same affect as the first clip.
bird4:setPosition(0,150)
bird4:setScale(0.5)
bird4:setClip(0,0,50,22)
 
-- add birds to the stage
stage:addChild(bird1)
stage:addChild(bird2)
stage:addChild(bird3)
stage:addChild(bird4)

Comments

  • hgy29hgy29 Maintainer
    Yes, clip coordinates are in Sprite local coordinates. You could try to embed one Sprite into another dummy one, and clip the container sprite instead
  • ChrisFChrisF Member
    edited February 2016
    That makes no sense, especially as getWidth and getHeight return the SCALED size.
    I could understand if there was a flag to say local or scaled but to have no option of scaled just seems odd.

    I tried using a container sprite and no matter which way I tried it never gave the correct result.

    This did, but is woefully inefficient
    bird4:setPosition(0,150)
    bird4:setScale(0.5)
    local width = bird4:getWidth()
    local height = bird4:getHeight()
    local scale = bird4:getScale()
    print(width,height,scale)
    if(scale>1) then
    	bird4:setClip(0,0,width*scale,height*scale)
    else
    	bird4:setClip(0,0,width/scale,height/scale)
    end
    In my game the scale,x & y are calculated and then applied - I then do overlap tests based upon the objects width & height - to then have to reapply to scale to do the clipping just seems overkill.

    If it is possible without doing this extra maths with a container would be great to see how as I just can't make it work (Even though in theory it should)
  • hgy29hgy29 Maintainer
    Accepted Answer
    Well on the contrary I thought (but may have been wrong) it was better to apply it on untransformed coordinates, so that you can scale your sprite after that without changing the visible part of it. Doesn't that make some sense said that way ?

    I would have done the way below (though it needs one more Sprite, right, but Sprites are cheap)
    bird4:setScale(0.5)
    local bird4clip=Sprite.new()
    bird4clip:addChild(bird4)
    bird4clip:setPosition(0,150)
    bird4clip:setClip(0,0,50,22)

    Likes: ChrisF

    +1 -1 (+1 / -0 )Share on Facebook
  • aha that works (bangs head against wall) , no idea why when I tried that it didn't work for me but long since deleted the attempt and yeah sprites are cheap. Thanks (Y)

    Your thoughts on applying it to untransformed coordinates make perfect sense, just not for all scenarios.

    For previous projects it would be fine. For this I do not know if I need to clip it until after I have scaled it, (I am being lazy and just grabbing a scale from a table based upon distance from camera) , so it is only once I have scaled the object do I know it's new size (just grabbing using getWidth() & getHeight) and I check and if it needs to be clipped or not (if that makes).

    Anyway thanks for your help the workaround should be fine.
Sign In or Register to comment.