List of basic questions

Forums Programming cocos2d support (graphics engine) List of basic questions

This topic contains 6 replies, has 5 voices, and was last updated by  abitofcode 1 year, 5 months ago.

Viewing 7 posts - 1 through 7 (of 7 total)
Author Posts
Author Posts
November 8, 2012 at 10:29 pm #245220

ccwambam
Participant
@ccwambam

I am at the end of the learning curve for cocos2d (objective c already learned) and I have a few basic questions. Preferably I would like somebody who is really experienced in ojbective c to answer these questions. They aren’t very complicated.

1. Synthesized Accessor Methods. What is their purpose in cocos2d and why do you declare it twice in the header. Once at the top of the interface and once again as a properties. The (nonatomic,retain,etc.) values aren’t really explained well either.

2. (General question/statement) it seems like people are making certain bits of easier to code but making it more complicated. I guess this is the point of object oriented programming but why make things “easier”

3. If you create a mutable array of ccsprites as objects and you want to add a certain object (ccsprite) to the layer how would you do that? Im guessing [self addchild:[arrayName ObjectAtIndex:i]] and this would add the object at that place in the array to the layer?

November 8, 2012 at 11:26 pm #391826

ayt
Participant
@ayt

1. Not sure if I understood it right but I think it is used to access a variable or an object from another class since using global variables doesn’t fit to the oop.

2. Sometimes it can be hard to get into some libs but in the end it’s mostly easier and faster to learn how to use than to create your own code for everything.

3. You are right but in the way you coded it the compiler wouldn’t know how to treat the object since it could be any type of object. I don’t know if it’s correct but it works like this:

[self addchild:(CCSprite*)[arrayName ObjectAtIndex:i]];

or this:

CCSprite *spr=(CCSprite*)[arrayName ObjectAtIndex:i];
[self addchild:spr]

November 9, 2012 at 2:34 am #391827

ccwambam
Participant
@ccwambam

@ayt thank you for your response

November 9, 2012 at 1:52 pm #391828

razvan_b
Participant
@razvan_b

i think that the answer to q1 is different!

a class has a data member and the methods to access it.

for example a class point has to floats :x, and y. These two are the members. a getter would be [my_point x], so i think what it seams to you that gets declared twice is a confusion between a data member and a getter/setter for that data member.

like:

object->x


and


[object GetX]

member(public)


GetterMethod for x;

November 10, 2012 at 1:04 am #391829

ccwambam
Participant
@ccwambam

@razvan_b this is more of what im talking about:

#import <Foundation/Foundation.h>

#import “cocos2d.h”

@interface GameObject : CCSprite {

BOOL didHappen;

}

@property (readwrite) BOOL didHappen;

When must be be declared as a property if it has already been declared in the interface?

November 10, 2012 at 11:55 pm #391830

michaelm
Participant
@michaelm

The bool didHappen is instance data, private by default only accessible in the class it’s declared. If you want to access that data outside of your class like myObject.didHappen you have to use a property. The @property says what kind of property it is and the @synthesize tells the compiler to go ahead and write the getters and setters for you when your compiling. It just saves you from writing a ton of boilerplate setters and getters.

So if you only want to access something inside the same class you don’t need a property, outside you need it or write your own accessors.

The arguments like nonatomic has to do with threading read http://stackoverflow.com/questions/588866/atomic-vs-nonatomic-properties , retain just automatically retains the item when set, readonly just creates a getter not a setter.

November 11, 2012 at 12:49 am #391831

abitofcode
Moderator
@abitofcode

1. synthesize = make something

In this case it creates the accessors and mutators (getters and setters) for the property you are creating. The values in the brackets give the methods that are created an indication of how they should be constructed. If the value readonly is present for example then only a getter is created, for a list of the other values check out apples documentation.

@property (nonatomic, strong) MyObject *thingy;

Would create methods;

-(MyObject*)thingy {
// mysterious stuff only apple knows about goes here
return something;
}

-(void*)setThingyl:(MyObject*) {
// mysterious stuff only apple knows about goes here
// before ARC, 'retain' in the brackets let this method know how to handle
// the memory management. (also see 'assign')
}

Using self.thingy would access the getter via the method above, you could however use synthesize to reference the variable directly rather than through the getter/setter

i.e

@synthesize thingy = _thingy;

Where _thingy was the variable reference declared between the brackets in your @interface.

You no longer need synthesize with the latest version of XCode, or have the variable declared between curly brackets in the interface. All you need is the @property.

For the example above, Xcode would allow you to access the self.thingy variable directly as _thingy.

(For NSStrings you typically want to use copy in case someone changes the string you have a reference to at a later date)

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.