我正在建立一个简单的游戏应用程序,你必须将球从另一个球移开。但是,我的代码有问题,请帮忙。当我构建并运行它时,我收到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
答案 0 :(得分:8)
在你的.h文件中,你是如何创建pos
变量的?
我想你添加了一个*:
CGPoint *pos;
删除*:
CGPoint pos;
编辑(感谢Jonathan Grynspan)
为什么使用 - &gt;运营商?我个人从未在Objective-C代码中看到它。 尝试将它们更改为点:
if (enemy.center.x > 320 || enemy.center.x < 0)
pos.x *= -1;
答案 1 :(得分:3)
在ViewController.h文件中,编写此声明。
CGPoint pos;
在ViewController.m文件中,将pos.x替换为pos-&gt; x和pos.y而不是pos-&gt; y。
答案 2 :(得分:1)
如果在var名称和尝试实例化对象的Class之间看到*,则表示它是该对象的指针。当*不存在时,它不是指针而是硬值。
你混淆了这些东西,忘了从.h文件中的CGPoint属性中删除*。解决了这个问题,错误消失了。
CGPoint * pos ---&gt; CGPoint pos