Question

i'm trying to implement a grid movement in my game. The grid has one column and three lines.

[ ] Y3 - 60 % of height
[ ] Y2 - 40% of height
[ ] Y1 - 20% of height

In iphone with 3.5 inch, these height are:

[ ] Y3 - 193px
[ ] Y2 - 128px
[ ] Y1 - 64px

When the user taps the screen above the NPC, the NPC should move to next cell above him (e.g. if NPC is in Y1, he should go to Y2). And the same thing should happen in the opposite direction. But, the NPC should never com above the 193px or below 64px. Which is not happening. If you tap twice, when NPC is moving, he starts moving faster and ignores the grind, and go above/below the boundaries.

This is my code. How can I improve it to do what I want?

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {

CGPoint touchLocation = [touch locationInNode:self];

positionY1 = self.contentSize.height*.2;
positionY2 = self.contentSize.height*.40;
positionY3 =  self.contentSize.height*.60;


float playerMove;

if (touchLocation.y > _player.position.y) {

    if (_player.position.y >= positionY3) {
        NSLog(@"decisao 1");
        playerMove = positionY3;

    } else  if (_player.position.y >= positionY2 && _player.position.y <= positionY3) {
        NSLog(@"decisao 2");
        playerMove = positionY3;

    } else  if (_player.position.y >= positionY1 && _player.position.y <= positionY2) {
        NSLog(@"decisao 3");
        playerMove = positionY2;

    } else  if (_player.position.y <= positionY1) {
        NSLog(@"decisao 4");
        playerMove = positionY1;

    }           

} else if ((touchLocation.y < _player.position.y)) {

    if (_player.position.y <= positionY1) {
        NSLog(@"decisao 5");
        playerMove = positionY1;

    } else  if (_player.position.y >= positionY1 && _player.position.y <= positionY2) {
        NSLog(@"decisao 6");
        playerMove = positionY1;

    } else  if (_player.position.y >= positionY2 && _player.position.y <= positionY3) {
        NSLog(@"decisao 7");
        playerMove = positionY2;

    } else  if (_player.position.y >= positionY3) {
        NSLog(@"decisao 8");
        playerMove = positionY3;

    }


}

// Move our sprite to touch location
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:CGPointMake(_player.position.x, playerMove)];
[_player runAction:actionMove];

}

Was it helpful?

Solution

One problem certainly is that you allow multiple move actions to stop, so every time you tap another move action runs regardless of whether there's already a move action running. So remove the move actions (or all of them) before running a new one:

[_player stopAllActions]; // or: remove by tag
[_player runAction:actionMove];

Next, if none of your if/else are true then the value of playerMove is left undefined:

float playerMove;

If that were to happen the variable may hold any value at random, possibly moving the node far, far away from the screen. That'll make it appear like the sprite would simply disappear and give you a hard time to figure out what's going on.

It is good practice, very good practice indeed, to always initialize standard value types with a default value:

float playerMove = 0.0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top