Cocos2D:SneakyJoystick +触摸事件,问题

时间:2011-04-28 12:32:42

标签: cocos2d-iphone

我在使用SneakyJoystick和ccTouchEnded时遇到了一些麻烦。

我的目标是什么,使用操纵杆走动以及与周围区域互动的接触。

我有两个层,ControlLayer(z:2)和GamePlayLayer(z:1) 我使用TiledMap为我的地面和地图。 操纵杆自动工作正常,它附在ControlLayer上。我可以走路,打算和东西。

当我将Touch事件添加到GamePlayLayer时,触摸起作用,我可以点击“地面”上的某些内容并与之互动。但是我的操纵杆会起作用,如果我使用触摸操作操纵杆只是坐在那里没有响应。

以下是我的一些代码,如果你们需要更多代码,请问。

触摸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

感谢您的帮助!

2 个答案:

答案 0 :(得分: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];添加到a​​ppDelegate。我不完全确定上述方法是否有效。 This question涉及使用cocos2d实现多点触控。希望这有帮助

答案 1 :(得分:0)

很抱歉这不是解决方案,我确实想要只处理一次,问题是优先级,我给我的gameplayLayer优先级0,并且每次都是swalloing,所以我没有给机会的控制器优先级1来触摸并操纵我的操纵杆。

我找到的解决方案是将优先级从0更改为2然后,(controlLayer)操纵杆将执行并将触摸传递给优先级2,这是我的gameplayLayer

但无论如何,谢谢:)