Accessing Value of a property of a derived class

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

  1. wisaunders macrumors newbie

    Mar 23, 2010
    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:
    #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;

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

    Here is my viewVontroller.h
    #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;

    and here is the relative code from my viewController.m

    #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];
    = CGPointMake((random() % 14) * 20 + 20 ,(random() % 20) * 20 + 20);		
    	[NSTimer scheduledTimerWithTimeInterval:0.005
    	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( -,2) + pow( -,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( -,2) > pow( -,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):

    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


    Jun 15, 2000
    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:
    Ball* ballx=[[[Ball alloc] initWithImage:[UIImage imageNamed:@"FireBall.png"]] autorelease];
  3. plinden macrumors 68040


    Apr 8, 2004
    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

    Mar 23, 2010


    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

    Mar 19, 2008
    North Shore, MA
    If I am reading the code correctly, ballz is defined as:
    Ball* ballz2=[mBallArray objectAtIndex:i];
    Looking at your interface for the Ball class, there is no ballVelocity property defined so using
    is incorrect and should be
    [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
    - (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