将多个精灵添加到屏幕

时间:2016-01-03 05:09:04

标签: swift sprite-kit

我是SpriteKit的新手。 我开始研究一个游戏,我想要一个可以选择的精灵菜单。当我选择一个特定的精灵时,我想能够点击屏幕并生成它们的副本。

这是我尝试过的代码的副本。

class GameScene: SKScene 
{
    override func didMoveToView(view: SKView) 
    {
        let button1  = SKSpriteNode(imageNamed: "Monster1A.png")

        button1.position = CGPointMake(500, 600)
        button1.name = "Monster1A"

        self.addChild(button1)

        let button2  = SKSpriteNode(imageNamed: "Monster2A.png")

        button2.position = CGPointMake(300, 600)
        button2.name = "Monster2A"

        self.addChild(button2)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)      
    {
        let touch = touches
        let location = touch.first!.locationInNode(self)
        let node = self.nodeAtPoint(location)


        if (node.name == "Monster1A") 
        {
            for touch in touches 
            {
                let touchLocation = touch.locationInNode(self)
                let sprite = SKSpriteNode(imageNamed:"Monster1A")

                sprite.xScale = 0.5
                sprite.yScale = 0.5
                sprite.position = touchLocation

                let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

                sprite.runAction(SKAction.repeatActionForever(action))
                self.addChild(sprite)
            }
        }
        if (node.name == "Monster2A") 
        {
            for touch in touches 
            {
                let touchLocation = touch.locationInNode(self)
                let sprite = SKSpriteNode(imageNamed:"Monster2A")

                sprite.xScale = 0.5
                sprite.yScale = 0.5
                sprite.position = touchLocation

                let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:1)

                sprite.runAction(SKAction.repeatActionForever(action))
                self.addChild(sprite)
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个:)

import SpriteKit

class GameScene: SKScene {

var isDuplicating: Bool = false
var selectedSpriteName: String!

override func didMoveToView(view: SKView) {
    let button1  = SKSpriteNode(imageNamed:"Monster1A")

    button1.position = CGPointMake(500, 600)
    button1.name = "Monster1A"

    self.addChild(button1)

    let button2  = SKSpriteNode(imageNamed:"Monster2A")

    button2.position = CGPointMake(300, 600)
    button2.name = "Monster2A"

    self.addChild(button2)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
   /* Called when a touch begins */

    for touch in touches {

        let location = touch.locationInNode(self)
        let sprite = self.nodeAtPoint(location)

        if sprite.name == "Monster1A" {

            if self.isDuplicating == true {

                // Deselect
                self.isDuplicating = false
                return
            }

            self.selectedSpriteName = "Monster1A"
            self.isDuplicating = true

        } else if sprite.name == "Monster2A" {

            if self.isDuplicating == true {

                // Deselect
                self.isDuplicating = false
                return
            }

            self.selectedSpriteName = "Monster2A"
            self.isDuplicating = true
        } else {

            if self.isDuplicating == true {
                let newsprite = SKSpriteNode(imageNamed: self.selectedSpriteName)
                newsprite.position = location
                addChild(newsprite)
            }
        }
    }
}
}