问题
I'm having a problem in my code. When my obstacles come down forever, the "path" where the square is supposed to go through is sometime off the screen, and not positioned where I want it. I want the path to be between 1/4 of the screen width. I think the error is somewhere in the width and randomXPos constants. How can I fix this? (If you want a further explanation I can explain more.)
func addObstacles2() {
let obstacle2A = SKSpriteNode(imageNamed: "Obstacle")
let obstacle2B = SKSpriteNode(imageNamed: "Obstacle")
let width = UInt32(self.frame.size.width / 5)
let randomXPos = CGFloat(arc4random_uniform(UInt32(width)))
let gapWidth = square.size.width * 2
let moveDown2A = SKAction.moveByX(0, y: -self.size.width, duration: 2.5)
let repeatAction2A = SKAction.repeatActionForever(moveDown2A)
let removeObstacle2A = SKAction.removeFromParent()
let moveAndRemove2A = SKAction.sequence([repeatAction2A, removeObstacle2A])
obstacle2A.size = CGSize(width: self.frame.size.width, height: 300)
obstacle2A.position = CGPointMake(CGRectGetMidX(self.frame) + obstacle2A.size.width / 2 + gapWidth / 2 + randomXPos, self.frame.size.height * 2)
obstacle2A.zPosition = 40
obstacle2A.runAction(moveAndRemove2A)
self.addChild(obstacle2A)
let moveDown2B = SKAction.moveByX(0, y: -self.size.width, duration: 2.5)
let repeatAction2B = SKAction.repeatActionForever(moveDown2B)
let removeObstacle2B = SKAction.removeFromParent()
let moveAndRemove2B = SKAction.sequence([repeatAction2B, removeObstacle2B])
obstacle2B.size = CGSize(width: self.frame.size.width, height: 300)
obstacle2B.position = CGPointMake(CGRectGetMidX(self.frame) - obstacle2B.size.width / 2 - gapWidth / 2 + randomXPos, self.frame.size.height * 2)
obstacle2B.zPosition = 40
obstacle2B.runAction(moveAndRemove2B)
self.addChild(obstacle2B)
}
(Please note: I have a function that already makes the obstacles spawn forever.)
回答1:
Ok so it looks like you're subtracting a little too much from the Object2B's x position.
The first important thing to realize is that self.frame.size.width
doesn't return the width of what the user sees on his device. It will return a little extra, actually, because sprite kit is configured by default to have a set coordinate system in order to allow the developer to hard-code in the coordinates and see the same thing on each device. (It's the scale mode that the scene has by default - SKSceneScaleModeResizeFill
).
So, if we take a look mathematically at what you are doing, we see that you are starting out in the middle of the view, (x coordinate 512, I believe). Then, you subtract half of that value, putting you at x coordinate 256 (getting close already to going off the screen). Then you subtract even more, thus resulting in some of the nodes being off the screen.
There isn't a problem with your random value. It's that with your default coordinate system your not seeing some of them. Try hard-coding the values in
来源:https://stackoverflow.com/questions/31735022/error-spawning-sprites-in-swift-spritekit