Question

I have two major classes in objective C using cocos2D, DebugZoneLayer and HeroClass. Using Cocos2D may not be part of the issue.

HeroClass contains a CGPoint and a property. I have an instance of HeroClass in DebugZoneLayer initialized like hero = [[HeroClass alloc] init];

My HeroClass.h shortened to show you how I create a CGPoint vel.

@interface HeroClass : CCLayer {
    @public CGPoint _vel;
}

@property(assign) CGPoint vel;

In HeroClass.m I synthesize my property like @synthesize vel = _vel;

In DebugZoneLayer.m, I can reference my hero.vel x or y just fine, but anything that assigns a value to hero.vel x or y returns the error: Lvalue required as left operand of assignment

Was it helpful?

Solution

That's right — you can't do that. A property is just a method call, and methods in Objective-C always return by value, meaning the CGPoint that gets returned is just a temporary CGPoint with the same value as the one in your object. Setting the components of this temporary value isn't allowed. You'll need to either create special setters on your class for the point's X and Y values or set the whole point at a time.

OTHER TIPS

Restating Chuck's entirely correct answer in a different way..

Your problem is that CGPoints are not Objective-c Objects, they are C Structs. Your property *_vel* is not an instance of an Object, like an NSArray, NSArray or DebugZoneLayer.

As a simple and lazy example, using an int instead of a struct and a bit of psuedocode..

@interface HeroClass : CCLayer {
    int _numberOfLives;
}
@end

@implementation HeroClass
- (id)init {
    [super init];
    _numberOfLives = 3;
}

- (int)livesRemaining {
    return _numberOfLives;
}
@end

you couldn't set the value of _numberOfLives like this..

foo = [[HeroClass alloc] init];
bar = [foo livesRemaining];
bar = 2;

Changing the value of bar won't change the value of foo's _numberOfLives instance variable because when you called -livesRemaining, bar was set to a copy of the current value of _numberOfLives.

In short, you need to learn you some C.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top