[英]CGPointMake! Coding error message
我正在構建一個簡單的游戲應用程序,在這里您必須將球移到另一個。 但是我的代碼有問題,請幫忙。 構建並運行它時,我收到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
在您的.h文件中,如何創建pos
變量? 我認為您添加了*:
CGPoint *pos;
去除 * :
CGPoint pos;
編輯 (感謝喬納森·格林斯潘)
為什么使用->運算符? 我個人從未在Objective-C代碼中看到它。 嘗試將它們更改為點:
if (enemy.center.x > 320 || enemy.center.x < 0)
pos.x *= -1;
在您的ViewController.h文件中,編寫此聲明。
CGPoint pos;
在您的ViewController.m文件中,用pos.x代替pos-> x替換pos.y代替pos-> y。
當您在變量名和試圖實例化對象的類之間看到*時,表示它是該對象的指針。 當*不存在時,它不是指針,而是硬值。
您混合了這些內容,卻忘記從.h文件中的CGPoint屬性中刪除*。 修復該錯誤,該錯誤消失了。
CGPoint * pos ---> CGPoint pos
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.