Cocos2d iphone Cocos2D:偷偷操纵杆&x2B;接触事件、问题

Cocos2d iphone Cocos2D:偷偷操纵杆&x2B;接触事件、问题,cocos2d-iphone,Cocos2d Iphone,我在使用“鬼鬼祟祟的操纵杆”和其他东西时遇到了一些麻烦 我的目标是,使用操纵杆四处走动,触摸与周围区域互动 我有两层,ControlLayer(z:2)和GamePlayLayer(z:1) 我用瓷砖贴图绘制我的地面和地图。 操纵杆本身工作正常,它连接到ControlLayer。我会走路,科利德什么的 当我将触控事件添加到GamePlayLayer时,触控工作正常,我可以点击“地面”上的某个东西并与之交互。但是我的操纵杆不起作用,如果我用触摸键跑步,操纵杆只是坐在那里,没有反应 这是我的一些代码

我在使用“鬼鬼祟祟的操纵杆”和其他东西时遇到了一些麻烦

我的目标是,使用操纵杆四处走动,触摸与周围区域互动

我有两层,ControlLayer(z:2)和GamePlayLayer(z:1) 我用瓷砖贴图绘制我的地面和地图。 操纵杆本身工作正常,它连接到ControlLayer。我会走路,科利德什么的

当我将触控事件添加到GamePlayLayer时,触控工作正常,我可以点击“地面”上的某个东西并与之交互。但是我的操纵杆不起作用,如果我用触摸键跑步,操纵杆只是坐在那里,没有反应

这是我的一些代码,如果你们需要更多,尽管问

Gameplayer中的触摸方法

-(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { return YES; } -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLocation = [touch locationInView: [touch view]]; touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; touchLocation = [self convertToNodeSpace:touchLocation]; CGPoint tileCoord = [self tileCoordForPosition:touchLocation]; int tileGid = [meta tileGIDAt:tileCoord]; if (tileGid) { NSDictionary *properties = [tileMap propertiesForGID:tileGid]; if (properties) { NSString *collectable = [properties valueForKey:@"Collectable"]; if (collectable && [collectable compare:@"True"] == NSOrderedSame) { [meta removeTileAt:tileCoord]; [foreground removeTileAt:tileCoord]; } } } } -(void)带有TouchDispatcher的注册表 { [[CCTouchDispatcher sharedDispatcher]addTargetedLegate:self 优先级:0燕子头饰:是]; } -(BOOL)cctouch开始:(UITouch*)触摸事件:(UIEvent*)事件 { 返回YES; } -(无效)ccTouchEnded:(UITouch*)触摸事件:(UIEvent*)事件 { CGPoint touchLocation=[触摸位置视图:[触摸视图]]; touchLocation=[[CCDirector sharedDirector]convertToGL:touchLocation]; touchLocation=[自转换节点空间:touchLocation]; CGPoint tileCoord=[自身tileCoordForPosition:touchLocation]; int-tileGid=[meta-tileGIDAt:tilecord]; 如果(tileGid){ NSDictionary*properties=[tileMap propertiesForGID:tileGid]; 如果(属性){ NSString*collectable=[properties valueForKey:@“collectable”]; if(可收集和[可收集比较:@“True”]==可收集名称){ [meta removeTileAt:tileCoord]; [前台移除:tileCoord]; } } } } 我的场景是如何安排的:

@implementation SandBoxScene -(id)init { self = [super init]; if (self!=nil) { //Control Layer controlLayer = [GameControlLayer node]; [self addChild:controlLayer z:2 tag:2]; //GamePlayLayer GameplayLayer *gameplayLayer = [GameplayLayer node]; [gameplayLayer connectControlsWithJoystick:[controlLayer leftJoyStick] andJumpButton:[controlLayer jumpButton] andAttackButton:[controlLayer attackButton]]; [self addChild:gameplayLayer z:1 tag:1]; } return self; } @end @沙盒场景的实现 -(id)init{ self=[super init]; if(self!=nil){ //控制层 controlLayer=[GameControlLayer节点]; [self-addChild:controlLayer z:2标签:2]; //游戏玩家 GameplayLayer*GameplayLayer=[GameplayLayer节点]; [gameplayLayer ConnectControlsWithGoverage:[controlLayer LeftGoverage] andJumpButton:[controlLayer jumpButton] andAttackButton:[controlLayer attackButton]]; [self-addChild:Gameplayer z:1标签:1]; } 回归自我; } @结束
谢谢你的帮助

问题在于您的代码没有考虑多次触摸。使用
-(BOOL)cctouch开始:(UITouch*)触摸..
-(void)cctouch结束:(UITouch*)触摸..
将只进行一次触摸。。。相反,您需要使用
cctouchesend:…
您的方法应该如下所示:

-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    //get your touch from the set of UITouches
    UITouch *myTouch = [touches anyObject];  

    //the rest below is the same from your method
    CGPoint touchLocation = [myTouch locationInView: [myTouch view]];     

    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
    touchLocation = [self convertToNodeSpace:touchLocation];
    CGPoint tileCoord = [self tileCoordForPosition:touchLocation];
    int tileGid = [meta tileGIDAt:tileCoord];
    if (tileGid) {
        NSDictionary *properties = [tileMap propertiesForGID:tileGid];
        if (properties) {
            NSString *collectable = [properties valueForKey:@"Collectable"];
            if (collectable && [collectable compare:@"True"] == NSOrderedSame) {
                [meta removeTileAt:tileCoord];
                [foreground removeTileAt:tileCoord];
            }
        }
    }
}

您还需要添加
[glView setmultipletouchedabled:YES]到您的appDelegate。我不能完全肯定上述方法是否有效。处理如何使用cocos2d实现多点触摸。希望这有帮助

对不起,这不是解决办法,我只想处理一次触摸,问题在于优先级,我给我的游戏玩家设置了优先级0,并且每次都进行切换,所以我没有给优先级为1的控制玩家机会获得触摸并操作我的操纵杆

我找到的解决方案是将优先级从0更改为2,然后(controlLayer)操纵杆将动作并将触摸传递到优先级2,这是我的游戏玩家

-(void) registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { return YES; } -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLocation = [touch locationInView: [touch view]]; touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; touchLocation = [self convertToNodeSpace:touchLocation]; CGPoint tileCoord = [self tileCoordForPosition:touchLocation]; int tileGid = [meta tileGIDAt:tileCoord]; if (tileGid) { NSDictionary *properties = [tileMap propertiesForGID:tileGid]; if (properties) { NSString *collectable = [properties valueForKey:@"Collectable"]; if (collectable && [collectable compare:@"True"] == NSOrderedSame) { [meta removeTileAt:tileCoord]; [foreground removeTileAt:tileCoord]; } } } } 不过还是要谢谢你:)