我正在使用 Xcode,SpriteKit和 Swift构建我的第一款iPhone游戏.我是这些技术的新手,但我熟悉一般的编程概念. 这是我想用英语做的事情.我希望圈子随机出现在屏幕上,然后开始扩大尺寸.但是
这是我想用英语做的事情.我希望圈子随机出现在屏幕上,然后开始扩大尺寸.但是,我不希望圆圈出现在当前存在圆圈的位置.我无法确定每个圆圈的位置.
在GameScene.swift里面我在didMoveToView中有以下代码:
runAction(SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock(addCircle), SKAction.waitForDuration(3, withRange: 2)]
)))
上面的代码调用我的“addCircle”方法:
func addCircle() {
// Create sprite.
let circle = SKSpriteNode(imageNamed: "grad640x640_circle")
circle.name = "circle"
circle.xScale = 0.1
circle.yScale = 0.1
// Determine where to position the circle.
let posX = random(min: 50, max: 270)
let posY = random(min: 50, max: 518)
// ***Check to see if position is currently occupied by another circle here.
circle.position = CGPoint(x: posX, y: posY)
// Add circle to the scene.
addChild(circle)
// Expand the circle.
let expand = SKAction.scaleBy(2, duration: 0.5)
circle.runAction(expand)
}
上面的随机函数只选择给定范围内的随机数.如何查看我的随机函数是否正在生成当前被另一个圆占据的位置?
我正在考虑使用do..while循环来随机生成一组x和y坐标,然后检查一个圆是否在该位置,但我找不到如何检查该条件.任何帮助将不胜感激.
在这方面,有几种方法可以帮助您:>(BOOL)intersectsNode:(SKNode *)节点,可与SKNode一起使用,和
> CGRectContainsPoint()以及CGRectContainsRect()方法.
例如,检查交集的循环可以如下所示:
var point: CGPoint
var exit:Bool = false
while (!exit) {
let posX = random(min: 50, max: 270)
let posY = random(min: 50, max: 518)
point = CGPoint(x: posX, y: posY)
var pointFound: Bool = true
self.enumerateChildNodesWithName("circle", usingBlock: {
node, stop in
let sprite:SKSpriteNode = node as SKSpriteNode
if (CGRectContainsPoint(sprite.frame, point))
{
pointFound = false
stop.memory = true
}
})
if (pointFound)
{
exit = true
}
}
//point contains CGPoint where no other circle exists
//Declare new circle at point
