本文介绍了如何限制SKSpriteNodes的位置(在我的情况下)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 好的,所以我正在制作一个游戏,其中的东西落在屏幕上,它随机地随机地在X值的顶部产生。有时当它们产卵时,它们会非常靠近屏幕的边缘,直到很难看到它们。所以我需要的是一些让它们产生距离边缘更远的方法。这是我的代码} * / func spawnChlorine(){ var Chlorine = SKSpriteNode(imageNamed: Chlorine.png) var minValue = self .size.width / 8 var maxValue = self .size.width - 20 var spawnPoint = UInt32 ( maxValue - minValue) Chlorine.position = CGPoint(x:CGFloat(arc4random_uniform(spawnPoint)),y: self .size.height) 让 action = SKAction.moveToY(-20,持续时间: 5 。 0 ) 让 actionDone = SKAction.removeFromParent() Chlorine.runAction(SKAction.sequence([action, actionDone])) Chlorine.runAction(SKAction.repeatActionForever(action)) Chlorine.physicsBody = SKPhysicsBody(rectangleOfSize:Chlorine.size) Chlorine.physicsBody?.categoryBitMask = PhysicsCatagory.Chlorine Chlorine.physicsBody?.contactTestBitMask = PhysicsCatagory.Shot Chlorine.physicsBody?.affectedByGravity = true Chlorine.physicsBody?.dynamic = true self .addChild(Chlorine)} 解决方案 Ok so I am making a game where things fall down the screen, its at a random where they spawn at the top on the X value randomly. sometimes when they spawn they go really close to the edge of the screen to the point where they are hard to see. So what I need is some way to make them spawn a little bit farther away from the edges. here is my code }*/ func spawnChlorine(){ var Chlorine = SKSpriteNode(imageNamed: "Chlorine.png") var minValue = self.size.width / 8 var maxValue = self.size.width - 20 var spawnPoint = UInt32(maxValue - minValue) Chlorine.position = CGPoint(x: CGFloat (arc4random_uniform (spawnPoint )) ,y: self.size.height) let action = SKAction.moveToY(-20, duration: 5.0) let actionDone = SKAction.removeFromParent() Chlorine.runAction(SKAction.sequence([action, actionDone])) Chlorine.runAction(SKAction.repeatActionForever(action)) Chlorine.physicsBody = SKPhysicsBody(rectangleOfSize: Chlorine.size) Chlorine.physicsBody?.categoryBitMask = PhysicsCatagory.Chlorine Chlorine.physicsBody?.contactTestBitMask = PhysicsCatagory.Shot Chlorine.physicsBody?.affectedByGravity = true Chlorine.physicsBody?.dynamic = true self.addChild(Chlorine) } 解决方案 这篇关于如何限制SKSpriteNodes的位置(在我的情况下)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 09:53