CGPointMake! Coding error message

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

Answers


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.


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;

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


Need Your Help

Codeigniter PHP url segments for users profile

php mysql codeigniter

I need it so that when i type in domain.com/user/1 the one gets pulled in as a variable and then can be used to get the correct database values. Right now the variable is set manually with domain.com/

Inserting items on default menus with extension org.eclipse.plugin.menus

java eclipse menu eclipse-plugin

I'm trying to add some options on the menu programatically, I already found out how to add a menu after the menu "Window", but I want to add a option inside the default menu (more specifically, add...