07-28-2010, 11:19 AM
#1 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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;
}
}
07-28-2010, 11:31 AM
#2 (permalink )
noob iphone developer
Join Date: Jul 2008
Location: Denmark
Posts: 69
Try moving the close bracket
Code:
paddleblue.center = xLocation;
} <==== Try to move this
if (CGRectIn
07-28-2010, 11:34 AM
#3 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
Unfortunately that doesn't work - the 3 errors still appear, and it takes quite a while (longer than usual) to compile...
07-28-2010, 11:36 AM
#4 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 28
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 11:39 AM .
07-28-2010, 11:38 AM
#5 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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?
07-28-2010, 11:43 AM
#6 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 28
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
07-28-2010, 11:46 AM
#7 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 28
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
07-28-2010, 11:48 AM
#8 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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?
07-28-2010, 11:52 AM
#9 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 28
Quote:
Originally Posted by
Ubermatik
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
07-28-2010, 12:04 PM
#10 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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.
07-28-2010, 12:11 PM
#11 (permalink )
Registered Member
Join Date: Apr 2010
Posts: 28
Look at the similar line above...it looks like you have CGPoint where you should have CGPointMake.
__________________
---
Paul
New iPhone Developer
07-28-2010, 01:19 PM
#12 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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...
07-28-2010, 01:50 PM
#13 (permalink )
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 62
Posts: 898
Quote:
Originally Posted by
Ubermatik
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?
07-28-2010, 01:55 PM
#14 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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.
07-28-2010, 02:26 PM
#15 (permalink )
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 62
Posts: 898
So what does the error say now, exactly?
07-28-2010, 02:33 PM
#16 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
"Expected expression before CGPoint"
07-28-2010, 02:39 PM
#18 (permalink )
Registered Member
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 62
Posts: 898
Post what the code looks like now, after you fixed the CGPoint into CGpointMake.
07-28-2010, 02:47 PM
#19 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
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.
07-28-2010, 04:52 PM
#20 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
*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.
07-29-2010, 05:14 AM
#21 (permalink )
Registered Member
Join Date: Jul 2010
Posts: 19
*Bump
My last try... anybody have any ideas? Thanks again.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 678
52 members and 626 guests
alksys7 , amatoria , anonymous@ , Balillas , BillSmith , bob8caldwell , brentmc79 , CHV , crunkstar , darrentousignant , david77 , dcinqc , desk110 , e.desk , e.dsk100 , e.dsk112 , edesk01 , elektrobank , ftwhere , garyshaf , gtyt38 , h2h , HookieTookie , ignicolist , iGuessSo , JasonR , jbro , jessk2herb , Kryckter , listingboat , lukasluk , MarkC , masc2279 , MiniRobinho , moehac , mofu , natanrolnik , nobre84 , Nuncha , raheel , realms , ryanduff , shebinc , Speed , strick242 , virvalid , Ziggy
Most users ever online was 965, 06-30-2010 at 04:26 AM.
» Stats
Members: 51,375
Threads: 52,830
Posts: 225,477
Top Poster: BrianSlick (3,576)
Welcome to our newest member, darrentousignant