2

Is it possible to prevent my CCSprite from going off-screen? I already allow it to go offscreen on the left and right so that is fine but I just want to stop it from going off screen on the top and bottom.

So far what I have done is just cause the sprite to just get stuck on either the top or bottom. I don't want this to affect the movement of the sprite, all I want to happen is the CCSprite will just stop when it hits the top or bottom.

Can anyone show me how to do this?

Thanks!

Edit:

CGSize size = [[CCDirector sharedDirector] winSize];

if ((sprite.y <= size.height) && (sprite.y >= 0) ) {
    // Set new position

} else {
   // sprite is colliding with top/bottom limits, do whatever you like, for example change direction

}
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191
  • CCSprite won't move unless you set its new position or you use CCMoveTo/By .. in both cases you have the control over where the sprite moves to so just put the logic restricting the y-coordinate there .. – Lukman Jan 03 '12 at 14:13
  • Take a look at my edit, I got that code but I am just not sure what to do in the if statements! – SimplyKiwi Jan 03 '12 at 23:04
  • for doing something like this for whole map check this : http://gamedev.stackexchange.com/questions/76067/how-to-prevent-showing-outside-of-game-map-cocos2d-x-top-down-game – Emadpres Jun 18 '14 at 19:01

2 Answers2

4

To limit the sprite within a boundary, don't check the current position but check the new position instead. But, rather than using (possibly multiple) if conditions, you can use clamping method:

Technique 1 - using MIN and MAX combo:

CGPoint newPosition = ... (assign new position here using touch location or something)
sprite.position = ccp(newPosition.x, MAX(0, MIN(size.height, newPosition.y)));

Technique 2 - using clampf:

CGPoint newPosition = ... (assign new position here using touch location or something)
sprite.position = ccp(newPosition.x, clampf(newPosition.y, 0, size.height));
Lukman
  • 18,462
  • 6
  • 56
  • 66
  • What new position do you mean? And I do not understand how I could do this without if statements – SimplyKiwi Jan 04 '12 at 21:14
  • @iBradApps well, the new position that you meant in your code: `// Set new position` .. d'uh! – Lukman Jan 06 '12 at 00:26
  • That was the original issue :P What should that new position be in order for it to act like screen boundaries? – SimplyKiwi Jan 06 '12 at 02:16
  • @iBradApps the touch location or the thing that determines where the sprite will move to. The logic that I give in my answer adjust the new position so that it will not go outside the boundary. – Lukman Jan 06 '12 at 11:27
  • My sprite is controlled by the UIAccelerometer. So lets say this, if the sprite x is less than or equal to 0 it will be touching the top boundary. Does that mean I should set the x to 1 to act like a boundary? – SimplyKiwi Jan 06 '12 at 12:44
  • @iBradApps Err .. the codes I posted will take care of any adjustment if the new position is outside the boundary, that is it will put the sprite to the nearest point inside the boundary. That's why I said there's no need for any if condition because you can just throw any new position to the code and the code will fix it. – Lukman Jan 07 '12 at 14:23
  • I just did my way and set it 1 pixel into the screen and it works fine. Thanks anyway – SimplyKiwi Jan 07 '12 at 20:34
2
 CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite* sprite = [CCSprite node];
CGSize spriteSize = sprite.boundingBox.size;
if ((sprite.position.y + spriteSize.height/2 < 0 )||(sprite.position.y + spriteSize.height/2 > winSize.height) ) {
    //Sprite is out of screen
}

not tested, but since you have the anchorpoint at 0.5, 0.5 as standard this should work for you

chrs
  • 5,906
  • 10
  • 43
  • 74