Pop up a sprite on the other side when it goes out of bounds

Hello sorry for asking so many question in a short period of time but I really dont know how to call my problem in google and I am wondering if there is a built-in method which might solve it.

To learn the basics of cocos2d I am trying to write a snake game. I would like to create an area within which a snake will be able to move. The bounds of the area will be visible range of device. Now the problem comes how to implement a method or rather what should I use to prevent the sprite going endlessly on each axes and instead of this when sprite passes through for example right border it should equally appearing from the left side. Should I implement it myself or is there any built-in method which allows me to do it ?

So firstly, I would do this with a single sprite first, and you can simplify this to a single axis.

Let’s say you have a sprite at the screen center. You can apply a repeat forever moveby (0, -10) action to it, and it will slide left and disappear forever.

Next, pick a point to the left, say (0, 50) and when the sprite position reaches that, you can just move the sprite to the other side of the screen.

You will have to take the width of the sprite into account, since you want the right edge to be at 50, not the left edge.

Finally, you can add a clipping rectangle for the area you want, and add the sprites to that.

1 Like

Oh for the detecting of when to move the sprite. Subclass the sprite, schedule it for update, and in the update method, check the position and move it when it is past the boundary.

1 Like

We also have the Programmers Guide

Check out Chapter 4.

I have recently stumbled upon this type of problem.

I solved it by using two schedules on my sprite:

auto sprite = (Sprite*)parachute.get_sprite();
auto bounds_check = [ sprite, visibleSize](float dt) {
    if(sprite->getPositionX()+sprite->getContentSize().width/2 <= 0) {
        sprite->setPositionX(visibleSize.width+sprite->getContentSize().width/2);
    } else if(sprite->getPositionX()-sprite->getContentSize().width/2 >= visibleSize.width) {
        sprite->setPositionX(-sprite->getContentSize().width/2);
    } else {
        return;
    }
    sprite->unschedule("bounds_check");
};

auto bounds_check_schedule_notifier = [bounds_check, visibleSize, sprite](float dt) {
    if(sprite->isScheduled("bounds_check")) return;
    
    if(sprite->getPositionX()-sprite->getContentSize().width/2 > 0) {
        sprite->schedule(bounds_check, "bounds_check");
    } else if(sprite->getPositionX()-sprite->getContentSize().width/2 < visibleSize.width) {
        sprite->schedule(bounds_check, "bounds_check");
    }
};

sprite->schedule(bounds_check, "bounds_check");
sprite->schedule(bounds_check_schedule_notifier, "bounds_check_schedule_notifier");

The bounds_check function checks wether the sprite is off the screen horizontal wise.
If it is, it warps it on the other side and then the bounds_check is unscheduled for the meantime.

The bounds_check_notifier function runs all the time.
If the bounds_check is running then we know that the sprite didn’t go out of bounds.
If it’s not running then we know the only way it was unscheduled if it went out of bounds.
So we wait till the user goes back into the screen and once that happens we reschedule
the bounds_check from within the bounds_check_notifier. And the process repeats :slight_smile: