2014-09-23 49 views
1

我是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 
} 
  1. 有沒有一種方法,我的故事板上添加一個UIButton,比連接 GameScene,迅速與故事板? (以編程方式添加子節點 可能確實很難)
  2. 節點的CGPoint基於什麼?目前的UIView?
  3. 如何編程更改按鈕的圖像(使用 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任何很好的例子,

回答

3

首先,創建一個你想要的圖像rray按鈕有:

let arrayOfImages = [ 
    "image1.png", 
    "image2.png", 
    "image3.png", 
    "image4.png", 
    "image5.png", 
    "image6.png" 
] 

然後初始化按鍵精靈,並給它一個紋理開始:

var rollButton = SKSpriteNode() 
rollButton.texture = SKTexture(imageNamed: arrayOfImages[0]) 

然後,每次changeButtonImage()被調用時,產生一個隨機數並更改按鈕的質感:

var randomNumberBetween0And5 = Int(arc4random_uniform(6)) 
rollButton.texture = SKTexture(imageNamed: arrayOfImages[randomNumberBetween0And5]) 
1

如果你想建立一個按鈕或使用通用的開關,你應該試試這個控制我做了,利用它非常直截了當: 你剛纔初始化按鈕類型/開關你想(ColoredSprite,紋理或純文字)

let control = TWButton(normalColor: SKColor.blueColor(), highlightedColor: SKColor.redColor(), size: CGSize(width: 160, height: 80)) 

和初始化後添加一個閉合它(像addTargetForSelector上的UIButton)

control.addClosureFor(.TouchUpInside, target: self, closure: { (scene, sender) ->() in 
      scene.testProperty = "Changed Property" 
     }) 
    } 

這它!有一個在GitHub的頁面上的自述節的更多信息:https://github.com/txaidw/TWControls

但是,如果你想在一個特定的節點來實現這裏是我如何做:

internal override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    let touchPoint = touch.locationInNode(self.parent) 

    if self.containsPoint(touchPoint) { 
     self.touchLocationLast = touchPoint 
     touchDown() 
    } 
} 

internal override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    let touchPoint = touch.locationInNode(self.parent) 
    self.touchLocationLast = touchPoint 
} 


internal override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) { 
    let touch = touches.first as! UITouch 
    let touchPoint = touch.locationInNode(self.parent) 

     if let lastPoint = self.touchLocationLast where self.containsPoint(lastPoint) { 
      // Ended inside 
      touchUpInside() 
     } 
     else { 
      // Ended outside 
      touchUpOutside() 
     } 
} 
相關問題