Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to detect if a Sprite is on screen? (sprite container with deep hierarchy) — Gideros Forum

How to detect if a Sprite is on screen? (sprite container with deep hierarchy)

MellsMells Guru
edited February 2013 in General questions
Hi friends,

(I write my questions in a FAQ style so that it's better indexed for people that might search for it later)

If I have a Sprite A that belongs to a SpriteSubgroup, that belongs to a SpriteGroup :
So we have SpriteGroup > SpriteSubGroup > SpriteA

SpriteGroup is moving and scaled (ex : *.5).
SpriteSubGroup is moving also, has its own scale (ex : *2), inside SpriteGroup, with its own speed.
SpriteA is moving also, has its own scale (ex : *.3) inside SpriteSubGroup, with its own speed.

How would you detect if SpriteA is visible on screen, whatever scale and local position it is?
I believe it has to deal with localToGloba() or globalToLocal() but I couldn't understand how to use it.

How do you deal with it?

thank you!
twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps

Comments

  • BJGBJG Member
    edited February 2013 Accepted Answer
    Re: localToGlobal, it looks like you can find the global position of the sprite with:
    local x, y = SpriteA:localToGlobal(0, 0)
    This seems to work even with extra nesting (ie localToGlobal seems to return the absolute screen position rather than the position of the parent) so I don't think you need worry about that. You could then test this against the screen limits while taking account of any scaling.
  • @BJG thank you, it helped :)

    For those who might be looking for the answer later :


    1. A working sample by @atilim :
    Source
    Hi,

    There are two functions Sprite:localToGlobal and Sprite:globalToLocal to solve this problem. You can use these functions like:

    1. Use Sprite:localToGlobal to find the global (screen) coordinates of the sprite before moving:
    local x, y = sprite:localToGlobal(0, 0)
    2. Move your sprite to another group.
    anotherGroup:addChild(sprite)
    3. Find the new local coordinates in this group by using Sprite:globalToLocal and set the position:
    sprite:setPosition(anotherGroup:globalToLocal(x, y))
    Hope this helps. Happy coding on your new game :)
    2. A discussion on the topic :
    How does globalToLocal and localToGlobal really work?
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
Sign In or Register to comment.