Search code examples
iosif-statementios7sprite-kitsprite

Make a sprite go to a certain position depending on the sprite's current position?


I think my title is rather confusing but I'm not really sure how to title it. Here is my current code:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *moveNodeRight = [SKAction moveByX:-209.0 y:0.0 duration:0.85];
 [_squirrelSprite runAction: moveNodeRight withKey:@"changeside"];
//_squirrelSprite.xScale = -1.0;

if(_squirrelSprite.xScale == -1.0)
{
    _squirrelSprite.xScale = 1.0;
} else {
    _squirrelSprite.xScale = -1.0;
}
}

When I tap the screen the sprite moves to a new position (X:-209.0, Y:0.0). The new position is to the left of the current position. When I tap the screen a second time, the sprite just moves farther left off of the screen.

What I want: I'm happy with what I have so far, but when the sprite is at the new position (X:-209.0, Y:0.0), I want the second tap to return it to it's original position. I think it would look something like the above code, but so far nothing is working. Below is an idea of what it could possibly look like. Thank you!

if(_squirrelSprite.position == the second position)
{
    _squirrelSprite.position = the first position;
} else {
    _squirrelSprite.xScale = the second position;
}

Solution

  • This moves a node from one position to another when the user taps the screen. It then moves the node back to the original position when the user taps the screen again.

    @interface MyScene()
    
    @property SKSpriteNode *squirrelSprite;
    @property BOOL atFirstPosition;
    @property CGPoint firstPosition;
    
    @end
    
    @implementation MyScene
    
    -(id)initWithSize:(CGSize)size
    {
        if (self = [super initWithSize:size])
        {
            _firstPosition = CGPointMake(300, self.frame.size.height/2);
            _squirrelSprite = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(32, 32)];
            _squirrelSprite.position = _firstPosition;
            _atFirstPosition = YES;
            [self addChild:_squirrelSprite];
        }
        return self;
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if (_atFirstPosition)
        {
            SKAction *moveNodeLeft = [SKAction moveByX:-209.0 y:0.0 duration:0.85];
            [_squirrelSprite runAction: moveNodeLeft withKey:@"changeside"];
        } else {
            _squirrelSprite.position = _firstPosition;
        }
        _atFirstPosition = !_atFirstPosition;
        _squirrelSprite.xScale *= -1.0;
    }
    
    @end
    

    EDIT: This moves the node back to its original position using an SKAction

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        if (_atFirstPosition)
        {
            SKAction *moveNodeLeft = [SKAction moveByX:-209.0 y:0.0 duration:0.85];
            [_squirrelSprite runAction: moveNodeLeft withKey:@"moveleft"];
        } else {
            SKAction *moveNodeRight = [SKAction moveByX:209.0 y:0.0 duration:0.85];
            [_squirrelSprite runAction: moveNodeRight withKey:@"moveright"];
        }
        _atFirstPosition = !_atFirstPosition;
        _squirrelSprite.xScale *= -1.0;
    }