Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

macdude3

macrumors member
Original poster
Alright so I am just starting out with programming and I have been following the tutorials in the book called beginning iphone games development. I am on the first tutorial which is making a brick breaker game. However when I build and run I get 4 errors and warnings in my viewcontroller.m file. The errors are 'expected declaration or statement at end of input' and 'view did load undeclared'.

Here is my header:

Code:
 #import <UIKit/UIKit.h>

@interface Ball_Game_2ViewController : UIViewController {
	UILabel *scoreLabel; 
	int score;
	UIImageView *ball;
	CGPoint ballMovement;
	UIImageView *paddle;
	float touchOffset;
}
@property (nonatomic, retain) IBOutlet UILabel *scoreLabel;
@property (nonatomic, retain) IBOutlet UIImageView *ball;
@property (nonatomic, retain) IBOutlet UIImageView *paddle;

- (void)initializeTimer;
- (void)animateBall:(NSTimer *)theTimer;

@end

And here is my main

Code:
#import "Ball_Game_2ViewController.h"

@implementation Ball_Game_2ViewController
@synthesize scoreLabel;
@synthesize ball;
@synthesize paddle;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject]; 
	touchOffset = paddle.center.x -
		[touch locationInView:touch.view].x;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	float distanceMoved = }
	([touch locationInView:touch.view].x + touchOffset) -
	paddle.center.x; 
float newX = paddle.center.x + distanceMoved;
if (newX > 30 && newX < 290)
	paddle.center = CGPointMake( newX, paddle.center.y ); 
if (newX > 290)
	paddle.center = CGPointMake( 290, paddle.center.y ); 
if (newX < 30)
	paddle.center = CGPointMake( 30, paddle.center.y );

- (void)viewDidLoad {
    [super viewDidLoad];
	ballMovement = CGPointMake(4,4); 
	[self initializeTimer];
}
{- (void)initializeTimer 
	float theInterval = 1.0/30.0; 
	[NSTimer scheduledTimerWithTimeInterval:theInterval target:self
	selector:@selector(animateBall:) userInfo:nil repeats:YES];
}
- (void)animateBall:(NSTimer *)theTimer { 
	ball.center = CGPointMake(ball.center.x+ballMovement.x,
	ball.center.y+ballMovement.y);
	if(ball.center.x > 300 || ball.center.x < 20)
		ballMovement.x = -ballMovement.x;
	if(ball.center.y > 440 || ball.center.y < 40)
		ballMovement.y = -ballMovement.y;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
	
}

- (void)viewDidUnload {

}


- (void)dealloc {
	[scoreLabel release];
	[ball release]
	[paddle release]
	
    [super dealloc];
}

@end

Thanks for any help.
 
Guess I'd start by making sure your braces are properly matched and in the right place. For example:
Code:
[COLOR="Red"][B]{[/B][/COLOR]- (void)initializeTimer
Why is that curly-brace at the start of the line?
 
I deleted it and it didn't fix anything.

Aloha macdude3,

You still need the curly brace, but after the name of the method. For example, your method should look something like this:

Code:
- (void)initializeTimer {
        float theInterval = 1.0/30.0; 
	[NSTimer scheduledTimerWithTimeInterval:theInterval target:self
	selector:@selector(animateBall:) userInfo:nil repeats:YES];
}

Look in the preferences for Xcode - you can set it up so that once you type in the leading curly brace, the trailing curly brace is automatically inserted, but you still need to be careful of their placement.
 
I deleted it and it didn't fix anything.
Did I tell you to delete it? No. Did I tell you that was the only thing needing fixing? No again. Brace-matching is an important basic skill of Objective-C (as well as other languages) coding. Make sure you have mastered it before you continue with the real coding.
 
Thanks, I replaced the curly brace and I was able to eliminate one error, but now it is still saying 'viewdidload' undeclared and 'expected declaration or statement at end of input'. The first error sounds like I need to define it in the header? And the second one is odd because I do have the command @end as the very final command of my main. Thanks for the help. I am still trying to master the basic concepts.
 
The viewDidLoad error is probably related to your braces-matching problems in touchesMoved:.
 
I find this code hard to read but try -

Code:
#import "Ball_Game_2ViewController.h"

@implementation Ball_Game_2ViewController
@synthesize scoreLabel;
@synthesize ball;
@synthesize paddle;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject]; 
	touchOffset = paddle.center.x -
		[touch locationInView:touch.view].x;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	float distanceMoved =
	([touch locationInView:touch.view].x + touchOffset) -
	paddle.center.x; 
float newX = paddle.center.x + distanceMoved;
if (newX > 30 && newX < 290)
	paddle.center = CGPointMake( newX, paddle.center.y ); 
if (newX > 290)
	paddle.center = CGPointMake( 290, paddle.center.y ); 
if (newX < 30)
	paddle.center = CGPointMake( 30, paddle.center.y );
}
- (void)viewDidLoad {
    [super viewDidLoad];
	ballMovement = CGPointMake(4,4); 
	[self initializeTimer];
}
- (void)initializeTimer {
	float theInterval = 1.0/30.0; 
	[NSTimer scheduledTimerWithTimeInterval:theInterval target:self
	selector:@selector(animateBall:) userInfo:nil repeats:YES];
}
- (void)animateBall:(NSTimer *)theTimer { 
	ball.center = CGPointMake(ball.center.x+ballMovement.x,
	ball.center.y+ballMovement.y);
	if(ball.center.x > 300 || ball.center.x < 20)
		ballMovement.x = -ballMovement.x;
	if(ball.center.y > 440 || ball.center.y < 40)
		ballMovement.y = -ballMovement.y;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
	
}

- (void)viewDidUnload {

}


- (void)dealloc {
	[scoreLabel release];
	[ball release]
	[paddle release]
	
    [super dealloc];
}

@end
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.