0
SWiFT SpriteKitの使用 'buttonLengthFirst'と 'buttonPressedRightFirst'が押されたときに、 'createLeftButtonFirst'と 'createLeftButtonFirst'ボタンを画面から削除します。あなたはこれで私を助けることができますか?ここでSWIFTボタンをクリックするとUIButtonが削除されます
は、コードは次のとおりです。
func createLeftButton() {
let button = UIButton();
button.frame = CGRectMake(self.frame.width - self.frame.width, 0, self.frame.width/2, self.frame.height) // X, Y, width, height
button.backgroundColor = UIColor.greenColor()
button.addTarget(self, action: "buttonPressedRight:", forControlEvents: .TouchUpInside)
self.view!.addSubview(button)
button.layer.zPosition = 0
}
func createRightButton() {
let buttonRight = UIButton();
buttonRight.frame = CGRectMake(self.frame.width/2, 0, self.frame.width/2, self.frame.height) // X, Y, width, height
buttonRight.backgroundColor = UIColor.blueColor()
buttonRight.addTarget(self, action: "buttonPressedLeft:", forControlEvents: .TouchUpInside)
self.view!.addSubview(buttonRight)
buttonRight.layer.zPosition = 0
}
func createLeftButtonFirst() {
let buttonFirst = UIButton();
buttonFirst.frame = CGRectMake(self.frame.width - self.frame.width, 0, self.frame.width/2, self.frame.height) // X, Y, width, height
buttonFirst.backgroundColor = UIColor.grayColor()
buttonFirst.addTarget(self, action: "buttonPressedRightFirst:", forControlEvents: .TouchUpInside)
self.view!.addSubview(buttonFirst)
buttonFirst.layer.zPosition = 1
}
func createRightButtonFirst() {
let buttonRightFirst = UIButton();
buttonRightFirst.frame = CGRectMake(self.frame.width/2, 0, self.frame.width/2, self.frame.height) // X, Y, width, height
buttonRightFirst.backgroundColor = UIColor.blackColor()
buttonRightFirst.addTarget(self, action: "buttonPressedLeftFirst:", forControlEvents: .TouchUpInside)
self.view!.addSubview(buttonRightFirst)
buttonRightFirst.layer.zPosition = 1
}
func buttonPressedRight(sender: UIButton!) {
if buttonPressed == 0 {
player.physicsBody?.applyImpulse(CGVector(dx: -15, dy: 0))
buttonPressed = 1
}
}
func buttonPressedLeft(sender: UIButton!) {
if buttonPressed == 1 {
player.physicsBody?.applyImpulse(CGVector(dx: 15, dy: 0))
buttonPressed = 0
}
}
/* First Buttons*/
func buttonPressedRightFirst(sender: UIButton!){
player.physicsBody?.applyImpulse(CGVector(dx: -15, dy: 0))
buttonPressed = 1
}
func buttonPressedLeftFirst(sender: UIButton!){
player.physicsBody?.applyImpulse(CGVector(dx: 15, dy: 0))
buttonPressed = 0
self.removeFromParent()
}
をところで、私はボタンが「作成」という機能の外で定義されるべきだと思いますそれら。関数の外側で 'weak var leftButton:UIButton!'を実行してから、 'leftButton = UIButton()'などに移動します。 – dbmrq
ありがとう!ボタンをタッチしたとき何も起こらない、動作するようには思えカント機能のように思える: /*最初のボタン*/ buttonPressedRightFirst FUNC(送信者:!UIButton)?{ player.physicsBody .applyImpulse(CGVector(DX:-15 、DY:0)) のbuttonPressed = 1 } FUNC buttonPressedLeftFirst(送信者:UIButton){ player.physicsBody .applyImpulse(CGVector(DX:15、DY:0)) のbuttonPressed = 0 self.removeFromParent() } – rkoi95
ウェル私はSpriteKitについて何も知らないので、私の答えは間違っている可能性があります。あなたのコードは以前に働いていましたか?あなたが投稿したコードの 'buttonPressed'関数の中で' sender.hidden = true'をやってみることもできると思います。 – dbmrq