我正在尝试检查我的 SKSpriteNode 在拖动手势期间是否会保持在屏幕范围内。我已经到了可以肯定我解决问题的逻辑是正确的地步,但是我的实现是错误的。基本上,在玩家从平移移动之前,程序会检查它是否在边界内。这是我的代码:
-(CGPoint)checkBounds:(CGPoint)newLocation{
CGSize screenSize = self.size;
CGPoint returnValue = newLocation;
if (newLocation.x <= self.player.position.x){
returnValue.x = MIN(returnValue.x,0);
} else {
returnValue.x = MAX(returnValue.x, screenSize.width);
}
if (newLocation.y <= self.player.position.x){
returnValue.y = MIN(-returnValue.y, 0);
} else {
returnValue.y = MAX(returnValue.y, screenSize.height);
}
NSLog(@"%@", NSStringFromCGPoint(returnValue));
return returnValue;
}
-(void)dragPlayer: (UIPanGestureRecognizer *)gesture {
CGPoint translation = [gesture translationInView:self.view];
CGPoint newLocation = CGPointMake(self.player.position.x + translation.x, self.player.position.y - translation.y);
self.player.position = [self checkBounds:newLocation];
}
出于某种原因,我的播放器正在离开屏幕。我认为我对 MIN & MAX 宏的使用可能是错误的,但我不确定。