我一直绞尽脑汁,在这里搜索并试图找出如何在屏幕上生成随机位置以产生一个圆圈。我希望有人可以帮助我,因为我完全难过。基本上,我试图创建一个在用户触摸时始终在屏幕上随机出现的形状。
override func touchesBegan(touches: Set<NSObject>, 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)
}
如果你们都需要更多我的代码,那就再也没有了。但是,谢谢你给予的任何帮助! (重申一下,这段代码很有用......但是大多数产生的球似乎都会在屏幕外产生)
答案 0 :(得分:2)
问题是你的场景比屏幕边界大
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)
答案 1 :(得分:1)
首先:确定有效的区域。它可能不是超级视图的框架,因为也许球(让我们称之为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>) -> 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坐标的值。
答案 2 :(得分:0)
给出以下随机生成器:
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
,以及打开助理编辑器以便您可以看到动画。)