Sprite Kit如何更新按钮上的图像?

时间:2014-09-23 11:56:22

标签: swift storyboard sprite-kit xcode-storyboard

我是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
}
  1. 有没有办法在Storyboard上添加UIButton而不是连接 GameScene,那个故事板很快? (添加子节点 编程可能真的很难)
  2. 节点的CGPoint基于什么?目前的UIView?
  3. 如何以编程方式更改按钮的图像(快速使用 SpriteKit Node)与其他图像有关,例如我有图像: 骰子1,骰子2,骰子3 ......
  4. 这是目标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,

2 个答案:

答案 0 :(得分:3)

首先,创建一个希望按钮具有的图像数组:

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 :(得分:1)

如果你想构建一个按钮或开关用于通用,你应该尝试我做的这个控件,使用它很直接: 您只需初始化所需的按钮/开关类型(ColoredSprite,Textured或TextOnly)

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

初始化后,为它添加一个闭包(就像UIButton上的addTargetForSelector)

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