私が読んだチュートリアルのほとんどは、HelloWorldクラスのCocos2Dの例のみを説明していますが、簡単なゲームの作成を開始したので、さまざまなクラスにイベントを送信する方法と、イベントが発生するたびに応答する方法を知る必要があります。 。
スプライトのさまざまなCCLayerをロードするCCLayerクラスであるGameSceneLayerがあります。
@implementation GameSceneLayer
+ (CCScene *)scene {
CCScene *scene = [CCScene node]; // Create a container scene instance
GameSceneLayer *gameLayer = [GameSceneLayer node]; // Create an instance of the current layer class
[scene addChild:gameLayer]; // Add new layer to container scene
return scene; // Return ready-made scene and layer in one
}
-(id)init
{
self = [super init];
if (self != nil)
{
Background *background = [Background node];
[self addChild:background z:0];
Player *player = [player node];
[self addChild:player z:1];
MainMenu *mainMenu = [MainMenu node];
[self addChild:mainMenu z:2];
}
return self;
}
@end
ただし、MainMenu CCLayerのSTARTスプライトに触れると、PlayerCCLayerからPLAYERスプライトにスポライトするようにしたいと思います。
次のようなGlobalVariables.hが必要だと思います。
#define gameStart @"0"
したがって、STARTスプライトを押すと、gameStartが1に変更され、PLAYERスプライトのどこかにあります。
if (gameStart == 1)
{
[self addChild:PLAYER];
}
ただし、PLAYERスプライトが常にその情報を検索するようにコードを設定する方法がわかりません。