在屏幕上的随机点中产生一个圆圈

我一直绞尽脑汁,在这里搜索并试图找出如何在屏幕上生成一个随机位置来产生一个圆圈。 我希望有人可以帮助我,因为我完全难过了。 基本上,我正在尝试创建一个在用户触摸时总是在屏幕上随机出现的形状。

override func touchesBegan(touches: Set, withEvent event: UIEvent) { let screenSize: CGRect = UIScreen.mainScreen().bounds let screenHeight = screenSize.height let screenWidth = screenSize.width let currentBall = SKShapeNode(circleOfRadius: 100) currentBall.position = CGPointMake(CGFloat(arc4random_uniform(UInt32(Float(screenWidth)))), CGFloat(arc4random_uniform(UInt32(Float(screenHeight))))) self.removeAllChildren() self.addChild(currentBall) } 

如果你们都需要更多我的代码,那就没有了。 但是,谢谢你给予的任何帮助! (重申一下,这段代码很有用……但是大多数产生的球似乎在屏幕外产生)

问题在于你的场景比屏幕边界大

 let viewMidX = view!.bounds.midX let viewMidY = view!.bounds.midY print(viewMidX) print(viewMidY) let sceneHeight = view!.scene!.frame.height let sceneWidth = view!.scene!.frame.width print(sceneWidth) print(sceneHeight) let currentBall = SKShapeNode(circleOfRadius: 100) currentBall.fillColor = UIColor.greenColor() let xPosition = view!.scene!.frame.midX - viewMidX + CGFloat(arc4random_uniform(UInt32(viewMidX*2))) let yPosition = view!.scene!.frame.midY - viewMidY + CGFloat(arc4random_uniform(UInt32(viewMidY*2))) print(xPosition) print(yPosition) currentBall.position = CGPointMake(xPosition,yPosition) view?.scene?.addChild(currentBall) self.removeAllChildren() self.addChild(currentBall) 

第一:确定有效的区域。 它可能不是ballView视图的框架,因为也许球(我们称之为ballView )可能会被切断。 该区域可能是( 伪代码 ):

 CGSize( Width of the superview - width of ballView , Height of the superview - height of ballView) 

获得该大小的视图后,只需将其放在屏幕上, origin为0,0。

第二:现在你有一系列有效的坐标。 只需使用随机函数(如您正在使用的函数)来选择其中一个。

使用以下内容创建一个swift文件:

 extension Int { static func random(range: Range) -> Int { var offset = 0 if range.startIndex < 0 // allow negative ranges { offset = abs(range.startIndex) } let mini = UInt32(range.startIndex + offset) let maxi = UInt32(range.endIndex + offset) return Int(mini + arc4random_uniform(maxi - mini)) - offset } } 

现在您可以指定一个随机数,如下所示:

 Int.random(1...1000) //generate a random number integer somewhere from 1 to 1000. 

您现在可以使用此函数生成x和y坐标的值。

鉴于以下随机生成器:

 public extension CGFloat { public static var random: CGFloat { return CGFloat(arc4random()) / CGFloat(UInt32.max) } public static func random(between x: CGFloat, and y: CGFloat) -> CGFloat { let (start, end) = x < y ? (x, y) : (y, x) return start + CGFloat.random * (end - start) } } public extension CGRect { public var randomPoint: CGPoint { var point = CGPoint() point.x = CGFloat.random(between: origin.x, and: origin.x + width) point.y = CGFloat.random(between: origin.y, and: origin.y + height) return point } } 

您可以将以下内容粘贴到游乐场中:

 import XCPlayground import SpriteKit let view = SKView(frame: CGRect(x: 0, y: 0, width: 500, height: 500)) XCPShowView("game", view) let scene = SKScene(size: view.frame.size) view.presentScene(scene) let wait = SKAction.waitForDuration(0.5) let popIn = SKAction.scaleTo(1, duration: 0.25) let popOut = SKAction.scaleTo(0, duration: 0.25) let remove = SKAction.removeFromParent() let popInAndOut = SKAction.sequence([popIn, wait, popOut, remove]) let addBall = SKAction.runBlock { [unowned scene] in let ballRadius: CGFloat = 25 let ball = SKShapeNode(circleOfRadius: ballRadius) var popInArea = scene.frame popInArea.inset(dx: ballRadius, dy: ballRadius) ball.position = popInArea.randomPoint ball.xScale = 0 ball.yScale = 0 ball.runAction(popInAndOut) scene.addChild(ball) } scene.runAction(SKAction.repeatActionForever(SKAction.sequence([addBall, wait]))) 

(只要确保也粘贴在随机生成器中,或者将它们复制到游乐场的Sources ,以及打开助理编辑器以便您可以看到动画。)