Beginner Programmer - xcode error

Discussion in 'iOS Programming' started by macdude3, Jun 16, 2010.

  1. macdude3 macrumors member

    Joined:
    Jun 16, 2010
    #1
    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.
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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?
     
  3. macdude3 thread starter macrumors member

    Joined:
    Jun 16, 2010
  4. HawaiiMacAddict macrumors 6502a

    Joined:
    Dec 28, 2006
    Location:
    On one of my Macs of course
    #4
    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.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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.
     
  6. macdude3 thread starter macrumors member

    Joined:
    Jun 16, 2010
    #6
    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.
     
  7. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    The viewDidLoad error is probably related to your braces-matching problems in touchesMoved:.
     
  8. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #8
    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
    
     

Share This Page