Objective-C property question.

Nibbie
Posts: 1
Joined: 2009.02
Post: #1
Hello all, First off i would like to apologize for the extreme noobness of my question but with my extremely new understanding of Objective-C I can't seem to figure out the following:

I created a class called GameObject:
GameObject.h:
Code:
@interface GameObject : NSObject
{
    //for now we will just store these in individual floats.
    //in the future i would like to have a specific datatype to contain these.
    float position_x;
    float position_y;
    float velocity_x;
    float velocity_y;
    
    //what is this objects name?
    NSString *name;
}

@property float position_x;
@property float position_y;
@property float velocity_x;
@property float velocity_y;
@property (nonatomic, retain) NSString *name;

-(void)setup:(NSString *)NameString;

@end

GameObject.m:
Code:
#import "GameObject.h"


@implementation GameObject

@synthesize position_x;
@synthesize position_y;
@synthesize velocity_x;
@synthesize velocity_y;
@synthesize name;

-(void)setup:(NSString *)NameString
{
    position_x = 0.0f;
    position_y = 0.0f;
    velocity_x = 0.0f;
    velocity_y = 0.0f;
    name = NameString;
}

-(void)dealloc
{
    [name release];
    [super dealloc];
}

@end

The app I trying to make is just a simple ball bouncing around the screen so i started with the OpenGL-ES template from XCode.

I added the following to the EAGL.h
Code:
@interface EAGLView : UIView {
...  
    GameObject *ball;
}

@property (nonatomic, retain) GameObject *ball;

And then I try to do the following in EAGL.m:
Code:
@implementation EAGLView
...
@synthesize ball;
Code:
- (id)initWithCoder:(NSCoder*)coder {
    
   ...
        [ball setup:@"ball"];
        ball.position_x = 50.0f;
        ball.position_y = 50.0f;
        ball.velocity_x = 1.0f;
        ball.velocity_y = 1.0f;
    }
    return self;
}

However, when i debug past all of my initialization, ball.name = invalid and all of the floats = 0;

Am I setting up my variables the wrong way or am I making some other silly mistake when it comes to Properties? I dunno this just seems like it should be really easy to figure out but I can't get it to work and it's making me feel like an R-Tard Sad

Please help.
Quote this message in a reply
Member
Posts: 65
Joined: 2009.01
Post: #2
The problem is that non-object types like floats require a different syntax from regular properties:
http://developer.apple.com/documentation...-CH17-SW10

You also need to do something like this when initializing them to avoid the zeros:
puck.velocity = [[Vector2D alloc] initWithPoint:CGPointMake(5.0, 10.0)];
where the Vector2D class contains floats for velocity.x and velocity.y
Quote this message in a reply
Member
Posts: 227
Joined: 2008.08
Post: #3
It also becomes faster for one to do this:
Code:
@interface GameObject : NSObject
{
@public
    float position_x;
    float position_y;
    float velocity_x;
    float velocity_y;
    NSString *name;
}
@property (nonatomic, retain) NSString *name;

-(void)setup:(NSString *)NameString;

@end

Then access with:
Code:
ball->position_x=5;
ball.name=@"Bob the Blue";
but ball must not be a void* or id type.
Quote this message in a reply
Member
Posts: 283
Joined: 2006.05
Post: #4
I'm not sure what the previous two posts saying. I can't see anything wrong with how you've defined/synthesized the properties.

Do you call the alloc/init methods for the ball anywhere? e.g. in your initWithCoder method before you do anything else do you have something like

Code:
ball = [[gameObject alloc] init];

If not I bet that's the problem.
Quote this message in a reply
Moderator
Posts: 608
Joined: 2002.04
Post: #5
johncmurphy Wrote:The problem is that non-object types like floats require a different syntax from regular properties:
http://developer.apple.com/documentation...-CH17-SW10

You also need to do something like this when initializing them to avoid the zeros:
puck.velocity = [[Vector2D alloc] initWithPoint:CGPointMake(5.0, 10.0)];
where the Vector2D class contains floats for velocity.x and velocity.y
This doesn't seem right at all... and that link doesn't really support what you're saying.

What Oddity is suggesting isn't properties so you wouldn't get key-value coding compliance. It is, however, bad OO practice.

proxus, I don't see anything immediately wrong. Like maximile said, make sure you are actually instantiating an instance with alloc/init.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Objective C question Wayrath 8 7,520 Apr 15, 2010 07:25 AM
Last Post: Wayrath
  Basic Objective-C question elliptic 7 5,065 Aug 1, 2008 12:03 PM
Last Post: SethWillits
  objective-c and c++ question OptimisticMonkey 4 4,652 May 28, 2008 02:51 PM
Last Post: OptimisticMonkey
  Beginner - Objective-C/C++/Engines question LeChuck 8 5,251 Nov 26, 2007 04:26 PM
Last Post: Duane