ios - 使用 SKAction.scale 更改 Sprite 的比例

标签 ios swift sprite-kit xcode8

尝试让 Sprite “PlayButton”的比例继续放大和缩小以提示用户触摸它。似乎无法让它工作。

这是菜单类:

import SpriteKit

class MenuScene: SKScene {

let ScalePBup = SKAction.scaleX(to: 300, y: 300, duration: 10)
let ScalePBdown = SKAction.scaleX(to: -300, y: -300, duration: 10)

//MARK: - Private Instance Variables
private let logo = GameLogo()
private let title = GameTitle()
private let playButton = PlayButton()
private let background = Background()
private let foreground = Foreground()
private let background2 = Background2()

//MARK: - Init
required init ?(coder aDecoder: NSCoder){
    super.init(coder:aDecoder)
}
override init(size: CGSize){
    super.init(size: size)
}
override func didMove(to view: SKView){
    self.setup()
}
//MARK: - Setup
private func setup() {

    self.backgroundColor = Colors.colorFromRGB(rgbvalue: Colors.background)
    self.addChild(logo)
    self.addChild(title)
    self.addChild(playButton)
    self.addChild(background)
    self.addChild(foreground)
    self.addChild(background2)        
}

// MARK: - Update
override func update(_ currentTime: TimeInterval) {}
// MARK: - Touch Handling
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch:UITouch = touches.first! as UITouch
    let touchLocation = touch.location(in: self)
    if playButton.contains(touchLocation){
        loadScene()
    }
}

// MARK: - Load Scene
private func loadScene() {
    let scene = GameScene(size: kViewSize)
    let transition = SKTransition.fade(with: SKColor.black, duration: 0.5)
    self.view?.presentScene(scene, transition: transition)
    run(SKAction.repeatForever(SKAction.sequence([
        SKAction.run(ScalePBup, onChildWithName: "playButton"),
        SKAction.run(ScalePBdown, onChildWithName: "playButton")
        ])
    ))
}
}

这是 Sprite

import SpriteKit

class PlayButton: SKSpriteNode {

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

override init(texture: SKTexture?, color: UIColor, size: CGSize) {
    super.init(texture: texture, color: color, size: size)
}

convenience init() {
    let texture = GameTextures.sharedInstance.texture(name: SpriteName.playButton)
    self.init(texture: texture, color: SKColor.white, size: texture.size())

    setup()
}

private func setup() {
    self.position = CGPoint(x: kViewSize.width / 2, y: kViewSize.height * 0.2)
}

}

如果您有任何建议,请告诉我!谢谢!

最佳答案

MenuScene中删除代码:

run(SKAction.repeatForever(SKAction.sequence([
        SKAction.run(ScalePBup, onChildWithName: "playButton"),
        SKAction.run(ScalePBdown, onChildWithName: "playButton")
        ])
    ))

类 PlayButton 中添加:

let ScalePBup = SKAction.scaleX(to: 300, y: 300, duration: 10)
let ScalePBdown = SKAction.scaleX(to: -300, y: -300, duration: 10)

func startPulse() {
    run(SKAction.repeatForever(SKAction.sequence([ScalePBup,ScalePBdown])))
}

func removePulse () {
    removeAllAction()
}

菜单场景

override func didMove(to view: SKView){
    self.setup()
    playButton.startPulse()
}

关于ios - 使用 SKAction.scale 更改 Sprite 的比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39971460/

相关文章:

ios - Xcode UI 测试 : staticText's label not updating as text is updated?

ios - 在选项卡 Controller 中的两个 View Controller 之间传递数据

swift - 接触物理学在 SpriteKit 中不适用于 Swift 中的平台游戏

swift - 在 Xcode 中播放声音,同时从应用程序播放音乐

ios7 - SKLabelNode错误,仅iOS7崩溃

ios - Xcode 6 App Store 提交失败,返回 "Your account already has a valid iOS distribution certificate"

ios - contentOffset 跳过日志中的值

javascript - 完美的触摸就绪、iPad 兼容和内容滑动就绪网站的框架

ios - Swift 中 'k' 和 'v' 之间的枚举值有什么区别?

ios - 用小方 block 填充 View