目前我正在尝试通过查看CCSprite是否越过屏幕的顶部或底部来检查屏幕边界。问题是,我真的想将下面的2个if语句合并到 1 语句中。无论如何,我将要做的唯一困难是以下。我在下面做的是向内移动CCSprite 1点以强制实际的屏幕边界。但我只是不知道如何使用if语句来做到这一点。
无论如何这里是方法:
- (void)checkScreenBoundaries {
CGSize size = [[CCDirector sharedDirector] winSize];
if (sprite.position.y <= 0) {
sprite.position = ccp(sprite.position.x, 1);
died = YES;
} else if (sprite.position.y >= size.height) {
sprite.position = ccp(sprite.position.x, size.height - 1);
died = YES;
}
}
任何人都可以告诉我如何将这两者合并为1吗?
谢谢!
答案 0 :(得分:2)
if ((sprite.position.y <= 0) || (sprite.position.y >= size.height)) {
sprite.position = ccp(sprite.position.x, (sprite.position.y <= 0) ? 1 : (size.height - 1));
died = YES;
}
答案 1 :(得分:2)
CGFloat yNew = MAX(1, MIN(sprite.position.y, size.height - 1));
if (yNew != sprite.position.y) {
sprite.position = ccp(sprite.position.x, yNew);
died = YES;
}