Thursday, March 13, 2014

Sprite Kit: Up is down, down is up

There's no error on messaging the location.x to moveToX action of SKAction, think spaceship which just move left and right and remains stationary on its Y


-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch* touched = [touches anyObject];
    

    CGPoint location = [touched locationInView:self.view];

    
    SKAction* moveAction = [SKAction moveToX: location.x duration:0.0];
    
    
    [player runAction: moveAction];
}


However, it's preferable to use locationInNode. If we want the spaceship to move in all direction yet we use locationInView, the app will exhibit this same error: http://stackoverflow.com/questions/21948027/skaction-moveto-goes-up-when-should-go-down

CGPoint location = [touched locationInView:self.view]
SKAction* moveAction = [SKAction moveTo: location duration:0.0];    


Though the spaceship's X will move accordingly to your touch, the spaceship's Y will not follow your finger movements.


To rectify that flaw, get the location from SKScene's view (self) instead of the UIView's view (self.view), then message that view to touched object's locationInNode instead of locationInView


CGPoint location = [touched locationInNode:self];
SKAction* moveAction = [SKAction moveTo: location duration:0.0];



Happy Coding! ツ

No comments:

Post a Comment