2011-04-28 80 views
0

即时通讯使用SneakyJoystick和ccTouchEnded东西有一些麻烦。Cocos2D:SneakyJoystick +触摸事件,问题

我的目标是什么,使用操纵杆走动和触摸与周围地区互动。我有两个图层,ControlLayer(z:2)和GamePlayLayer(z:1) Im使用TiledMap作为我的地面和地图。 由它的游戏杆自我工作正常,它附加到ControlLayer。我可以走路,冒险和东西。

当我将触摸事件添加到GamePlayLayer时,触摸可以工作,我可以点击“地面”上的某些东西并与其进行交互。但我的操纵杆dosent工作,然后,如果我运行使用触摸操纵杆只是坐在那里没有响应。

这里是我的一些代码,如果你们需要更多,请问。在GameplayLayer

 
-(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]; 
      } 

     } 
    } 
} 

而如何

触摸方式,我的现场布置:

 
@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 

感谢您的帮助!

回答

1

问题是您的代码没有考虑多个触摸。使用-(BOOL) ccTouchBegan:(UITouch *)touch..-(void) ccTouchEnded:(UITouch *)touch..只需要一个触摸......不是,您需要使用您的ccTouchesEnded:..方法应该是这个样子:

-(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 setMultipleTouchEnabled:YES];到您的appDelegate。我不完全确定上述方法的工作原理。 This question涉及使用cocos2d实施多点触控。希望这有帮助

0

对不起,这不是解决方案,我只想处理一个触摸,问题是优先考虑的,我优先0给我的gameplayLayer,并swalloing每次,所以我没有机会控制者有优先权1来触摸并操作我的操纵杆。

,我发现从0到2改变优先级以后,(controlLayer)操纵杆将采取行动,并通过触摸优先级2,那是我gameplayLayer

但还是谢谢你:)

解决方案