0

我有一个代码,但在这个代码中有一个错误,我不知道为什么我有这个错误。我输入此代码是因为我希望我的球至少击中屏幕的边框。你可以看到我的代码:

CGPoint ballCenter = ball.center;
CGRect ballRect = CGRectMake(50, 73, 50, 50); // an arbitrary location for the ball, you would normally use the frame property of the ball here
CGRect s = [UIScreen mainScreen].bounds;
CGRect adjustedScreenRect = CGRectMake(s.x-50   // take the ball's (width or height) and divide it by two to get the distance to the center. Then multiply it by two and subtract from the appropriate dimension(x offset (width), y offset (height), width (width), height (height)). 
BOOL isOnScreen = CGRectContainsPoint(adjutedScreenRect, ballCenter);
// do whatever with the BOOL from the above line...

我在这一行有一个错误:

CGRect adjustedScreenRect = CGRectMake(s.x-50   // take the ball's (width or height) and divide it by two to get the distance to the center. Then multiply it by two and subtract from the appropriate dimension(x offset (width), y offset (height), width (width), height (height)). 
BOOL isOnScreen = CGRectContainsPoint(adjutedScreenRect, ballCenter);

错误是“struct CGRect 中没有名为“x”的成员”。

谢谢你的帮助

答案是CGRect adjustedScreenRect = CGRectMake(s.origin.x-50
BOOL isOnScreen = CGRectContainsPoint(adjutedScreenRect, ballCenter);

但是我在 BOOL 上有一个错误 Expected )。

你能帮助我吗

4

1 回答 1

5

我想你的意思是s.origin.x,不是s.x。因为 CGRect 是一个 CGPoint 和一个 CGSize 的结构,所以如果不指定要首先访问的结构的哪一部分,就无法直接访问 CGRect 的 x 值。

编辑:

您从未真正关闭 CGRect 的括号,或满足所有 4 个参数。尝试这个:

CGRectMake(s.origin.x-50, s.origin.y, width,height);
于 2012-04-22T18:49:54.703 回答