如何在一定限度内使用操纵杆移动精灵?

时间:2016-06-29 04:55:53

标签: swift sprite-kit sprite move

我想这样做,玩家可以用操纵杆移动,但不能离开圆圈。我制作了操纵杆,但我不知道如何做其他的东西。下图中有一个示例,也是我的代码。希望有人可以帮助我,谢谢。

class GameScene: SKScene {

    var circuloPrincipal = SKSpriteNode(imageNamed: "circulo")
    var circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")
    let base = SKSpriteNode(imageNamed: "circuloFondo")
    let ball = SKSpriteNode(imageNamed: "circulo")
    var stickActive:Bool = false



    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        base.size = CGSize(width: 100, height: 100)
        base.alpha = 0.3
        base.zPosition = 2.0
        base.position = CGPoint(x: frame.width / 2, y: frame.height / 2 - 310)
        self.addChild(base)

        ball.size = CGSize(width: 50, height: 50)
        ball.color = circuloPrincipal.color
        //ball.alpha = 0
        ball.zPosition = 3.0
        ball.position = base.position
        self.addChild(ball)

        circuloPrincipal.size = CGSize(width: 35, height: 35)
        circuloPrincipal.position = CGPoint(x: frame.width / 2, y: frame.height / 2)
        self.addChild(circuloPrincipal)
        circuloPrincipal.color =  UIColor(red: 0.75, green: 0.75, blue: 0.75, alpha: 1.0)
        circuloPrincipal.colorBlendFactor = 1.0
        circuloPrincipal.zPosition = 3.0
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event:     UIEvent?) {


        for touch in touches {
            let location = touch.locationInNode(self)

            if (CGRectContainsPoint(ball.frame, location)) {

                stickActive = true
            }else {

                stickActive = false

            }

        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch in touches {
            let location = touch.locationInNode(self)

            if (stickActive == true) {


            let v = CGVector(dx: location.x - base.position.x, dy: location.y - base.position.y)
            let angle = atan2(v.dy, v.dx)
            let deg = angle * CGFloat(180 / M_PI)
            print(deg + 180)

            let lenght:CGFloat = base.frame.size.height / 2 - 20
            let xDist: CGFloat = sin(angle - 1.57079633) * lenght
            let yDist: CGFloat = cos(angle - 1.57079633) * lenght

            ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

            if (CGRectContainsPoint(base.frame, location)) {

                ball.position = location
            }else {

                ball.position = CGPointMake(base.position.x - xDist, base.position.y + yDist)

            }


            } // termina stickActive

        }

    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (stickActive == true) {

            let move: SKAction = SKAction.moveTo(base.position, duration: 0.2)
            move.timingMode = .EaseOut

            ball.runAction(move)

        }
    }

1 个答案:

答案 0 :(得分:1)

查看SKConstraint - 您可以设置一个约束,即节点不能移动到距另一个节点指定距离的位置。你要设置这个&#39;其他&#39;节点到播放器限制区域的中心。如果它转到远处,Sprite-Kit会自动移回你的节点。由于这是在节点重新移动之前以60fps进行游戏循环,因此你不会得到任何奇怪的“跳跃”。的效果。

https://developer.apple.com/reference/spritekit/skconstraint

与其他一些SK文档相比,该文档有点缺乏。这里以及制作绿色矩形的示例&#39;关注&#39;指定距离内的黄色三角形:

let distanceRange = SKRange(lowerLimit: 200, upperLimit: 400)
let distanceConstraint = SKConstraint.distance(distanceRange, toNode: yellowTriangle)  
greenRect.constraints = [distanceConstraint]

SKNode(greenRect)的constraints属性是一个数组,所以如果你还想要一个方向约束(例如,让greenRect面向黄色三角形),你可以编写如下代码:

let orientRange = SKRange(lowerLimit: 0.0, upperLimit: 0.0)
let orientConstraint = SKConstraint.orientToNode(yellowTriangle, offset: orientRange)
greenRect.constraints = [orientatConstraint, distanceConstraint]

对于您的特定示例,您可能希望将约束设置为:

let distanceRange = SKRange(lowerLimit: 0, upperLimit: limitcircle.size/2)
 let distanceConstraint = SKConstraint.distance(distanceRange, toPoint: limitCircle.position)
player.constraints = [distanceConstraint]

这假设玩家不能移动的圆圈是名为limitcircle的SKNode,它的锚点设置为(0.5,0.5)i,e,它的中心。上面的代码会将玩家从圆心的一个点约束到圆的宽度(即它的半径)的0到1/2之间的距离。