我的sprite位置有个奇怪的问题,我尝试了clean-build,重启xcode,运行不同的方案(iPhone5s,iPhone6),它们都返回相同的奇怪问题。
我试着设定精灵的位置:

balls.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width)

所以当我将ln(balls.position)打印到控制台时,它返回(0.0,0.0)
但当我试着
println(CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width)

它返回(512.01408.0),这是球应该在的正确位置。
最后一个函数func ballPosition有问题,它用于确定精灵“balls”的位置。出于某种原因,它总是(0,0)。
以下是我的测试项目的完整代码:
import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var circle = SKShapeNode()
    var balls = SKShapeNode()
    var ballColor = ["red", "blue", "green", "yellow"]
    var points = ["up", "down", "left", "right"]

    override func didMoveToView(view: SKView) {

        backgroundColor = SKColor.whiteColor()

        // set circle position and size

        circle = SKShapeNode(circleOfRadius: 100 ) // Size of Circle
        circle.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))  //Middle of Screen
        circle.strokeColor = SKColor.whiteColor()
        circle.fillColor = SKColor.orangeColor()
        self.addChild(circle)

    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */

        circleRotate()

        ballPosition()
        // test ball position, this is the part with the issue I mentioned above.
        println(balls.position) // (0, 0)
        println(CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width)) // (512.0,1408.0)

    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */

    }

    func circleRotate() {

        let circleAction = SKAction.rotateByAngle(CGFloat(-M_PI * 2 / 3), duration: 0.1)

        circle.runAction(SKAction.repeatAction(circleAction, count: 1))

    }

    func ballMove() {

        let ballMovement = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)), duration: 5)
        balls.runAction(ballMovement)

    }

    func randomColor() {

        let ballColorIndex = Int(arc4random_uniform(UInt32(ballColor.count)))

        balls = SKShapeNode(circleOfRadius: 10 )

        if ballColorIndex == 0 {

            balls.strokeColor = SKColor.whiteColor()
            balls.fillColor = SKColor.redColor()
            // balls.zPosition = 10
            ballMove()

        } else if ballColorIndex == 1 {

            balls.strokeColor = SKColor.whiteColor()
            balls.fillColor = SKColor.blueColor()
            // balls.zPosition = 10
            ballMove()

        } else if ballColorIndex == 2 {

            balls.strokeColor = SKColor.whiteColor()
            balls.fillColor = SKColor.greenColor()
            // balls.zPosition = 10
            ballMove()

        } else if ballColorIndex == 3 {

            balls.strokeColor = SKColor.whiteColor()
            balls.fillColor = SKColor.yellowColor()
            // balls.zPosition = 10
            ballMove()

        }

    }

    func ballPosition() {

        let ballPointIndex = Int(arc4random_uniform(UInt32(points.count)))

        if ballPointIndex == 0 {

            balls.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width)
            randomColor()
            self.addChild(balls)

        } else if ballPointIndex == 1 {

            balls.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) - self.frame.size.height)
            randomColor()
            self.addChild(balls)

        } else if ballPointIndex == 2 {

            balls.position = CGPoint(x:CGRectGetMidX(self.frame) - self.frame.size.width, y:CGRectGetMidY(self.frame))
            randomColor()
            self.addChild(balls)


        } else if ballPointIndex == 3 {

            balls.position = CGPoint(x:CGRectGetMidX(self.frame) + self.frame.size.width, y:CGRectGetMidY(self.frame))
            randomColor()
            self.addChild(balls)

        }

    }

}

最佳答案

问题是您正在覆盖randomColor()中的球-您正在创建一个尚未添加到父视图的新节点。
潜在的问题是,您的代码结构会导致混乱和错误。你应该保持你的功能单一。他们应该照他们说的做。名为randomColor()的函数不应移动移动球或设置球的大小。位置函数不应设置颜色。
我重新排列了代码并运行了它。那个窃听器修好了。你可以看到我改变了什么,现在应该可以走得更远了。祝你好运。

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var circle = SKShapeNode()
    var ball = SKShapeNode(circleOfRadius: 10)
    var ballColor = ["red", "blue", "green", "yellow"]
    var points = ["up", "dowm", "left", "right"]

    override func didMoveToView(view: SKView) {
        backgroundColor = SKColor.whiteColor()
        circle = SKShapeNode(circleOfRadius: 100 ) // Size of Circle
        circle.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame))  //Middle of Screen
        circle.strokeColor = SKColor.whiteColor()
        circle.fillColor = SKColor.orangeColor()
        self.addChild(circle)
        self.addChild(ball)
        ball.position = CGPointMake(150, 0)
        println("Initial Ball Pos:   \(ball.position)")
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        circleRotate()
        setRandomBallPosition()
        setRandomColor()
        ballMove()
        // test ball position
        println("--------------")
        println("Ball Pos:   \(ball.position)")
        println("Circle pos: \(circle.position)")
        println("Midpoint:   \(CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width))")
        println("--------------")
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }

    func circleRotate() {
        let circleAction = SKAction.rotateByAngle(CGFloat(-M_PI * 2 / 3), duration: 0.1)
        circle.runAction(SKAction.repeatAction(circleAction, count: 1))
    }

    func ballMove() {
        let ballMovement = SKAction.moveTo(CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame)), duration: 5)
        ball.runAction(ballMovement)
    }

    func setRandomColor() {
        ball.strokeColor = SKColor.whiteColor()
        let ballColorIndex = Int(arc4random_uniform(UInt32(ballColor.count)))
        switch(ballColorIndex) {
        case 0:
            ball.fillColor = SKColor.redColor()
        case 1:
            ball.fillColor = SKColor.blueColor()
        case 2:
            ball.fillColor = SKColor.greenColor()
        case 3:
            ball.fillColor = SKColor.yellowColor()
        default:
            println("Unexpected random index value ", ballColorIndex)
        }
    }

    func setRandomBallPosition() {
        let ballPointIndex = Int(arc4random_uniform(UInt32(points.count)))
        println("ballPointIndex = \(ballPointIndex)")
        switch(ballPointIndex) {
        case 0:
            ball.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + (self.frame.size.width / 2))
        case 1:
            ball.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) - (self.frame.size.height / 2))
        case 2:
            ball.position = CGPoint(x:CGRectGetMidX(self.frame) - (self.frame.size.width / 2), y:CGRectGetMidY(self.frame))
        case 3:
            ball.position = CGPoint(x:CGRectGetMidX(self.frame) + (self.frame.size.width / 2), y:CGRectGetMidY(self.frame))
        default:
            println("Unexpected random index value: ", ballPointIndex)
        }
        println("ball position = \(ball.position)")
    }
}

关于ios - 使用相同代码的不同 Sprite 位置,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29464869/

10-11 19:47
查看更多