如何纠正这个程序的错误

时间:2012-04-22 18:46:01

标签: ios

我有一个代码,但在这段代码中有一个错误,我不知道为什么我有这个错误。 我把这个代码放在一边,因为我希望我的球最少击中边界大豆屏幕。 你可以看到我的代码:

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);

错误是“在结构CGRect中没有成员命名为”x“。

感谢您的帮助

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

但是我在BOOL上有一个错误预期。

你能帮助我吗

1 个答案:

答案 0 :(得分:5)

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

编辑:

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

CGRectMake(s.origin.x-50, s.origin.y, width,height);