1

我正在构建一个简单的游戏应用程序,您必须在其中将一个球从另一个球上移开。但是我的代码有问题,请帮忙。当我构建并运行它时,我收到 2 条错误消息。我不明白问题是什么。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
//(X speed, Y speed) vvv
pos = CGPointMake(5.0,4.0);///////// this part here I get an error message saying assigning to CGPoint * (aka 'struct CGPoint*') from incompatible type 'CGPoint' (aka 'struct CGPoint')
}

- (IBAction)start {
[startbutton setHidden:YES];
randomMain = [NSTimer scheduledTimerWithTimeInterval:(0.03) target:(self) selector:@selector(onTimer) userInfo:nil repeats:YES];

}

-(void)onTimer {
[self checkCollision];

enemy.center = CGPointMake(enemy.center.x+pos->x,enemy.center.y+pos->y);

if (enemy.center.x > 320 || enemy.center.x < 0)
    pos->x = -pos->x;

if (enemy.center.y > 480 || enemy.center.y < 0)
    pos->y = -pos->y;

}

-(void)checkCollision {

if( CGRectIntersectsRect(player.frame,enemy.frame))
{

[randomMain invalidate];
[startbutton setHidden:NO];

CGRect frame = [player frame];
frame.origin.x = 137.0f;
frame.origin.y = 326.0;
[player setFrame:frame];

CGRect frame2 = [enemy frame];
frame2.origin.x =137.0f;
frame2.origin.y = 20.0;
[enemy setFrame:frame2];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"You Lost!" message:[NSString stringWithFormat:@"You Were Hit! Try Again"] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
    [alert show];
    [alert release];

}



}

-(void)touchesMoved: (NSSet *)touches withEvent: (UIEvent *)event {
UITouch *myTOuch = [[event allTouches] anyObject];
player.center = [myTouch locationInView:self.view];      /////// Here also I get an error message saying Assigning to 'CGPoint' (aka struct CGPoint') form incompatible type 'id'

////////////////// Also with that error message is Class method '+locationalView' not found (return type defaults to 'id')
}

@end
4

3 回答 3

8

在您的 .h 文件中,您是如何创建pos变量的?我想你加了一个 * :

CGPoint *pos;

去除那个 * :

CGPoint pos;

编辑(感谢乔纳森格林斯潘)

为什么使用 -> 运算符?我个人从未在 Objective-C 代码中见过它。尝试将它们更改为点:

if (enemy.center.x > 320 || enemy.center.x < 0)
    pos.x *= -1;
于 2012-08-16T12:03:16.417 回答
3

在您的 ViewController.h 文件中,编写此声明。

CGPoint pos;

在您的 ViewController.m 文件中,替换 pos.x 而不是 pos->x 和 pos.y 而不是 pos->y。

于 2012-08-16T12:18:25.817 回答
1

当您在 var 名称和您尝试实例化对象的 Class 之间看到 * 时,这意味着它是该对象的指针。当 * 不存在时,它不是指针而是硬值。

你把这些东西弄混了,忘了从 .h 文件中的 CGPoint 属性中删除 * 。修复它,错误就消失了。

CGPoint *pos ---> CGPoint pos

于 2012-08-16T12:04:51.750 回答