Objective-C property question.
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:
GameObject.m:
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
And then I try to do the following in EAGL.m:
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
Please help.
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;
@endGameObject.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];
}
@endThe 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

Please help.
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
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
It also becomes faster for one to do this:
Then access with:
but ball must not be a void* or id type.
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;
@endThen access with:
Code:
ball->position_x=5;
ball.name=@"Bob the Blue";
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
If not I bet that's the problem.
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.
johncmurphy Wrote:The problem is that non-object types like floats require a different syntax from regular properties:This doesn't seem right at all... and that link doesn't really support what you're saying.
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
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.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Objective C question | Wayrath | 8 | 6,644 |
Apr 15, 2010 07:25 AM Last Post: Wayrath |
|
| Basic Objective-C question | elliptic | 7 | 4,313 |
Aug 1, 2008 12:03 PM Last Post: SethWillits |
|
| objective-c and c++ question | OptimisticMonkey | 4 | 4,169 |
May 28, 2008 02:51 PM Last Post: OptimisticMonkey |
|
| Beginner - Objective-C/C++/Engines question | LeChuck | 8 | 4,604 |
Nov 26, 2007 04:26 PM Last Post: Duane |
|

