gpt4 book ai didi

swift - 将多个 Sprite 添加到屏幕

转载 作者:行者123 更新时间:2023-11-30 13:48:50 26 4
gpt4 key购买 nike

我是 SpriteKit 新手。我开始开发一款游戏,我想要一个可以从中选择的 Sprite 菜单。当我选择一个特定的 Sprite 时,我希望能够点击屏幕并生成它们的副本。

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

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)
}
}
}
}

最佳答案

试试这个:)

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)
}
}
}
}
}

关于swift - 将多个 Sprite 添加到屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34573526/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com