5
\$\begingroup\$

Part of me thinks that it would be simpler to ditch Apple's move API and just updated position with a timer. I know that's what I did with a JS canvas snake game I built a while ago. But I'm also trying to learn the iOS API so unless that's clearly the best choice I'd prefer the most idiomatic way.

Also, I'm pretty sure that technically this could overflow but it seems almost impossibly unlikely unless that were someone's goal.

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */
    snakeHead.removeAllActions()
    var actionMove = SKAction()
    if notYetTouched {
        introLabel.removeFromParent()
        notYetTouched = false
        actionMove = SKAction.moveByX(CGFloat(self.size.width / 2), y: 0, duration: 1)
    } else {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            actionMove = SKAction.moveBy(CGVector(dx: location.x - snakeHead.position.x , dy: location.y - snakeHead.position.y), duration: 1)                
        }
    }
snakeHead.runAction(
  SKAction.repeatActionForever(
    SKAction.sequence( [ SKAction.runBlock({ self.wraparound(actionMove) }), actionMove]) ) )
}

func wraparound(defaultAction: SKAction) -> SKAction? {

    if snakeHead.position.y > size.height  {
        snakeHead.removeAllActions()
        let repositionSnakeHeadAction = SKAction.moveToY(1, duration:0)

        snakeHead.runAction( SKAction.sequence( [ repositionSnakeHeadAction, SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock({ wraparound(defaultAction) }), defaultAction]))] ) )
    } else if snakeHead.position.y < 1 {
        snakeHead.removeAllActions()
        let repositionSnakeHeadAction = SKAction.moveToY(size.height - 1, duration: NSTimeInterval(0))

        snakeHead.runAction( SKAction.sequence( [ repositionSnakeHeadAction, SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock({ wraparound(defaultAction) }), defaultAction]))] ) )
    } else if snakeHead.position.x > size.width  {
        snakeHead.removeAllActions()
        let repositionSnakeHeadAction = SKAction.moveToX(1, duration: NSTimeInterval(0))

        snakeHead.runAction( SKAction.sequence( [ repositionSnakeHeadAction, SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock({ wraparound(defaultAction) }), defaultAction]))] ) )
    } else if snakeHead.position.x < 1 {
        snakeHead.removeAllActions()
        let repositionSnakeHeadAction = SKAction.moveToX(size.width, duration: NSTimeInterval(0))

        snakeHead.runAction( SKAction.sequence( [ repositionSnakeHeadAction, SKAction.repeatActionForever(SKAction.sequence([SKAction.runBlock({ wraparound(defaultAction) }), defaultAction]))] ) )
    } else {
        return defaultAction
    }
    return nil
}
\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to Code Review! Hope you get some great answers! \$\endgroup\$ – Phrancis Sep 4 '15 at 0:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.