にしあぷ

札幌在住のエンジニア。コンサドーレ、甘いもの、ドット絵が好き。

Cocos2d for Iphone 0.99 Beginner's Guide 正誤表

Cocos2d for Iphone 0.99 Beginner's Guide

Cocos2d for Iphone 0.99 Beginner's Guide

Cocos2d for iPhone 0.99 Beginner's Guide | PACKT Books
Cocos2D for Beginners Errata - Pastebin.com

上記の正誤表をページ順にまとめ。コードの一部がはてな記法で変換されている(行頭のハイフンとか)ので注意。自分が気付いた誤りもメモ。

Chapter 1

Errata type: Code | Page numbers: 14

Time for action-installing the templates
The step 2. command should read:

cd desktop/Cocos2d 
./install-templates.sh

Page 22 *

Example HelloWorldScene class differs, if using the latest release, 0.99.5

Errata type: Spelling Error | Page numbers: 26

-(id) removeAllChildernWithCleanup:(BOOL)cleanup

Should read:

-(id) removeAllChildrenWithCleanup:(BOOL)cleanup

Page 28 *

The book uses the schedule:@selector method of scheduling an update to the node. However, the cocos wiki says to use scheduleUpdate over scheduleSelector for performance reasons : http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:draw_update#scheduleupdate

Chapter 2

Chapter 2 General

From looking at the cocos2d source code included with the ColouredStones Final project, it is based on the 0.99.5 template. However, the ColouredStonesAppDelegate.m file has deprecated functions included in it, suggesting that the code is based on an earlier template. This makes it very confusing for a beginner. More confusing to a beginner is the additional GameConfig.h and RootViewController files included with the 0.99.5 template.

Page 42 *

Example code provided in point 2 at the top of the page does not match that explained in the 'What just happened?' section at the bottom of the page. One uses absolute positioning, whereas the other uses a dynamic positioning, based on the size of the window.
背景の表示位置を、ウインドウサイズをもとに指定。
GameScene.h > -(id)init

        CGSize wins = [[CCDirector sharedDirector] winSize];
        CCSprite *background = [CCSprite spriteWithFile:@"background.png"];
        [self addChild:background z:0];
        //[background setPosition:ccp(240, 160)];
        [background setPosition:ccp(wins.width / 2, wins.height / 2)];

Page 47 *

The code discussed in 'What just happened?' refers to constant STONE_TYPES, which has not been defined anywhere in the previous code. On page 46, the number 4 is used directly with the arc4random function.
ストーンの色(STONE_TYPES)を定義。
Stone.m > -(void)placeInGrid:(CGPoint)place pt:(int)pt pl:(int)pl

    //int sType = arc4random() % 4;
    int sType = arc4random() % STONE_TYPES;

GameScene.h

#define STONE_TYPES 4

Errata type: Code | Page numbers: 48

code says

[self addChild:mySprite z:1]; 

but explanation says: "...stone object with a z-order of two"
so code should really be

[self addChild:mySprite z:2];

z-orderの説明が誤り。(コードの修正は不要)

Page 49 *

The line in the GameScene.h listing in section 1 should read :

+(CCScene *) scene; 

not :

+(CCScene) scene;

The example code returns a compiler error.
アスタリスク(*)を忘れるとエラーに。

Page 51 *

Executing the entered code crashes the application, due to the program requiring four sprite graphics for the stones : sBlue.png, sRed.png, sYellow.png and sGreen.png. These are not present in the support files for Chapter 2 when downloaded from the Packt site, only a combined spritesheet image. Also, none of the stones in the example share the colours listed, they are different.
ストーン個別の画像がない(スプライトシートのみ)。32x32ピクセルの画像はこちら。
f:id:tmuramoto:20130101141421p:plainf:id:tmuramoto:20130101141430p:plainf:id:tmuramoto:20130101141436p:plainf:id:tmuramoto:20130101141439p:plain

In the 'What just happened?' section, the location of the gridBackground is specified as :

[gridBackground setPosition:ccp(305,160)];

as opposed to the original listing on page 49, which reads :

[gridBackground setPosition:ccp(305,154)];

The original positioning leaves the grid misaligned with the stones.
グリッド画像の位置が違う。ccp(305, 160)が正しい。

Errata type: Code (Typo) | Page number: 56 | Errata date: 24 May, 12

The line

return CGRectContainsPoint( self.rect , ....... )

Should be

return CGRectContainsPoint( [self rect] , .......)

Page 56 *

The code for the rect function has to be placed before the containsTouchLocation function, otherwise, it will not be able to reference it.
Likewise, the code for the containsTouchLocation function has to be placed before the ccTouchBegan function, otherwise, it too will have referential issues

P.56

GameScene.m > -(id) init

-(id) init
{
	if ((self = [super init]))
	{
        self.allowTouch = YES; // この行を追加

Page 57 *

The line that checks the state of theGame.allowTouch fails, as the property hasn't been synthesized as part of the implementation.

Errata type: Code | Page numbers: 60

[grid[i][j-1].mySprite setPosition:ccp(42*i + GRID_OFFSET.x,42*(j-1) GRID_OFFSET.y)];

should be

[grid[i][j-1].mySprite setPosition:ccp(42*i + GRID_OFFSET.x,42*(j-1) + GRID_OFFSET.y)]; 

GRID_OFFSET.y の前の + が抜けている。

and don't forget to mention to add

-(CGRect)rect;

to Stone.h, so it builds.

Page 66 *

There is additional code for incrementing the game score in the 'What just happened?' code that is not included in the checkGroups: listing on page 64.
64ページにないコードが、66ページに書かれている。(スコア計算のコード)

                        score += 100 * [n count];
                        NSLog(@"Score: %d", score);

Page 71 *

The following line :

CGRect color = [stone setStoneColor:stoneT];

gives an error at compilation, as it expects a CGRect to be returned from the setStoneColor: method of the Stone class. However, as at this point, all of the stones are using individual sprite files, the method returns an NSString with the filename of the sprite that needs to be loaded to replace the stone. This code will only work with the spritesheet approach to loading sprites, which is not discussed or implemented until page 79.
The correct code can be found at the top of page 77.

setStoneColor: メソッドの戻り値は CGRect ではなくて NSString。(後のページで、スプライトシートを使った処理に書き換える)
GameScene.m > -(void)moveStoneDown

            //CGRect color = [stone setStoneColor:stoneT];
            //[stone.mySprite setTextureRect:color];
            NSString *color = [stone setStoneColor:stoneT];
            stone.mySprite = [CCSprite spriteWithFile:[NSString stringWithFormat:@"s%@.png", color]];

Page 74 *

remainingTime has not been added to the interface definition for the GameLayer, so can't be set to MAX_TIME.
最大時間(MAX_TIME)を定義。
GameScene.h

#define MAX_TIME 230

bar has also not been added to the same interface definition, so is also not able to be set.
The bar logic references bar.png, which the reader isn't instructed to add to their resources folder.
タイムバーの画像(bar.png)を resources フォルダに追加。

Page 77

sSmile.png is not included in the downloadable source code.
ニコニコマークの画像(sSmile.png)がサンプルコードにない。

Page 82 *

When changing the return type of the setStoneColor method, this also needs to be set in the header file.
setStoneColor メソッドの返り値(型)が変わったので、ヘッダーファイルも変更する。
Stone.h

//-(NSString *)setStoneColor:(int)stoneT;
-(CGRect)setStoneColor:(int)stoneT;

Errata type: Code (Typo) | Page number: 82 | Errata date: 20 May 11

CCSpriteBatchNode * s = (CCSpriteBatchNode *)[theGame getChildByTag:K_SSheet]; 

Should be

CCSpriteBatchNode * s = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];

タグ名は kSSheet が正しい。

Errata type: Code | Page numbers: 82
code says:
CCSpriteBatchNode * s = (CCSpriteBatchNode *)[theGame getChildByTag:K_SSheet];
should be
CCSpriteBatchNode * s = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];

P.90

cocos2d 2.x では glColor4ub(), glPointSize() は非推奨。
Stone.m > -(void)draw

		//glColor4ub(255, 0, 0, 100);
		//glPointSize(30);
		ccDrawColor4F(255, 0, 0, 100);
    	        ccPointSize(30);

参考:Unable to get OpenGL ES 'draw' functions to work (+ C99 error) with Cocos2d - Stack Overflow

Errata type: Code | Page numbers: 94

code says....

if(nilCount >0 && !d.disappearing) { 
[d.mySprite runAction:[CCMoveTo actionWithDuration: (0.5 * nilCount)/3 position:ccp(42*i + GRID_OFFSET.x,42* (j-nilCount) + GRID_OFFSET.y)]]; 
} 

should be:

if(nilCount >0 && !d.disappearing) { 
[stone.mySprite runAction:[CCMoveTo actionWithDuration: (0.5 * nilCount)/3 position:ccp(42*i + GRID_OFFSET.x,42* (j-nilCount) + GRID_OFFSET.y)]]; 
}

本では [d.mySprite ...]、サンプルコードは [stone.mySprite ...] になっている。どちらかに合わせればOK。

Chapter 3

Page 103 *

The code shown to load the grid background by using the spritesheet and the getChildByTag with the constant K_GBACK haven't been run through in previous chapters.
This is present in the example project for chapter 3, however.

GameScene.h

#define kGBACK 2

GameScene.m > -(void)checkGroups:(bool)firstTime

			CCSprite * b  = (CCSprite *)[self getChildByTag:kGBACK];

Page 104 *

With 0.99.5, CCRepeat no longer accepts CCAction as a passed type, it must be CCFiniteTimeAction, so act must be defined as such.

Page 111 *

Step 3 in the Time for Action section refers to a variable called disallowTouch that needs to be set to YES to stop the player interacting with the stones. The actual instance variable built into the code so far is called allowTouch, so this needs to be set to NO.

Page 118

The reader is prompted to include stonesSheet.png and stonesSheet.plist from the source files, but these are not included in the files.
In section 2, the code added to the init method refers to a file called 'colouredSheet.plist', which also accesses a file called 'colouredSheet.png', both of which are included in the source files.

Chapter 5

Errata type: Code | Page numbers: 153

The line :

isTouchEnabled = YES;

should read :

self.isTouchEnabled = YES;

otherwise the CCLayer property isn't recognised by the complier.

Page 153

The line :

isTouchEnabled = YES;

should read :

self.isTouchEnabled = YES;

otherwise the CCLayer property isn't recognised by the complier.

The line that sets the image position has the X and Y co-ordinates transposed :

[splashImage setPosition:ccp(240,160)];

should read :

[splashImage setPosition:ccp(160,240)];

Page 158

The CCFadeTransition has been replaced with CCTransitionFade in 0.99.5

Errata type: Code | Page numbers: 173

The fire method is missing the line:

[self.mySprite setVisible:YES]; 

This enables the bullet to be seen when fired.

Errata type: Code | Page numbers: 173

3. Add a new property to the GameLayer, NSMutableArray * enemies. This array will hold the bullets.
Should be NSMutableArray * bullets

Errata type: Code | Page numbers: 189

[theGame loseLive]; 

should be

[theGame loseLife];

Errata type: Code | Page numbers: 203

Missing the line in the HudLayer class init method to enable touches.

self.isTouchEnabled=YES;

Cocos2d for Iphone 0.99 Beginner's Guide

Cocos2d for Iphone 0.99 Beginner's Guide