【发布时间】:2015-04-06 03:17:00
【问题描述】:
我的精灵位置有一个奇怪的问题,我尝试干净构建,重新启动 xcode,并在不同的方案(iPhone5s,iPhone6)中运行,它们都返回相同的奇怪问题。 我尝试通过以下方式设置精灵的位置:
balls.position = CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width)
所以当我 println(balls.position) 到控制台时,它返回 (0.0, 0.0)
但是当我尝试时
println(CGPoint(x:CGRectGetMidX(self.frame), y:CGRectGetMidY(self.frame) + self.frame.size.width)
它返回 (512.0,1408.0),这是球应该在的正确位置。
我遇到了最后一个函数 func ballPosition 的问题,它用于确定精灵“球”的位置。出于某种原因,它总是 (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)
}
}
}
【问题讨论】:
-
屏幕中间是(512.0, 384.0),精灵应该从屏幕顶部移动到屏幕中。但相反,精灵总是来自屏幕左下方的外部。控制台中的位置总是打印 (0, 0)
-
谢谢,我也会修改代码,方便大家阅读。
-
我发现了问题并包含了您的代码工作版本的示例。您可能会发现要替换节点或添加多个节点,但这只是一个小调整。
标签: ios swift sprite-kit