Monthly Archive for March, 2009

cocos2d v0.7.1 released

cocos2d_67Hey,
cocos2d v0.7.1 is available for download.

Download:
http://cocos2d-iphone.googlecode.com/files/cocos2d-iphone-0.7.1.tar.gz

Highlights:

  • Sprite Sheets (For further info please read this post: spritesheet-in-v071 )
  • Local to World coordinates and vice-versa
  • Sound Support (warning: experimental feature)


Compatibility
:
cocos2d v0.7.1 is 90% compatible with v0.7.0. You need to add new files and
you might need to add some linker parameter. Also some selectors were deprecated.
Please, read CAREFULLY the following instructions:

Full Changelog:

  • Actions: added tags to actions (issue #222)
  • Actions: Spawns can be Speeded (issue #257)
  • Actions: Speed can be altered in runtime (part of issue #236)
  • AtlasSprite: Sprite sheet implementation (issue #238)
  • Chipmunk: cpVect is defined as a CGPoint (issue #260)
  • CocosNode: Camera is lazy alloced (issue #94)
  • CocosNode: addChild, removeChild:cleanup, getChildByTag family functions (issue #253)
  • CocosNode: added better comments, cleanup code (issue #219)
  • CocosNode: added RGB protocol: Atlas,Texture & ColorLayer conforms it (issue #234)
  • CocosNode: fixed memory leak when removeAndStop a node with children with actions (issue #254)
  • CocosNode: added transform local to world coordinates that support rotation,scale & position (issue #207)
  • CocosNode: removed scale ivar (issue #231)
  • CocosNode: improved handling of nil parameters (issue #262)
  • CocosLive: filter by device id (issue #223)
  • CocosLive: category is UTF8′ized (issue #227)
  • CocosLive: using cc_playername instead of usr_playername (issue #242)
  • CocosLive: supports “update score” (issue #250)
  • Demos: added performance test (issue #243)
  • Director: FastDirector doesn’t leak autoreleased objects (issue #221)
  • Director: prevents calling startAnimation twice in a row (issue #215)
  • Menu: can be aligned in columns / rows. Updated menu example (issue #206)
  • Menu: MenuItem supports LabelAtlas (issue #235)
  • Menu: MenuItemFont fixed memory leak (issue #232)
  • Menu: MenuItem improved hit testing (issue #214)
  • Scheduler: if signature is not correct Assert (issue #218)
  • Sprite: correct transform anchor point (issue #216)
  • TextureAtlas: support for texture2D (issue #161)
  • Tools: Added perf-test results (issue #243)

As always, big thanks to the community for adding new features, fixing bugs, submitting patches, opening issues, testing the release candidate, etc!!

cpVect vs. CGPoint (Chipmunk vs. CoreGraphics)

Chipmunk has a type called cpVect that is used to define a vector.
The above mentioned structure is defined as:

1
2
3
typedef struct cpVect{
    cpFloat x,y;
} cpVect;

On the other hand, CoreGraphics has a similar type called CGPoint that is defined as:

1
2
3
4
5
struct CGPoint {
    CGFloat x;
    CGFloat y;
};
typedef struct CGPoint CGPoint;

These 2 types has the same attributes (they are identical) but they aren’t interchangeable because they are different types.
You can’t add a CGPoint with a cpVect, neither you can’t test whether or not a cpVect is inside a CGRect, etc.

Since cocos2d v0.7.1 you will be able to mix them and use them interchangeably. The solution was a simple hack:

1
2
3
4
5
6
7
// file cpVect.h
 
#import <CoreGraphics/CGGeometry.h>
#define cpVect CGPoint
//typedef struct cpVect{
//     cpFloat x,y;
//} cpVect;

Documentation in cocos2d


Perhaps the most important feature that is missing in cocos2d for iPhone is a Programing Guide. In the meantime you can learn cocos2d for iPhone by:

You can also read news and some recipes from these blogs:

And if you have a question and you don’t know who to ask, you can ask it in the cocos2d forum:

Ah… and last but not least, if you are also new to iPhone and objective-c, check this page:

SpriteSheet in v0.7.1

cocos2d v0.7.1 supports SpriteSheets. An SpriteSheet basically is an image that contains subimages, and these subimages can be used as sprites. In cocos2d v0.7.1 these new sprites are called AtlasSprite.

These are the benefits of using AtlasSprite instead of the normal Sprite:

  • Reduces texture memory since all the sprites are in one big image.
  • Reduces render time since there are less OpenGL calls. The new order is O(1) while the order of using normal Sprites is O(n). If you have 400 AtlasSprites, now you have to do 6 OpenGL calls. With 400 Sprites you have to do 400 *6 OpenGL calls.

How do you use it?:

// Create the AtlasSpriteManager
// You can use a .PNG file or a PVRTC image.
// capacity is an optional parameter. It is just a hint that means the quantity of total sprites.
// You can add more (or less) sprites than the quantity.
// grossini_dance_atlas.png is an image than contains the subsprites.
AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:50];
 
// You create the sprite
AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(x,y,85,121) spriteManager:mgr];
 
// you add the sprite to the parent
// the parent must be the AtlasSpriteManager
// z must be 0.
// You can add tags if you want.
[mgr addChild:sprite z:0 tag:kTagSprite1];
 
// you can transform the sprite
sprite.position = cpv( 200,300 );
sprite.rotation = 90;
sprite.scale = 2.0f;
 
// you can run actions in this new sprite
[sprite runAction: [RotateBy actionWithDuration:2 angle:360]];

The AtlasSpriteTest.m file contains a working example of AtlasSprite.
The PerformanceSpriteTest example compares Sprite vs AtlasSprite performance (more on this later).

[edit: updated links]

deprecated API in v0.7.1


cocos2d-iphone started as a port of cocos2d-python and during the port most of the function names remained exactly the same. For example, in cocos2d-python to add child to a parent you should do this:

parent.add( child )

and in cocos2d-iphone you should do this:

[parent add:child];

But the name add does not follow the objective-c convention. So, in v0.7.1 these functions will be deprecated. This means that these functions will be still present during the v0.7 series. A warning will be displayed at compile time and at runtime (in debug mode only) but they will still work as expected.
In v0.8 these functions will be removed.

These are the new functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
//Add:
//
[self add:node];        // OLD
[self addChild:node];   // NEW
 
[self add:node z:0];          // OLD
[self addChild:node z:0];     // NEW
 
[self add:node z:0 tag:t];          // OLD
[self addChild:node z:0 tag:t];     // NEW
 
[self add:node z:0 tag:t parallaxRatio];        // OLD
[self addChild:node z:0 tag:t parallaxRatio];   // NEW
 
//
//Get:
//
[self getByTag:tag];        // OLD
[self getChildByTag:tag];   // NEW
 
//
//Remove:
//
[self remove:node];                    // OLD
[self removeChild:node cleanup:NO];    // NEW
 
[self removeAndStop:node];                  // OLD
[self removeChild:node cleanup:YES];        // NEW
 
[self removeByTag:tag];                    // OLD
[self removeChildByTag:tag cleanup:NO];    // NEW
 
[self removeAndStopByTag:tag];              // OLD
[self removeChildByTag:tag cleanup:YES];    // NEW
 
[self removeAll];                           // OLD
[self removeAllChildrenWithCleanup: NO];    // NEW
 
[self removeAndStopAll];                    // OLD
[self removeAllChildrenWithCleanup: YES];    // NEW
 
//
// Actions:
//
[self do: action];          // OLD
[self runAction: action];   // NEW

It is noteworthy the new cleanup parameter. If you want to stop all running actions and/or scheduled functions in a node, use the cleanup parameter with YES.