Super Art Studio

Recent site activity

Home‎ > ‎Blog‎ > ‎

Cocos2d Digest

posted ‎‎Aug 26, 2009 8:28 AM‎‎ by Leo - www.superarts.org -
Cocos2d is the BEST open source 2D iPhone game engine, so far I heard.

  • Menu System

1 CGSize s = [[Director sharedDirector] winSize];
2
3 Label* label = [Label labelWithString:[self title] fontName:@"Arial" fontSize:32];
4 [self addChild: label z:1];
5 [label setPosition: ccp(s.width/2, s.height-50)];
6
7 MenuItemImage *item1 = [MenuItemImage itemFromNormalImage:@"b1.png" selectedImage:@"b2.png" target:self selector:@selector(backCallback:)];
8 MenuItemImage *item2 = [MenuItemImage itemFromNormalImage:@"r1.png" selectedImage:@"r2.png" target:self selector:@selector(restartCallback:)];
9 MenuItemImage *item3 = [MenuItemImage itemFromNormalImage:@"f1.png" selectedImage:@"f2.png" target:self selector:@selector(nextCallback:)];
10
11 Menu *menu = [Menu menuWithItems:item1, item2, item3, nil];
12
13 menu.position = CGPointZero;
14 item1.position = ccp( s.width/2 - 100,30);
15 item2.position = ccp( s.width/2, 30);
16 item3.position = ccp( s.width/2 + 100,30);
17 [self addChild: menu z:1];

  • Sprite

1 Sprite *clouds;
2
3 clouds = [Sprite spriteWithTexture:tex];
4 [clouds setScale:1.3f];
5
6 [clouds setPosition:cpp(0, 0)];

  • Coordinate Converting

1 - (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
2 {
3 UITouch *touch = [touches anyObject];
4 CGPoint touchLocation = [touch locationInView: [touch view]];
5
6 touchLocation = [[Director sharedDirector] convertCoordinate: touchLocation];
7 CGPoint location = ccp(touchLocation.x, touchLocation.y);
8
9 ...
10 }

  • Label & Actions

1 BitmapFontAtlas *label = [BitmapFontAtlas bitmapFontAtlasWithString:@"Bitmap Font Atlas" fntFile:@"bitmapFontTest.fnt"];
2 [self addChild:label];
3
4 CGSize s = [[Director sharedDirector] winSize];
5
6 label.position = ccp(s.width/2, s.height/2);
7 label.anchorPoint = ccp(0.5f, 0.5f);
8
9 AtlasSprite *BChar = (AtlasSprite*) [label getChildByTag:0];
10
11 id scale = [ScaleBy actionWithDuration:2 scale:1.5f];
12 id scale_back = [scale reverse];
13 id scale_seq = [Sequence actions:scale, scale_back,nil];
14 id scale_4ever = [RepeatForever actionWithAction:scale_seq];
15
16 [BChar runAction:scale_4ever];

  • Sprite & Animation (No Loop)

1 AtlasSpriteManager *mgr = [AtlasSpriteManager spriteManagerWithFile:@"grossini_dance_atlas.png" capacity:50];
2 [self addChild:mgr z:0 tag:kTagSpriteManager];
3
4 AtlasSprite *sprite = [AtlasSprite spriteWithRect:CGRectMake(0, 0, 85, 121) spriteManager: mgr];
5
6 AtlasAnimation *animation = [AtlasAnimation animationWithName:@"dance" delay:0.2f];
7 for(int i=0;i<14;i++) {
8 int x= i % 5;
9 int y= i / 5;
10 [animation addFrameWithRect: CGRectMake(x*85, y*121, 85, 121) ];
11
12 }
13
14 [mgr addChild:sprite];
15
16 CGSize s = [[Director sharedDirector] winSize];
17 sprite.position = ccp( s.width /2, s.height/2);
18
19 id action = [Animate actionWithAnimation: animation];
20
21 sprite.scale = 0.5f;
22
23 [sprite runAction:action];

457         [mgr runAction:[OrbitCamera actionWithDuration:10 radius: 1 deltaRadius:0 angleZ:0 deltaAngleZ:360 angleX:0 deltaAngleX:0]];