【发布时间】:2018-04-17 15:46:13
【问题描述】:
我是编码新手,我在主类中创建了一个带有黑色按钮的游戏。我想创建一个设置类,用户可以单击蓝色按钮将主类中的黑色按钮更改为红色,或者单击红色按钮将主类中的黑色按钮更改为红色。
这里是主类:
class Mainclass: SKScene{
var gameButton = SKSpriteNode(imageNamed: "blackDot") //current button image
}
这里是设置类:
class Settings: SKScene {
override func didMove(to view: SKView) {
self.backgroundColor = SKColor.white
let blueButton = SKSpriteNode(imageNamed: "blueDot") //button to select
blueButton.position = CGPoint(x: self.size.width * 0.2, y: self.size.height * 0.8)
blueButton.setScale(0.53)
self.addChild(blueButton)
let redButton = SKSpriteNode(imageNamed: "redDot") //button to select
redButton.position = CGPoint(x: self.size.width * 0.4, y: self.size.height * 0.8)
redButton.setScale(0.53)
self.addChild(redButton)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
let locationUser = touch.location(in: self)
if atPoint(locationUser) == blueButton {
//Change the button image in the Mainclass from black to blue if user tap on blueButton
}
if atPoint(locationUser) == redButton {
//Change the button image in the Mainclass from black to red if user tap on redButton
}
}
}
}
【问题讨论】:
-
所以你想要两个场景,一个是实际游戏,另一个是设置屏幕?如果是这样,您可以使用用户默认值来保存设置(例如,将在游戏场景中使用哪个图像)。你可以试试,如果你还是卡住了,我可以给你举个例子,告诉我。
-
是的,我想要两个不同的屏幕。一个很好的例子@Whirlwind
标签: swift button sprite-kit imagebutton