【问题标题】:Sprite Kit how to update an image on a button?Sprite Kit 如何更新按钮上的图像?
【发布时间】: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
}
  1. 有没有办法在 Storyboard 上添加一个 UIButton 而不是连接 GameScene,用那个故事板迅速? (添加子节点 以编程方式可能真的很难)
  2. 节点的CGPoint基于什么?当前的 UIView?
  3. 如何以编程方式更改按钮的图像(快速使用 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


    【解决方案1】:

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

    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])
    

    【讨论】:

      【解决方案2】:

      如果你想构建一个通用的按钮或开关,你应该试试我制作的这个控件,它的使用非常简单: 您只需初始化所需的按钮/开关类型(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()
              }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-09-11
        • 2016-05-01
        • 1970-01-01
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多