Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-28-2010, 12:19 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default Coding problem ("Expected identifier or '(' before 'if'")

Hello,

First off, I'm new here (as you may be able to tell!) so I'm sorry if this is the wrong section...

I have a problem and was wondering if you could help me: I'm following a tutorial at the moment, which involves building a simple Pong-style game. I've followed it letter for letter (apart form the fact I have changed the names of a few objects etc.), but have come across a problem - it's an error that reads: "Expected identifier or '(' before 'if'.
Do you have a solution to the problem, or know of a way in which I could solve it? I'm really eager to become familiar with the SDK, so I'd love to know what the problem is too. Thanks in advance! (the problem line is highlighted in red) (the tutorial is here, if needed)

Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.gameState = kGameStatePaused;
	pucVelocity = CGPointMake(kPucSpeedX,KPucSpeedY);
	[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}

-(void) gameLoop {
	if(gameState == kGameStateRunning) {
		
		puc.center = CGPointMake(puc.center.x + pucVelocity.x , puc.center.y + pucVelocity.y);
		
		if(puc.center.x > self.view.bounds.size.width || puc.center.x < 0) {
			pucVelocity.x = -pucVelocity.x;
		}
		
		if(puc.center.y > self.view.bounds.size.height || puc.center.y < 0) {
			pucVelocity.y = -pucVelocity.y;
		}
	} else {
		if (tapToBegin.hidden) {
			tapToBegin.hidden = NO;
		}
	}
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	if (gameState == kGameStatePaused) {
		tapToBegin.hidden = YES;
		gameState = kGameStateRunning;
	} else if(gameState == kGameStateRunning) {
		[self touchesMoved:touches withEvent:event];
	}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	CGPoint xLocation = CGPointMake(location.x,paddleblue.center.y);
	paddleblue.center = xLocation;
}

if (CGRectIntersectsRect(puc.frame,paddleblue.frame)) {
	if(puc.center.y < paddleblue.center.y) {
		pucVelocity.y = -pucVelocity.y;
		NSLog(@"%f %f",puc.center,paddlegreen.center);
	}
}

if (CGRectIntersectsRect(puc.frame,paddlegreen.frame)) {
	if(puc.center.y > paddlegreen.center.y) {
		pucVelocity.y = -pucVelocity.y;
	}
}

//AI
if (puc.center.y <= self.view.center.y) {
	if(puc.center.x < paddlegreen.center.x) {
		CGPoint compLocation = CGPointMake(paddlegreen.center.x - kCompMoveSpeed, paddlegreen.center.y);
		paddlegreen.center = compLocation;
	}
	
	if(puc.center.x > paddlegreen.center.x) {
		CGPoint compLocation = CGPoint(paddlegreen.center.x + kCompMoveSpeed, paddlegreen.center.y);
		paddlegreen.center = compLocation;
	}
}
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 12:31 PM   #2 (permalink)
noob iphone developer
 
Join Date: Jul 2008
Location: Denmark
Posts: 230
Default

Try moving the close bracket

Code:
	paddleblue.center = xLocation;
} <==== Try to move this

if (CGRectIn
Trisect Development is offline   Reply With Quote
Old 07-28-2010, 12:34 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

Unfortunately that doesn't work - the 3 errors still appear, and it takes quite a while (longer than usual) to compile...
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 12:36 PM   #4 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 49
Default

Your if conditions are just kinda floating out there...If thats a class, then they should reside within a method.

My guess, without looking at the tutorial, is that your if's should be moved to the touchesMoved method.
__________________
Paul
New iPhone Developer

Last edited by Paul10; 07-28-2010 at 12:39 PM.
Paul10 is offline   Reply With Quote
Old 07-28-2010, 12:38 PM   #5 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

I'm very new to all this - hence why I'm following a tutorial. Could you elaborate? Should they be included within the - (void) touchesMoved part, instead of just below it?
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 12:43 PM   #6 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 49
Default

I'd try just moving your If conditions inside the touchesMoved code. That should fix your errors...now, if its what the tutorial wanted, I dont know
__________________
Paul
New iPhone Developer
Paul10 is offline   Reply With Quote
Old 07-28-2010, 12:46 PM   #7 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 49
Default

Ok, I took a glance at the tutorial. It looks like for your IF's, it wanted to you to put them "inside" the gameloop method.

"Inside the gameLoop method of iTennisViewController.m add the following code"
__________________
Paul
New iPhone Developer
Paul10 is offline   Reply With Quote
Old 07-28-2010, 12:48 PM   #8 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

Okay... how would I go about doing that? Do I simply indent all my code so it's beneath the touchesMoved code? Or should I get rid of all the {/}'s?
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 12:52 PM   #9 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 49
Default

Quote:
Originally Posted by Ubermatik View Post
Okay... how would I go about doing that? Do I simply indent all my code so it's beneath the touchesMoved code? Or should I get rid of all the {/}'s?
You'd just cut them from their location and put them inside the gameloop..so gameloop would be:

Code:
-(void) gameLoop {
	if(gameState == kGameStateRunning) {
		
		puc.center = CGPointMake(puc.center.x + pucVelocity.x , puc.center.y + pucVelocity.y);
		
		if(puc.center.x > self.view.bounds.size.width || puc.center.x < 0) {
			pucVelocity.x = -pucVelocity.x;
		}
		
		if(puc.center.y > self.view.bounds.size.height || puc.center.y < 0) {
			pucVelocity.y = -pucVelocity.y;
		}
	} else {
		if (tapToBegin.hidden) {
			tapToBegin.hidden = NO;
		}
	}

	if (CGRectIntersectsRect(puc.frame,paddleblue.frame)) {
		if(puc.center.y < paddleblue.center.y) {
			pucVelocity.y = -pucVelocity.y;
			NSLog(@"%f %f",puc.center,paddlegreen.center);
		}
	}

	if (CGRectIntersectsRect(puc.frame,paddlegreen.frame)) {
		if(puc.center.y > paddlegreen.center.y) {
			pucVelocity.y = -pucVelocity.y;
		}
	}

	//AI
	if (puc.center.y <= self.view.center.y) {
		if(puc.center.x < paddlegreen.center.x) {
			CGPoint compLocation = CGPointMake(paddlegreen.center.x - kCompMoveSpeed, paddlegreen.center.y);
			paddlegreen.center = compLocation;
		}
	
		if(puc.center.x > paddlegreen.center.x) {
			CGPoint compLocation = CGPoint(paddlegreen.center.x + kCompMoveSpeed, paddlegreen.center.y);
			paddlegreen.center = compLocation;
		}
	}

} //End of gameloop
__________________
Paul
New iPhone Developer
Paul10 is offline   Reply With Quote
Old 07-28-2010, 01:04 PM   #10 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

Okay, that rids the 3 original errors - but now I get a new error:

Code:
//AI
	if (puc.center.y <= self.view.center.y) {
		if(puc.center.x < paddlegreen.center.x) {
			CGPoint compLocation = CGPointMake(paddlegreen.center.x - kCompMoveSpeed, paddlegreen.center.y);
			paddlegreen.center = compLocation;
		}
		
		if(puc.center.x > paddlegreen.center.x) {
			CGPoint compLocation = CGPoint(paddlegreen.center.x + kCompMoveSpeed, paddlegreen.center.y);
			paddlegreen.center = compLocation;
		} 
	}
}
At the highlighted line, I get the message "Expected expression before CGPoint". Any ideas? Thanks for your patience.
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 01:11 PM   #11 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 49
Default

Look at the similar line above...it looks like you have CGPoint where you should have CGPointMake.
__________________
Paul
New iPhone Developer
Paul10 is offline   Reply With Quote
Old 07-28-2010, 02:19 PM   #12 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

Ah! Thanks for spotting that! It would have been a pain to find later on, when I realised the game doesn't work properly!

The error still exists, however...
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 02:50 PM   #13 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

Quote:
Originally Posted by Ubermatik View Post
Ah! Thanks for spotting that! It would have been a pain to find later on, when I realised the game doesn't work properly!

The error still exists, however...
You mean the error that says "Expected expression before CGPoint" even though you now don't have CGPoint, but CGPointMake?
RLScott is offline   Reply With Quote
Old 07-28-2010, 02:55 PM   #14 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

Yes - as in, it's referring to the first part of that line, where it says:

Code:
CGPoint compLocation = CGPointMake(paddlegreen.center.x + kCompMoveSpeed, paddlegreen.center.y);
At least, I think that's what it is referring to.
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 03:26 PM   #15 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

So what does the error say now, exactly?
RLScott is offline   Reply With Quote
Old 07-28-2010, 03:33 PM   #16 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

"Expected expression before CGPoint"
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 03:33 PM   #17 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

I found these solutions - but unfortunately, non of them work...

Objective-C Tip: Switch Statements | Scyanide's World
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 03:39 PM   #18 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,525
Default

Post what the code looks like now, after you fixed the CGPoint into CGpointMake.
RLScott is offline   Reply With Quote
Old 07-28-2010, 03:47 PM   #19 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

Code:
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	self.gameState = kGameStatePaused;
	pucVelocity = CGPointMake(kPucSpeedX,KPucSpeedY);
	[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
}

-(void) gameLoop {
	if(gameState == kGameStateRunning) {
		
		puc.center = CGPointMake(puc.center.x + pucVelocity.x , puc.center.y + pucVelocity.y);
		
		if(puc.center.x > self.view.bounds.size.width || puc.center.x < 0) {
			pucVelocity.x = -pucVelocity.x;
		}
		
		if(puc.center.y > self.view.bounds.size.height || puc.center.y < 0) {
			pucVelocity.y = -pucVelocity.y;
		}
	} else {
		if (tapToBegin.hidden) {
			tapToBegin.hidden = NO;
		}
	}
	
		if (CGRectIntersectsRect(puc.frame,paddleblue.frame)) {
		if(puc.center.y < paddleblue.center.y) {
			pucVelocity.y = -pucVelocity.y;
			NSLog(@"%f %f",puc.center,paddlegreen.center);
			}
		}
	
		if (CGRectIntersectsRect(puc.frame,paddlegreen.frame)) {
			if(puc.center.y > paddlegreen.center.y) {
			pucVelocity.y = -pucVelocity.y;
			}
		}
		
		//Begin AI
		if (puc.center.y <= self.view.center.y) {
			if(puc.center.x < paddlegreen.center.x) {
				CGPoint compLocation = CGPointMake(paddlegreen.center.x - kCompMoveSpeed, paddlegreen.center.y);
				paddlegreen.center = compLocation;
			}
	
			if(puc.center.x > paddlegreen.center.x) {
				CGPoint compLocation = CGPointMake(paddlegreen.center.x + kCompMoveSpeed, paddlegreen.center.y);
				paddlegreen.center = compLocation;
			} //End AI
		}
	} //End of GameLoop
Again, the line the error comes up in is highlighted in red.
Ubermatik is offline   Reply With Quote
Old 07-28-2010, 05:52 PM   #20 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

*Bump

Any ideas?
I'm sorry to be a pain, but I'm really eager to learn this, and getting this solved would help enormously.
Ubermatik is offline   Reply With Quote
Old 07-29-2010, 06:14 AM   #21 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 19
Default

*Bump

My last try... anybody have any ideas? Thanks again.
Ubermatik is offline   Reply With Quote
Old 01-22-2011, 08:22 PM   #22 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 3
Default

Quote:
Originally Posted by Ubermatik View Post
*Bump

My last try... anybody have any ideas? Thanks again.
Get rid of the "}" before else that should fix it
You have } else { and it should be just else{
andresavcc is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 158,294
Threads: 89,032
Posts: 379,807
Top Poster: BrianSlick (7,086)
Welcome to our newest member, Marian123
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:42 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0