swift - SKNode 未移动到编程位置

标签 swift xcode sprite-kit sknode

我创建了一个运行良好的 SKNode 类。但是,我无法在实例化后更改其 .position。

按钮确实出现并正常工作,但在编程时不会改变位置。我已经包含了为下面的按钮制作的类,我还添加了实例化该类并调用节点位置的代码。

类代码:

class LButton: SKNode {
    var button: SKSpriteNode
    private var mask: SKSpriteNode
    private var cropNode: SKCropNode
    private var action: () -> Void
    var isEnabled = true
    
   
    
    init(imageNamed: String, buttonAction: @escaping () -> Void) {
        button = SKSpriteNode(imageNamed: imageNamed)
        button.size = CGSize(width: 50.0, height: 50.0*(24/40))
        
        mask = SKSpriteNode(color: SKColor.black, size: button.size)
        mask.alpha = 0
        
        cropNode = SKCropNode()
        cropNode.maskNode = button
        cropNode.zPosition = 3
        cropNode.addChild(mask)
        
        action = buttonAction
        
        super.init()
        
        isUserInteractionEnabled = true
        
        setupNodes()
        addNodes()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupNodes() {
        button.zPosition = 0
    }
    
    func addNodes() {
        addChild(button)
        addChild(cropNode)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if isEnabled {
            mask.alpha = 0.5
            run(SKAction.scale(by: 1.05, duration: 0.05))
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if isEnabled {
            for touch in touches {
                let location: CGPoint = touch.location(in: self)
                
                if button.contains(location){
                    mask.alpha = 0.5
                    
                } else {
                    mask.alpha = 0.0
                }
            }
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if isEnabled {
            for touch in touches {
                let location: CGPoint = touch.location(in: self)
                
                if button.contains(location) {
                    disable()
                    action()
                    run(SKAction.sequence([SKAction.wait(forDuration: 0.2), SKAction.run({self.enable()})]))
                }
            }
        }
    }
    
    
    func disable() {
        isEnabled = false
        mask.alpha = 0.0
        button.alpha = 0.5
    }
    
    func enable() {
        isEnabled = true
        mask.alpha = 0.0
        button.alpha = 1.0
    }
    
}

实例化代码:

var playButtonTwo: LButton {
        let revealGameScene = SKTransition.fade(withDuration: 0.5)
        let goToGameScene = GameSceneTwo(size: self.size)
        goToGameScene.scaleMode = SKSceneScaleMode.resizeFill
        
        var button = LButton(imageNamed: "playButton", buttonAction: {        self.view?.presentScene(goToGameScene, transition:revealGameScene)
        })
        
        button.zPosition = 5
        button.position = CGPoint(x: -self.frame.width*0.3, y: self.frame.height*0.4)
        return button
    }

调用改变节点位置:

playButtonTwo.position = CGPoint(x:0, y: -self.frame.height*0.4)

感谢任何建议!

最佳答案

我认为您不应该在创建按钮时设置按钮的位置。而只是将它放在 didMove 中

var playButtonTwo: LButton {
    let revealGameScene = SKTransition.fade(withDuration: 0.5)
    let goToGameScene = GameSceneTwo(size: self.size)
    goToGameScene.scaleMode = SKSceneScaleMode.resizeFill

    var button = LButton(imageNamed: "playButton", buttonAction: {        self.view?.presentScene(goToGameScene, transition:revealGameScene)
    })

    button.zPosition = 5
    // button.position = CGPoint(x: -self.frame.width*0.3, y: self.frame.height*0.4) <- comment out his line 
    return button
}

关于swift - SKNode 未移动到编程位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55549382/

相关文章:

ios - 如何设置 SKCameraNode 相对于节点的 x 和 y 位置?

objective-c - Unicode 格式化编译器警告 : Format specifies type 'unsigned short' but the argument has type 'int'

swift - 多个 Sprite 节点,仅出现一个

ios - 在 Swift 中带有变量的 NSLocalizedString

swift - 调用特定的表部分和行来进行备用转场? (以编程方式)

ios - 如何访问特定目录中的所有图像?

objective-c - 剪辑 View 的框架在 Xcode 5 和 Objective-C 中的运行时错误会有所不同

ios - 使用 React 库时 xcode 中没有这样的文件或目录

swift - SpriteKit : unwanted stretching of sprite when resizing/scaling

sprite-kit - Spritekit 如何测量冲击力?