我是Swift和Sprite Kit的新手。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
}
- 有沒有一種方法,我的故事板上添加一個UIButton,比連接 GameScene,迅速與故事板? (以編程方式添加子節點 可能確實很難)
- 節點的CGPoint基於什麼?目前的UIView?
- 如何編程更改按鈕的圖像(使用 SpriteKit節點SWIFT)與其他圖像舉例來說,如果我有圖片: dice1,dice2,dice3 ...
這是客觀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任何很好的例子,