[SOLVED] Finding if a the xcordinate of a scrolling sprite

Say I have a scrolling sprite the scrolls across the screen from LHS to RHS. How can I know the xcordinate of its ending when its on the screen . Consider the following diagram

How can I find the x cordinate of its Lower right corner so that I could know that the scrolling sprite has almost ended,

auto pos = sprite->getParent()->convertToWorldSpace(sprite->getPosition()); should tell you where on screen the centre of your sprite is.

If your sprite has the default anchor point of (0.5, 0.5), then you would get the bottom right position using

auto size = sprite->getBoundingBox().size;
pos.x += size.width / 2;
pos.y += size.height / 2;

Then pos would be the location on screen.

1 Like

I tried using

auto pos = t->getBoundingBox().size;
if (pos.width < visibleSize.width-500)
{
    CCLOG("The end of the box is visible");
}

However the the condition never becomes true. Does the bounding box size value return the visible size of the box only or does it return the size in which the hidden size is also included. Remember that my rectangular sprite is scrolling to the left and is bigger than the width of the screen

The other approach however works

auto pos = sprite->getParent()->convertToWorldSpace(sprite->getPosition());

You need to add the screen X position of the sprite to the sprite’s half width.

auto pos = t->getParent()->convertToWorldSpace(t->getPosition());
auto size = t->getBoundingBox().size;
if (pos.x + size.width / 2.f < visibleSize.width-500)
{
    CCLOG("The end of the box is visible");
}

could you explain why you did size.width / 2.f ? will that not take us to the center of the square image?

@grimfate explained it, essentially pos is based on the center of the sprite (by default .5,.5 anchor point). I just took both you and grimfate’s codes and combined them and checked only X coord based on your code.

@stevetranby Thanks for clearing that up. A couple of last questions

1- Could you explain what position the following returns
auto pos = t->getParent()->convertToWorldSpace(t->getPosition());
Does it return the x position that is visible on the screen or could it return the x position of the content that is on the left hand side of the screen and not visible I am thinking the x-cordinate in that case would be negative

2-Same question with bounding box. Does the boundingBox().size() return the size of the entire image or only the image that is seen on the screen. The reason i am asking this is because my image is way bigger than the width of the screen and its starting edge has moved to the left side of the screen where its not visible.

  1. yeah, pos is basically the screen position of the node t
  2. answered the size in the other post, but bbox.size = contentSize*scale
    2b. A node scales based on its anchor point of the node, so if 0.5,0.5 then it scales in all directions (left edge is further left, etc).
    (anyone correct me if I’m wrong)