0
我正在用Cocos2d爲iPhone構建遊戲,現在我正試圖讓Touch輸入工作。我已經在多層場景中的獨立控制層上啓用了觸摸響應,並且它工作正常 - 除非觸摸方法在觸摸位於單獨圖層上的精靈頂部時觸發觸摸方法(單獨的圖層爲實際上只是一個節點)。除了這個精靈之外,我沒有其他的屏幕內容。Cocos2d輸入圖層僅接收精靈頂部的觸摸
這裏是我的控制層實現:
#import "ControlLayer2.h"
extern int CONTROL_LAYER_TAG;
@implementation ControlLayer2
+(void)ControlLayer2WithParentNode:(CCNode *)parentNode{
ControlLayer2 *control = [[self alloc] init];
[parentNode addChild:control z:0 tag:CONTROL_LAYER_TAG];
}
-(id)init{
if (self=[super init]){
[[[CCDirector sharedDirector]touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;
}
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
CCLOG(@"touchbegan");
return YES;
}
@end
,這裏是與內部具有精靈的子節點層:
extern int PLAYER_LAYER_TAG;
int PLAYER_TAG = 1;
@implementation PlayerLayer
//reduces initializing and adding to the gamescene to one line for ease of use
+(void)PlayerLayerWithParentNode:(CCNode *)parentNode{
PlayerLayer *layer = [[PlayerLayer alloc] init];
[parentNode addChild:layer z:1 tag:PLAYER_LAYER_TAG];
}
-(id)init{
if(self = [super init]){
//add the player to the layer, know what I'm sayer(ing)?
[Player playerWithParentNode:self];
}
return self;
}
@end
最後,包含他們兩個場景:
int PLAYER_LAYER_TAG = 1;
int CONTROL_LAYER_TAG = 2;
@implementation GameScene
+(id)scene{
CCScene *scene = [CCScene node];
CCLayer *layer = [GameScene node];
[scene addChild:layer z:0 tag:0];
return scene;
}
-(id)init{
if(self = [super init]){
//[[[CCDirector sharedDirector]touchDispatcher] addTargetedDelegate:[self getChildByTag:0] priority:0 swallowsTouches:YES];
//add the player layer to the game scene (this contains the player sprite)
[ControlLayer2 ControlLayer2WithParentNode:self];
[PlayerLayer PlayerLayerWithParentNode:self];
}
return self;
}
@end
我該如何使控制層響應所有觸摸輸入?
我想這一點,達到同樣的效果。 – Swanijam 2013-03-16 16:59:06
ControlLayer2的父級是GameScene。在GameScene中未啓用觸摸。因此,在GameScene中放置self.touchEnabled = YES並將ccTouchesBegan放在相同的位置... – Guru 2013-03-16 17:00:39
將所有觸摸控件放入GameScene中?我想我可以,但是我想在一個單獨的課程中處理所有的問題,以確保代碼和組織的清晰。或者你的意思是把self.touchEnabled放在gamescene中,但是ControlLayer2類中的觸摸控件? – Swanijam 2013-03-16 17:06:11