簡體   English   中英

如何創建一個精靈,然后通過Swift在SpriteKit中刪除特定的精靈?

[英]How to create a sprites then remove a specific sprite in SpriteKit with Swift by touch?

我想創建一堆精靈,並在觸摸它們時一次將其刪除。 到目前為止,發生的事情是,當我添加代碼時,最后一個精靈被刪除,而不是我觸摸的精靈。

var sprite = SKSpriteNode?()
var touchLocation : CGPoint?


for touch in touches {

        let location = touch.locationInNode(self)                  
        touchLocation = location
        addASprite()        
    }
    removeSprite()

}

func addASprite(){
        sprite = SKSpriteNode(color: UIColor.orangeColor(), size: CGSize(width: 100, height: 100))

        sprite!.position = touchLocation!
        self.addChild(sprite!)

}

func removeSprite(){


        if ((sprite?.containsPoint(touchLocation!) != nil)){

            sprite?.removeFromParent()

        }

}

刪除觸摸的節點:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch in touches {
        let location = touch.locationInNode(self)
        let touchedNode = nodeAtPoint(location)
        touchedNode.removeFromParent()
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM