Archive for the 'cocos2d' Category

Page 4 of 5

cocos2d XCode templates

You can start a cocos2d project within XCode from a template.

To do that you need to follow the instructions from this post:
Project and File Templates
This template also includes Scene and Layer classes.

Another template (from another user) can be found in this post:
XCode template for cocos2d

And last, you can find here another template for cocos2d. This one is for cocos2d v0.5.3
XCode Template

Misc tips for cocos2d & iPhone projects

Do you have problems finding memory leaks ? Do you have problems propagating your touch events ?

Recently the user lhunath found an interesting tip to find memory leaks using an static analyzer for XCode and iPhone.
He opened the issue #90 with the list of memory leaks found, plus the tool that he used: LLVM/CLan Static Analyzer

Also, the user webmaster found this discussion about touch propagation. Basically you can change the priority of the touch events. If your touches are not being propagated as fast as you expect try adding this code to your main loop:

while (CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0, YES) == kCFRunLoopRunHandledSource) {};

Read the full cocos2d discussion here: ccTouchesBegan too heavy?

cocos2d for iPhone v0.6.2 released

Hey, I’ve just released cocos2d for iPhone v0.6.2

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

Changelog:

  • Actions: Accelerate & Speed ratios can be modified at runtime (issue #127)
  • CocosNode: added absolutePosition (issue #9 & issue #122)
  • CocosNode: tag is propagated when adding children (issue #118)
  • CocosNode: change change z-order at runtime (issue #59)
  • Demos: Chipmunk_Accel supports multiple touches (issue #129 and issue #116)
  • Demos: fixed typo in MenuTest (issue #111)
  • Director: Touches work fast again (issue #126)
  • License: removed “GNU LGPL” from sources files (issue #115)
  • License: fixed grammar errors (issue #120)
  • Sprite & Animation: can be created from Texture2D, updated SpriteDemo example (issue #113)
  • TextureMgr: Avois duplicate cache (issue #121)
  • Transitions: FadeTransition supports variable color. Updated TransitionsDemo (issue #129)

Performance issues:

  • You will notice that your games consumes much less RAM thanks to #issue 121.
  • You will notice that the Touch events are propagated a little bit faster, just like in v0.5.x

New features:

  • FadeTransition supports color. Default is black
  • You can reorder a node using [parent reorderChild:child z:newZValue];
  • You can calculate the absolute position of a child using: [child absolutePosition];
  • You can create Sprites and Animations with Textures2D objects: [Sprite spriteWithTexture:tex];
  • You can change the Accelerate ratio and Speed ratio in runtime.

Compatibility:

  • zOrder (from CocosNode) is no longer a read-write property. It is now read-only.

How to use the TileMap editor for cocos2d

Since v0.5 cocos2d for iPhone supports TileMaps. Instead of using the original cocos2d tilemap format (cocos2d for PC) it is using a new format based on the PGU tilemap editor.

This editor lets you edit tiles, tile maps and tile codes.

How do you install the PGU tilemap editor ?

To run the PGU level editor do:

home:~ $ cd src/pgu_level_editorhome:~/src/pgu_level_editor $ python leveledit.py level.tga

The left panel is the map.
The upper-right panel are the tiles.
The bottom-right panel are the codes.

The map is loaded from the file: level.tga
The tiles are loaded from the file: tiles.tga
The codes are loaded from the file: codes.tga

For more info see the leveledit.ini file. eg: you can change the tiles’ size.

Features and limitations:

  • cocos2d v0.6.1 (an earlier) doesn’t support codes, so ignore this feature for the moment.
  • PGU tile editor uses the TGA RGB channels to identify the codes, tiles’ positions and (mmm… what else? I’m not sure). So, you can only have 256 different tiles per TileMap.

IMPORTANT: You need to convert the tiles.tga file to PNG (eg: tiles.png). To convert a TGA file to PNG you can use the GIMP editor.

How to use these files from cocos2d for iPhone:

  • Include the tiles.png and level.tga to your XCode project
  • See the AtlasDemo example: TestAtlas.m. In particular see the Atlas3 demo.

-(id) init{if( ![super init] )return nil;

TileMapAtlas *tilemap = [TileMapAtlas tileMapAtlasWithTileFile:@"tiles.png" mapFile:@"level.tga" tileWidth:16 tileHeight:16];[self add:tilemap];

return self;}

Update: Added missing pyobjc-1.4 package

Compile for Thumb

From the iPhone OS Programming Guide:

“iPhones and iPod touches support two instruction sets, ARM and Thumb. Xcode uses Thumb instructions by default because using Thumb typically reduces code size by about 35 percent relative to ARM.
Applications that have extensive floating point code might perform better if they use ARM instructions rather than Thumb. You can turn off Thumb for your application by turning off the Compile for Thumb build setting.”

So, are you using cocos2d + chipmunk ? Probably you will need to disable “compile for thumb”.
How to:

  1. Open your XCode project
  2. Select ‘Device – iPhone 2.x‘ as Active SDK
  3. For each Target do these:
  4. Go to: Project -> Edit Active Target
  5. Search for ‘thumb’
  6. Disable ‘Compile for Thumb

That’s all. Your game will perform much much much faster.
On the other hand, if you’re not using Chipmunk, you might want to leave ‘compile for thumb’ enabled.

Chipmunk example Thumb disabled Thumb enabled
Example 1 60 FPS 60 FPS
Example 2 30 FPS 12~15 FPS
Example 3 15~20 FPS 8~12 FPS
Example 4 60 FPS 30 FPS
Example 5 12 FPS 4 FPS
Example 6 60 FPS 60 FPS
Example 7 60 FPS 60 FPS

update: Added FPS comparisons

cocos2d for iPhone v0.6.1 released

Hey, I’ve just released cocos2d for iPhone v0.6.1

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

Changelog:

  • Documentation: Generation of doc fixes (issue #105)
  • Primitives: drawLine prototype fixed (issue #103)
  • XCode: all projects have “thumb compilation” turned off (issue #104)

As you can see a the changelog is pretty small. But I decided to release a new release because of the “compile for thumb” thing. An article regarding “compilation for thumb” coming soon.

cocos2d for iPhone v0.6.0 released

Hey, I’ve just released v0.6.0!!

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

ChangeLog:

  • Actions: Sequences of Repeats fixes (issue #80)
  • Actions: Added RepeatForEver. Added example in SpritesDemo example (issue #93)
  • Actions: Repeat can be reversed (issue #92)
  • Actions: Memory leaks detected by static analyzer. (issue #90)
  • CocosNode: added support for Parallax scroller. Created ParallaxDemo example (issue #89)
  • CocosNode: removed deprecated ‘named’ methods (issue #91)
  • CocosNode: lazy allocations with timers, children & actions (issue #94)
  • CocosNode: added removeAndStop, removeAndStopByTag and removeAndStopAll (issue #101)
  • Demos: fixed memory leaks in AnchorDemo, SpritesDemo and RotateWorld (related to issue #90)
  • Director: propagation of Touch events can be stopped (issue #96)
  • Documentation: properties are documented (issue #100)
  • Menu: conforms to CocosNodeSize protocol (issue #99)
  • Menu: support both vertical & horizontal alignment (issue #97)
  • Menu: added MenuItemToggle (issue #83)
  • Particle: Added ParticleSnow (issue #102)
  • Texture2D: creates the textures with clamp to edge and mag filter to linear (issue #95)

Regarding Parallax Scrolling (issue #89) read this:
http://blog.sapusmedia.com/2008/12/cocos2d-parallax-scroller.html

Regarding the new way to propagate events (issue #96) read this:
http://blog.sapusmedia.com/2008/12/cocos2d-propagating-touch-events.html

Regarding RepeatForever and Repeat (issue #93 and #80) read this:
http://blog.sapusmedia.com/2008/12/cocos2d-repeat-vs-repeatforever-vs.html

Regarding the new way to align Menus (issue #97) read this:
http://blog.sapusmedia.com/2008/12/cocos2d-menu-alignment-in-v06.html

I will continue to write articles regarding the new features in the blog.

Important: in cocos2d/tools/ you will find a tool that converts v0.5.3 code to v0.6.0. It is a very simple sed script. It is very incomplete, but will help you with some basic conversions.

cocos2d: propagating touch events

cocos2d v0.6 has a new way to propagate and most importantly, to stop the propagation of touch events.

The old interface was using the UIKit interface:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

The new interface is very similar, but instead of returning void, it now returns a BOOL, and since the return value can’t be used as part of the selector’s identity, the selector was renamed. The prefix ‘cc’ (short of cocos2d) was prepended. Example:

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;- (BOOL)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

If a certain layer handles the event then it should return kEventHandled, else it should return kEventIgnored.

Example taken from Menu.c:

- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint point = [touch locationInView: [touch view]]; int idx;

 MenuItem *item = [self itemInPoint: point idx:&idx];

 if( item ) {     [item selected];     selectedItem = idx;

     // Event handled. It won't be propagated     return kEventHandled; }

 // event not handled. It will be propagated to the next // Layer on the stack return kEventIgnored;}

Which Layer is the first one to receive the event ?
The last Layer to be added. If this layer doesn’t return kEventHandled, then the event will be propagated to the next Layer on the stack.

cocos2d: Parallax scrolling


cocos2d v0.6 introduces Parallax Scrolling. For those who don’t know the meaning of Parallax Scrolling, it is a way to simulate a 3D environment scrolling layers in a 2D environment.

(When I think of Parallax Scroller I think of Shadow of the Beast.)

As you can see from the screenshot, the image is composed of a Parallax Scrolling of 3 layers:

  • A) A ‘powered by cocos2d’ image
  • B) A tilemap
  • C) A background image

When you move the image, the layers start to scroll at different speeds simulating a 3D environment.

How do you code a parallax scrolling in cocos2d ?

// You need a parent node// It can be an empty node, or a node with images. Your choiceCocosNode *voidNode = [CocosNode node];

// Then you add the children, using the 'add' selector.// But with the additional parallaxRatio

// example:// a child that will move at 0.4 ratio horizontally//                       and 0.5 ratio vertically[voidNode add:background z:-1 parallaxRatio:cpv(0.4,0.5)];

// another child with a ratio of 2.2x, 1.0y[voidNode add:tilemap z:1 parallaxRatio:cpv(2.2,1.0)];

// and another child (top one) that is moved at a ratio of 3.0x, 2.5y[voidNode add:cocosImage z:2 parallaxRatio:cpv(3.0,2.5)];

That’s all.

If you want to alter the Parallax Ratio, you can modify the parallaxRatio property. Eg:

background.parallaxRatio = cpv(1.5, 0.5);tilemap.parallaxRatioX = 2;tilemap.parallaxRatioY = 0.9;

Working Example: ParallaxDemo example. See TestParallax.m file.

cocos2d: Menu alignment in v0.6

cocos2d v0.6 introduces a manual way to align the items. You can align them both vertically and horizontally.
Prior to v0.6 the items were vertically aligned automatically. In v0.6 the automatic alignment feature was removed. If you want to align them you need to do this:

menu = [Menu menuWithItems:item1, item2, item3, nil];[menu alignItemsHorizontally];

or:

menu = [Menu menuWithItems:item1, item2, item3, nil];[menu alignItemsVertically];


Also, the previous vertical alignment algorithm had a bug, and wasn’t aligning the items in the center of the screen. If you want to use the old and buggy alignment algorithm you can do this:

menu = [Menu menuWithItems:item1, item2, item3, nil];[menu alignItemsVerticallyOld];


The alignItemsVerticallyOld is deprecated and will be removed in v0.7.

Tip: you can place your MenuItem as well as the Menu manually like any other CocosNode:

menu = [Menu menuWithItems:item1, item2, item3, nil];menu.position = cpv(0,0);item1.position = cpv(50,50);item2.position = cpv(200,200);

Working Example: MenuTest example. See MenuTest.m file.