甘いものが好きです

iOS App開発時に感じた疑問や課題、その他の雑感などを書いていきます。

CCMenuItemImageのインスタンス作成時に指定するメソッドのシグネチャに注意

Problems with the new cocos2d version 0.99.1

上記記事はcocos2d for iPhoneのVer. 0.99.1での話だが、今回はVer. 0.99.5でこれと同じ現象に遭遇した。CCMenuItemImageのインスタンス作成時に例外が発生したのだが、原因はわかってしまえばたいしたことはない。itemFromNormalImage:selectedImage:target:selector:の第4引数で指定するメソッドのシグネチャが正しくないためだった。

itemFromNormalImage:selectedImage:target:selector:の第4引数で指定するメソッドは、呼び元の参照を渡すためにid型の引数(ここでは「sender」と呼ぶことにする)を1つとらなければならない。

CCMenuItemImage Class Referenceにもシグネチャが限定されることについては記載されていなかった。cocos2d for iPhoneのドキュメントでは、以下のようにsenderの利用方法に関して言及している。


Note: There is a difference between @selector(doSomething) and @selector(doSomething:) (note the extra colon). With the colon, the menuItem will be passed to the method. This is useful if you want to pass additional data to the method being called. For example, you could use the menu item 'tag' to decide how to proceed. You may even choose to subclass the MenuItemType to store additional information which can be accessed in the doSomething: method.


prog_guide:lesson_3._menus_and_scenes – cocos2d for iPhone