gpt4 book ai didi

swift - SKNode 未移动到编程位置

转载 作者:行者123 更新时间:2023-11-28 07:29:09 25 4
gpt4 key购买 nike

我创建了一个运行良好的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com