快速移动pacman

时间:2015-06-20 10:51:42

标签: swift move pacman

我是swift的新手,我正在尝试编程pacman。我试图将pacman移动到滑动的方向,到目前为止我已设法将其移动到屏幕的边缘,问题是当我尝试移动它不是从屏幕的边缘而是在中间滑动动作,它只是到屏幕的边缘并移动到滑动方向,这是一个方向的代码:

    var x = view.center.x
    for var i = x; i > 17; i--
    {
        var origin: CGPoint = self.view.center
        var move = CABasicAnimation(keyPath:"position.x")
        move.speed = 0.13
        move.fromValue = NSValue(nonretainedObject: view.center.x)
        move.toValue = NSValue(nonretainedObject: i)
        view.layer.addAnimation(move, forKey: "position")
        view.center.x = i
    }

问题是,我知道问题,当我向我想要的方向滑动for循环不会等待动画停止但它将在不到一秒的时间内完成循环,我需要一些延迟这里或其他代码。

1 个答案:

答案 0 :(得分:0)

这是一个有趣的问题,所以我决定在SpriteKit中做一个例子。没有碰撞检测,路径查找或甚至路径。这仅仅是如何制作“吃豆人”的一个例子。滑动发生时改变方向。

我已将GameScene包括在内:

class GameScene: SKScene {
    enum Direction {
        case Left
        case Right
        case Up
        case Down
    }
    lazy var openDirectionPaths = [Direction: UIBezierPath]()
    lazy var closedDirectionPaths = [Direction: UIBezierPath]()
    lazy var wasClosedPath = false
    lazy var needsToUpdateDirection = false
    lazy var direction = Direction.Right
    lazy var lastChange: NSTimeInterval = NSDate().timeIntervalSince1970

    var touchBeganPoint: CGPoint?
    let pacmanSprite = SKShapeNode(circleOfRadius: 15)

    override func didMoveToView(view: SKView) {
        let radius: CGFloat = 15, diameter: CGFloat = 30, center = CGPoint(x:radius, y:radius)
        func createPaths(startDegrees: CGFloat, endDegrees: CGFloat, inout dictionary dic: [Direction: UIBezierPath]) {
            var path = UIBezierPath(arcCenter: center, radius: radius, startAngle: startDegrees.toRadians(), endAngle: endDegrees.toRadians(), clockwise: true)
            path.addLineToPoint(center)
            path.closePath()
            dic[.Right] = path
            for d: Direction in [.Up, .Left, .Down] {
                path = path.pathByRotating(90)
                dic[d] = path
            }
        }
        createPaths(35, 315, dictionary: &openDirectionPaths)
        createPaths(1, 359, dictionary: &closedDirectionPaths)
        pacmanSprite.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        pacmanSprite.fillColor = UIColor.yellowColor()
        pacmanSprite.lineWidth = 2
        if let path = openDirectionPaths[.Right] {
            pacmanSprite.path = path.CGPath
        }
        pacmanSprite.strokeColor = UIColor.blackColor()
        self.addChild(pacmanSprite)
        updateDirection()

        // Blocks to stop 'Pacman' changing direction outside of a defined path?
        //375/25 = 15 width
        //666/37 = 18 height
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        touchBeganPoint = positionOfTouch(inTouches: touches)
    }

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        if let touchStartPoint = touchBeganPoint,
            touchEndPoint = positionOfTouch(inTouches: touches) {
                if touchStartPoint == touchEndPoint {
                    return
                }
                let degrees = atan2(touchStartPoint.x - touchEndPoint.x,
                    touchStartPoint.y - touchEndPoint.y).toDegrees()
                var oldDirection = direction
                switch Int(degrees) {
                case -135...(-45):  direction = .Right
                case -45...45:      direction = .Down
                case 45...135:      direction = .Left
                default:            direction = .Up
                }
                if (oldDirection != direction) {
                    needsToUpdateDirection = true
                }
        }
    }

    override func touchesCancelled(touches: Set<NSObject>!, withEvent event: UIEvent!) {
        touchBeganPoint = nil
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
        if let nodes = self.children as? [SKShapeNode] {
            for node in nodes {
                let p = node.position
                let s = node.frame.size
                //let s = node.size
                if p.x - s.width > self.size.width {
                    node.position.x = -s.width
                }
                if p.y - s.height > self.size.height {
                    node.position.y = -s.height
                }
                if p.x < -s.width {
                    node.position.x = self.size.width + (s.width / 2)
                }
                if p.y < -s.height {
                    node.position.y = self.size.height + (s.height / 2)
                }
                if needsToUpdateDirection || NSDate().timeIntervalSince1970 - lastChange > 0.25 {
                    if let path = wasClosedPath ? openDirectionPaths[direction]?.CGPath : closedDirectionPaths[direction]?.CGPath {
                        node.path = path
                    }
                    wasClosedPath = !wasClosedPath
                    lastChange = NSDate().timeIntervalSince1970
                }
                updateDirection()
            }
        }
    }

    // MARK:- Helpers

    func positionOfTouch(inTouches touches: Set<NSObject>) -> CGPoint? {
        for touch in (touches as! Set<UITouch>) {
            let location = touch.locationInNode(self)
            return location
        }
        return nil
    }

    func updateDirection() {
        if !needsToUpdateDirection {
            return
        }
        pacmanSprite.removeActionForKey("Move")
        func actionForDirection() -> SKAction {
            let Delta: CGFloat = 25
            switch (direction) {
            case .Up:
                return SKAction.moveByX(0.0, y: Delta, duration: 0.1)
            case .Down:
                return SKAction.moveByX(0.0, y: -Delta, duration: 0.1)
            case .Right:
                return SKAction.moveByX(Delta, y: 0.0, duration: 0.1)
            default:
                return SKAction.moveByX(-Delta, y: 0.0, duration: 0.1)
            }
        }
        let action = SKAction.repeatActionForever(actionForDirection())
        pacmanSprite.runAction(action, withKey: "Move")
        needsToUpdateDirection = false
    }
}

存储库可以是variable declaration within the while loop C/C++

我已经添加了MIT许可证,因此您可以根据需要分叉此存储库。我希望这会有所帮助。