【发布时间】:2014-09-23 11:56:22
【问题描述】:
我是 Swift 和 Sprite Kit 的新手。
我想在 MyScene.swift 中创建一个具有特定图像的按钮。每次单击按钮时,我都想用新图像更改按钮图像。 我该怎么做?
GameScene.swift
class GameScene: SKScene {
var rollButton:SKSpriteNode = SKSpriteNode(imageNamed: "dice1.png")
override func didMoveToView(view: SKView) {
rollButton.position = CGPoint(x: 79, y: 290) //this position is based to what? Is there an easier way to put a button using storyborard
rollButton.setScale(0.5) //what does setScale mean?
self.addChild(rollButton)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
if CGRectContainsPoint(rollButton.frame, touch.locationInNode(self)) {
changeButtonImage()
}
}
}
func changeButtonImage() {
//radom get value beteween 1 - 6
change the rollButton image with a new value??? //how to do that
}
- 有没有办法在 Storyboard 上添加一个 UIButton 而不是连接 GameScene,用那个故事板迅速? (添加子节点 以编程方式可能真的很难)
- 节点的CGPoint基于什么?当前的 UIView?
- 如何以编程方式更改按钮的图像(快速使用 SpriteKit 节点)与其他图像,例如,如果我有图像: 骰子1,骰子2,骰子3...
这是目标 C 代码:
NSString *fileName = [NSString stringWithFormat:@"dice%d.png", num];
// Setting the uiimageview to the appropriate image
self.rollButton.image = [UIImage imageNamed:fileName];
我没有找到任何关于如何从 storyborard 设置按钮以及使用 uiviewcontrollers 到创建的每个新 GameScene 的好的示例,
【问题讨论】:
标签: swift storyboard sprite-kit xcode-storyboard