SKActionTo moves up when it should lower

I followed SpriteKit's tutorial here to create a simple sprite shooter where you make a spaceship that shoots asteroid lasers.

I want the lasers (each laser is SKSpriteNode) to move to the point where I click. I get the message correctly in the method touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event. However, when I set SKActionto SKSpriteNode, it moves in the y direction of OPPOSITE, where I click. Those. the image has a width (x) 500 and a height (y) 400. When I touch the screen at the coordinate (300, 100), the laser seems to move to the coordinate (300, 300).

I checked that the coordinates touchLocationare correct.

FYI I only used the iPhone simulator for this - but that doesn't matter, does it?

Corresponding code snippet:

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    SKSpriteNode *shipLaser = [_shipLasers objectAtIndex:_nextShipLaser];

    shipLaser.position = CGPointMake(_ship.position.x+shipLaser.size.width,_ship.position.y+0);
    shipLaser.hidden = NO;
    [shipLaser removeAllActions];

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView:self.view];

    SKAction *laserMoveAction = [SKAction moveTo:touchLocation duration:0.5];
    SKAction *laserDoneAction = [SKAction runBlock:(dispatch_block_t)^() {
        shipLaser.hidden = YES;
    }];

    SKAction *moveLaserActionWithDone = [SKAction sequence:@[laserMoveAction,laserDoneAction]];
    [shipLaser runAction:moveLaserActionWithDone withKey:@"laserFired"];
}

EDIT: I wonder if this could be related to the fact that the SprikeKit coordinate system originates from the left, while UIKit comes from the upper left corner.

+3
source share
2 answers

You want touchLocation in SKScene and not in UIView.

change:

CGPoint touchLocation = [touch locationInView:self.view];

to:

CGPoint touchLocation = [touch locationInNode:self];
+4
source

The problem was that the SprikeKit coordinate system originates on the left, unlike the UIKit coordinate system, which appears in the upper right corner. Thus, the coordinates for the touch event were in the UIkit coordinate system, and SKNode moved in the SpriteKit system. Once I understood this difference, it was pretty easy to fix.

+1
source

All Articles