0
votes

I am making a modified version of the game, 2048. I found this tutorial and used it to build the basic game and then edit it to meet what I'm trying to do, but I have run into some issues.

To give further info, the "tiles" that have the number are their own instanced class, called simply Tile. The displayed grid is nothing more than an array containing either a *_noTile for each blank space, or an instance of the Tile class.

I want to use UILongPressGestureRecognizer to "select" and highlight one of the tiles. I have added this in through information I found on here. Originally I tried using the below code in my Grid class (from the tutorial) as part of the didLoadFromCCB method which contains the swipe gesture recognizers, since it made since to put all gesture recognition in the same place. This is from this tutorial.

UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(onLongPress:)];
[longpress setMinimumDuration:0.5];
[[[CCDirector sharedDirector] view] addGestureRecognizer:longpress];

I tested this and its working to detect the long press just fine. But where I am running into issue is figuring out how to select the "Tile" when the user does a long press. I tried looking through the move method from the tutorial to figure out how it selected the tile, but it had more to do with swipe direction.

I have seen a bunch of ways on here to select using convertToNodeSpace, but every example was from a Sprite and I'm not using a sprite (there is one in the node but I need to select the node not the sprite). I have tried doing this within the Tile class and in my Grid class, neither seem to work so I'm coming here asking for advice on how I might do this. Below is an example of my handle long press method that I put in the Tile class.

-(void)_handleLongPressGesture:(UILongPressGestureRecognizer *)recognizer {
    if(recognizer.state == UIGestureRecognizerStateBegan) {
        CCLog(@"long press");

        CGPoint touchPoint = [[CGDirector sharedDirector] convertToGL:[recognizer locationInView:[recognizer view]]];
        touchPoint = [self convertToNodeSpace:touchPoint];
        CGRect boundingBox = [self boundingBox];
        if(CGRectContainsPoint(boundingBox, touchPoint)) {
            self.backgroundNode.color = [CCColor whiteColor];
            self.valueLabel.color = [CCColor blackcolor];
        }
    }
}

Can anyone help me figure this out? Basically the end goal is to be able to longpress on a tile to have it "highlight" by changing colors, then as the user continues to hold down and slide their finger to tiles next to the long-pressed tile, values from the tiles will be combined. I'd be willing to talk more over Skype or any other way to get this problem solved. Thanks.

------------------- FIX Thanks to denis alenti for pointing me in the right general direction here. Figured I'd post how I fixed this in the end. My Tile class is a child of my Grid class, so I just moved the gesture recognition there and created the below method. I'm still playing around with it a bit but it has been working every time I press on a tile so far.

-(void)onLongPress:(UILongPressGestureRecognizer *) recognizer {
    CGPoint touchPoint = [[CCDirector sharedDirector] convertToGL:[recognizer locationInView:[recognizer view]]];
    touchPoint = [self convertToNodeSpace:touchPoint];

    if (recognizer.state == UIGestureRecognizerStateBegan) {
        for (Tile *tile in self.children) {
            if([tile isKindOfClass:[Tile class]) {
                CGRect tileBoundingBox = tile.boundingbox;

                if (CGRectContainsPoint(tileBoundingBox, touchPoint) {
                    // perform action here
                }
            }
        }
    }
}
1

1 Answers

0
votes

Try using something like this. In your _handleLongPressGesture use for loop to go trough all your items, in this case CCSprite, on stage and then use radius or you boundingBox soulution for limits.

for (CCSprite *item in self.children) {
   if ([item isKindOfClass:[CCSprite class]]) {
            float radius = item.contentSize.width/2.0;
            float distance = pow(item.position.x - touchPoint.x, 2) + pow(item.position.y - touchPoint.y, 2);
            distance = sqrt(distance);

            if (distance <= radius) {
                //do something

                break;
            }
    }
}