SpriteKit-在随机位置创建而不重叠 [英] SpriteKit - Create at random position without overlapping

查看:77
本文介绍了SpriteKit-在随机位置创建而不重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在随机位置创建一些精灵而不重叠,这是我的代码:

I want to create some sprites at random positions without getting overlapped, here is my code :

var sprites = [SKSpriteNode]()

 for index in 0...spriteArray { 

                let sprite = SKSpriteNode(imageNamed: named)
                sprites.append(sprite)

                checkInterception(sprite, sprite2: sprites)// positioning using a function

                addChild(sprite)
            }

我在这里检查是否重叠:

Here I check for overlapping:

func checkInterception(sprite1: SKSpriteNode, sprite2: [SKSpriteNode]) {

        let xPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxX
        let yPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxY
        sprite1.position = CGPoint(x: xPos, y: yPos )

        for index in 0...sprite2.count-1 {

            if sprite1.intersectsNode(sprite2[index]) {

                let yPos = sprite1.position.y + sprite1.size.height
                sprite1.position = CGPoint(x: xPos, y: yPos )
            }
        }
    }

但是一些精灵仍然重叠.我知道for循环有些不正确,但无法弄清楚.

But some sprites still get overlapped. I know there is something not right with the for loop, but just can't figure it out.

推荐答案

我敢肯定,有几种不同的方法可以解决该问题,而这与您已经写过的最接近.

I am sure there are a few different ways to attack the problem and this would be the closest to what you already wrote.

let sprites = [SKSpriteNode]() //loaded with your sprites to spawn
let maxX  = size.width //whatever your max is
let maxY  = size.height //whatever your max is

var spritesAdded = [SKSpriteNode]()

for currentSprite in sprites{

    addChild(currentSprite)

    var intersects = true

    while (intersects){

        let xPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxX
        let yPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxY

        currentSprite.position = CGPoint(x: xPos, y: yPos )

        intersects = false

        for sprite in spritesAdded{
            if (currentSprite.intersectsNode(sprite)){
                intersects = true
                break
            }
        }
    }

    spritesAdded.append(currentSprite)
}

要考虑的事情是,如果您不知道在何处可以安全地添加sprite,则可能会带来性能风险.例如,添加您的最后一个精灵可能要经过1,000,000次尝试,然后才能随机选择一个与其他人不相交的点.

Things to consider is if you don't already know where it is safe to add a sprite you run the risk of performance. For instance adding your last sprite may take 1,000,000 attempts before it randomly picks a point that doesn't intersect with others.

如果您不必一次全部添加它们,我会在更新循环中解决该问题,并执行类似的操作...

If you don't have to have them all added at once I would attack the problem on the update loop and do something like this...

var sprites = [SKSpriteNode]() //loaded with your sprites to spawn
var maxX : CGFloat = 0.0 //whatever your max is
var maxY : CGFloat = 0.0 //whatever your max is

var spritesAdded = [SKSpriteNode]()

override func didMoveToView(view: SKView) {
    maxX  = size.width //whatever your max is
    maxY  = size.height //whatever your max is
}

func addSprite(){

    if let currentSprite = sprites.first {

        let xPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxX
        let yPos = CGFloat( Float(arc4random()) / Float(UINT32_MAX)) * maxY

        currentSprite.position = CGPoint(x: xPos, y: yPos )

        for sprite in spritesAdded{
            if (currentSprite.intersectsNode(sprite)){
                return
            }
        }

        addChild(currentSprite)
        spritesAdded.append(currentSprite)
        sprites.removeFirst()
    }
}

override func update(currentTime: NSTimeInterval) {
    addSprite()
}

有点粗糙,但是想法是每次更新都会尝试添加一个sprite.这样,如果您用完了空间,它将不会锁定.希望有帮助.

Kind of rough but the idea is every update it will try to add a sprite. This way if you run out of room it won't lock up. Hopefully that helps.

这篇关于SpriteKit-在随机位置创建而不重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆