开发者

Cocos2D: SneakyJoystick + Touch Events, problem

开发者 https://www.devze.com 2023-03-01 20:26 出处:网络
im having some trouble using SneakyJoystick and ccTouchEnded stuff. What is my goal, use the joystick to walk around and the touches to interact with the surrounding area.

im having some trouble using SneakyJoystick and ccTouchEnded stuff.

What is my goal, use the joystick to walk around and the touches to interact with the surrounding area.

I have two layers,ControlLayer (z:2) and GamePlayLayer (z:1) Im Using TiledMap for my ground and map. The Joystick by it self works fine, it is attached to the ControlLayer. I can walk, colide and stuff.

When i add the Touch events to the GamePlayLayer, the touch works, i can click on something on the "ground" and interact with it. BUT my joystick dosent work then, if i run using the touch the joystick just sit there not responsive.

Here is some of my code, if u guys need more, just ask.

Touch methods in the 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 (proper开发者_C百科ties) {
            NSString *collectable = [properties valueForKey:@"Collectable"];
            if (collectable && [collectable compare:@"True"] == NSOrderedSame) {
                [meta removeTileAt:tileCoord];
                [foreground removeTileAt:tileCoord];
            }

        }
    }
}

And how my scene is arranged:

@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

Thanks for the help!


The problem is that your code does not account for multiple touches. Using -(BOOL) ccTouchBegan:(UITouch *)touch.. or -(void) ccTouchEnded:(UITouch *)touch.. will only take one touch... instead you need to use ccTouchesEnded:.. your method should look something like this:

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

You also need to add [glView setMultipleTouchEnabled:YES]; to your appDelegate. I am not entirely sure that the method above works. This question deals with implementing multitouch with cocos2d. Hope this helps


Sorry thats was not the solution, i do want to handle just one touch, the problem was in the priority, i was giving priority 0 to my gameplayLayer, and swalloing every time, so i didnt give chance to the controlayer that had priority 1 to get the touch and act upon my joystick.

The solution that i found was changing the priority from 0 to 2 then, the (controlLayer) joystick would act and pass the touch to the priority 2, that was my gameplayLayer

but thanks anyway :)

0

精彩评论

暂无评论...
验证码 换一张
取 消