Accessing Value of a property of a derived class

Discussion in 'iOS Programming' started by wisaunders, Mar 23, 2010.

  1. wisaunders macrumors newbie

    Joined:
    Mar 23, 2010
    #1
    I'm having trouble getting the value of a property of my class. My class is called ball and I'd like to get it's velocity (which i set but can change) from my view control.

    Here is my ball.h code:
    Code:
    #import <Foundation/Foundation.h>
    
    @interface Ball : UIImageView {
    	CGPoint ballVelocity;
        int mXSpeed;
        int mYSpeed;
        float mAngle;
    }
    
    
    - (void)getVelocity:(CGPoint)speed;
    - (void)move;
    - (void)bounceHorizontal;
    - (void)bounceVertical;
    - (void)setSpeedX:(int)xSpeed Y:(int)ySpeed;
    - (void)setVelocity:(CGPoint)speed;
    - (CGPoint) ballVelocity;
    
    @end



    Here is my part of my ball.m code:
    Code:
    #import "Ball.h"
    
    
    @implementation Ball
    
    
    - (void)setVelocity:(CGPoint)speed {
        ballVelocity = speed;
    }


    Here is my viewVontroller.h
    Code:
    #import <UIKit/UIKit.h>
    
    
    @class Ball;
    
    @interface BallsOfFireViewController : UIViewController {
    	IBOutlet UIImageView *ball;
    	NSMutableArray *mBallArray;
    	CGPoint   ballVelocity;	
    	CGPoint   velocityz;
    	CGPoint   velocityz2;
    }
    
    @property(nonatomic, retain) IBOutlet UIImageView *ball;
    @property(retain, nonatomic) IBOutlet NSMutableArray *mBallArray;
    @property(nonatomic) CGPoint ballVelocity;
    
    - (void) checkCollision;
    @end


    and here is the relative code from my viewController.m

    Code:
    #import "BallsOfFireViewController.h"
    #import "Ball.h"
    
    //#define kGameStateRunning 1
    //#define kGameStatePaused  2
    
    #define kBallSpeedX 1
    #define kBallSpeedY 1
    
    @implementation BallsOfFireViewController
    @synthesize ball, mBallArray, ballVelocity;
    
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    	//self.gameState = kGameStatePaused;
    
    	
    	mBallArray = [[NSMutableArray alloc] init];
    	int i;
    	for (i = 0; i < 3; i++)
    	{
    		Ball* ballx=[[Ball alloc] initWithImage:[UIImage imageNamed:@"FireBall.png"]];
    		[mBallArray addObject:ballx];
    		[self.view addSubview:ballx];
    		
    		if (i == 14
    			){
    			//[ballx setBackgroundColor:([UIColor redColor])];	
    			ballx.image = [UIImage imageNamed:@"IceBall.png"];
    		}
    		
    		ballVelocity = CGPointMake(1,1);
    		[ballx setVelocity:ballVelocity];
    		
    		ballx.center = CGPointMake((random() % 14) * 20 + 20 ,(random() % 20) * 20 + 20);		
    
    	}
    	
    		
    	[NSTimer scheduledTimerWithTimeInterval:0.005
    									 target:self 
    								   selector:@selector(moveBall) 
    								   userInfo:nil 
    									repeats:YES];       	
    	
    }
    
    
    
    
    -(void)moveBall
    {
    	int i;
    	int numberOfBalls=[mBallArray count];
    	for (i = 0; i < numberOfBalls; i++)
    	{
    		
    		Ball* ballz=[mBallArray objectAtIndex:i]; 
    		
    		int i2;
    		for (i2 = 0; i2 < numberOfBalls; i2++)
    			if (i2 != i) {
    				Ball* ballz2=[mBallArray objectAtIndex:i2]; 
    				
    				if (CGRectIntersectsRect(ballz.frame, ballz2.frame)) {
    					
    					if (sqrt(pow(ballz.center.x - ballz2.center.x,2) + pow(ballz.center.y - ballz2.center.y,2)) <= pow(ballz.frame.size.width,2)) {
    	
    						
    						[ballz move];
    						 velocityz  = ballz.ballVelocity;
    						 velocityz2 = ballz2.ballVelocity;
    						
    						if (velocityz.x == velocityz2.x * -1 && velocityz.y == velocityz2.y * -1) {
    							
    							if (pow(ballz.center.x - ballz2.center.x,2) > pow(ballz.center.y - ballz2.center.y,2)) {
    								
    								[ballz bounceHorizontal];
    								[ballz2 bounceHorizontal];	
    							} else {
    								[ballz bounceVertical];
    								[ballz2 bounceVertical];	
    							}
    							
    						}						
    						
    					}
    					
    					
    				}
    
    			} 
    
    		[ballz move];
    	}
    
    	
    }
    


    When I get to the following line in debug mode the game in my iPhone Simulator freezes and I do not see any errors in xCode (probably don't know where to look):

    Code:
    velocityz  = ballz.ballVelocity;
    I think I'm just not setting up my ball class correctly to be able to pull back the value of the ballVelocity variable. Can anyone point me in the correct direction?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I don't see a direct problem from a quick glance, but when it freezes go to Run > Debugger and see what it's showing.

    Also you should be consistent with your use of properties and accessor methods. You use ballz.ballVelocity but ballVelocity is not defined as a @property, so it should be [ballz ballVelocity] - same goes for all the other attributes of your Ball class.

    Additionally, when you call [[Ball alloc] initWithImage...], you should add an autorelease to that, otherwise you're leaking:
    Code:
    Ball* ballx=[[[Ball alloc] initWithImage:[UIImage imageNamed:@"FireBall.png"]] autorelease];
     
  3. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #3
    To add to what kainjow has mentioned, I don't see where you're trying to get velocity from a ball, but the following line stands out to me:

    - (void)getVelocity: (CGPoint)speed;

    Not only that you're passing in a value to a getter, but in Obj-C you don't access a property using getX ... this isn't a JavaBean - use:
    - (CGPoint)velocity;
     
  4. wisaunders thread starter macrumors newbie

    Joined:
    Mar 23, 2010
    #4
    asm___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION__ 0x01c1c0001:1

    Thanks

    here's what i see in the dubugger when it stops:

    [NSObject doesNotRecognizeSelector:]

    this is the line of code it is trying to execute:

    velocityz = ballz.ballVelocity

    Still don't know what to do
     
  5. Guiyon macrumors 6502a

    Joined:
    Mar 19, 2008
    Location:
    North Shore, MA
    #5
    If I am reading the code correctly, ballz is defined as:
    Code:
    Ball* ballz2=[mBallArray objectAtIndex:i];
    Looking at your interface for the Ball class, there is no ballVelocity property defined so using
    Code:
    ballz2.ballVelocity
    is incorrect and should be
    Code:
    [ballz ballVelocity]
    In order for it to work as a property you would need to add a property definition to the Ball class (like in BallsOfFireViewController) and create the property using either @synthesize or @dynamic. Additionally, your methods for the property (assuming you dynamically assign them) would need to be fixed as, by default, the compiler would be looking for methods
    Code:
    - (CGPoint)ballVelocity;
    - (void)setBallVelocity:(CGPoint);
    as your accessor and mutator methods (your accessor has the right signature but your mutator needs to be changed).
     

Share This Page