Cocos2d: Get and set properties between classes (composition vs inheritance)

I’m taking a break from posting about the hours from Sams Learn iPhone in 24 hours, since I’m jumping back and forth between books. Currently I’m hot into Cocos2d and game dev. I’m reading Steffen Itterheims book on Cocos2d which is really good, but since I’m a n00b it gets a little to advanced for me.

So, I’ve ordered Programming in Objective-C 2.0 by Stephen G. Kochan covering the basics on ObjC, but while waiting I’m doing trial-and-error programming. Here’s some stuff I found out.

I’m trying to subclass NSObject to create an Enemy class to spawn enemies that are supposed to live it’s own life. The code is based on what I got out of Itterheims book (p107-109). I got some questions which I found out after testing, and also from the cocos2d forum.

I made a new class called Enemy, here’s the header and implementation files, and also a part of my GameScene where I “spawn” it. This will spawn an enemy that will move randomly.


@interface Enemy : NSObject {
int numUpdates;
CCSprite* enemySprite;

+(id) enemyWithParentNode:(CCNode*)parentNode;
-(id) initWithParentNode:(CCNode*)parentNode;



#import "Enemy.h"

@implementation Enemy

+(id) enemyWithParentNode:(CCNode*)parentNode {
return [[[self alloc] initWithParentNode:parentNode] autorelease];

-(id) initWithParentNode:(CCNode *)parentNode {
if ((self = [super init])) {
CGSize screenSize = [[CCDirector sharedDirector] winSize];

enemySprite = [CCSprite spriteWithFile:@"ship.png"];
enemySprite.position = CGPointMake(screenSize.width/2, screenSize.height/2);

[parentNode addChild:enemySprite];

[[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:0 paused:NO];

return self;

-(void) dealloc {
[[CCScheduler sharedScheduler] unscheduleUpdateForTarget:self];
[super dealloc];

-(void) update:(ccTime)delta {

if (numUpdates % 50 == 0) {
numUpdates = 0;
[enemySprite stopAllActions];

CGPoint moveTo = CGPointMake(CCRANDOM_0_1()*200-100, CCRANDOM_0_1()*100-50);

CCMoveBy* move = [CCMoveBy actionWithDuration:1 position:moveTo];
[enemySprite runAction:move];



In the GameScene i do this to create an enemy:

Enemy* enemy1 = [Enemy enemyWithParentNode:self];

So here are my questions, with answers:

1. How can I set the scale/position of the sprite in the update-function?

[enemySprite setScale:1.5f];
[enemySprite setPosition:CGPointMake(100, 200)];

2. How can I get the position of the sprite within the enemy update method?

[enemySprite position].x

3. How can I do 1+2 from GameScene that spawned the class?

In Enemy.m: (and the first line also in the .h)

-(void) setScale:(float_t)scale {
[enemySprite setScale:scale];

In the GameScene:

[enemy1 setScale:2.5f];

Do I have to recreate all properties like scale, position, etc into methods like this, or is there a better way?

4. How can I add custom variables to the Enemy class, that can be set/get from within or outside the class?

Don’t know yet… will update as soon as I know🙂

5. Do I need to create an array in the gamescene to keep track of all enemies, or could I just do [Enemy enemyWithParentNode:self]; and still work with object collisions etc?

Don’t know… But I think the answer is yes I need an array to do stuff with it…

I got some great tips from this old but good post on Cocos2d. It’s based on an old version, so some things are called differently:

If you have comments on how I solved this, please comment. I also like to make my code as easy to read as possiblem, so nothing too fancy…🙂

This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

3 Responses to Cocos2d: Get and set properties between classes (composition vs inheritance)

  1. Peter Small says:

    Hoping you will continue your commentary as you progress through the SAMS 24 hour book. I have also bought the book recently and have started to work through it.
    However, I have only a scripting background and am finding the conceptual jump to cocoa quite perplexing – even though I’ve written a couple of scripting books which concentrated on OOPs.

    I too have bought Kochan’s book on programming in Objective-C 2.0 (and have ordered the new book -3rd Edition – due out next month). I have also bought an O’Reilly book “iPhone Programming” by Alasdair Allan and a Sams book “Cocoa Touch Programming” by Sengan Baring-Gould. I’m hoping that between them I can start to get my head round the structural concepts. I know from experience that this takes quite a bit of time and effort.

  2. Niclas says:

    Hi Peter, thanks for your kind words!🙂 Like I wrote in this post, I’m currently taking a little break of this book, trying to grasp the concepts around ObjC, and yes, it’s a bit to learn for a PHP-programmer. Interesting that you find it hard even though you know OOP.

    I wanted to order Kochans 3rd edition too, but I thought it was due out May, so that’s why I ordered this one right away. I’d be happy to know if there are any major differences in it. I’ll take a look at the other books you mentioned. Right now I have 4 books that I’m working with so that’s a bit of a job🙂

    Let me know if you find any interesting links for us scripters->objective-C:ers. I found some really good starter guides on, check that out. They talk a bit about the difference between scripting languages and ObjC/Cocoa.

  3. Hi,

    Objective-C properties are a quick way to create setters/getters automatically for you, which could be accessed from within your class, or out of it. Just learned about them..🙂 Here is the post that I found most useful in wrapping my mind around these:

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s