Reputation: 15
Im building a simple game app, where you have to move a ball away from another one. However Im having an issue with my code, please help. When I build and run it I get 2 error messages. I don't understand what the problem is.
#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
Upvotes: 1
Views: 2945
Reputation: 6718
In your ViewController.h file, Write this declaration.
CGPoint pos;
In your ViewController.m file, replace pos.x instead of pos->x and pos.y instead of pos->y.
Upvotes: 3
Reputation: 2999
In your .h file how did you create the pos
variable?
I think you added a * :
CGPoint *pos;
remove the * :
CGPoint pos;
EDIT (thanks Jonathan Grynspan)
Why do you use the -> operator? I personally never saw it in Objective-C code. Try changing them to dots:
if (enemy.center.x > 320 || enemy.center.x < 0)
pos.x *= -1;
Upvotes: 8
Reputation: 7573
When you see a * between your var name and the Class of which you try to instantiate an object it means it is a pointer of that object. When the * is not there it is not a pointer but a hard value.
You mixed up those things and forgot to remove the * from your CGPoint property inside your .h file. Fix that and the error is gone.
CGPoint *pos ---> CGPoint pos
Upvotes: 1