如何解决如何通过在 Swift 5. (OOP) 中按下按钮来为图像设置动画
上次你帮了我很多,现在我再次需要你的帮助。 我的问题是在点击按钮时为图像设置动画。为了检测,当点击按钮时,我做了一个带有布尔值的结构。但是当我点击按钮时,图像不会改变/动画。有人可以帮助我吗? 谢谢:)
我的游戏场景:
import SpriteKit
import UIKit
struct Animation {
static var pressed = false
}
class GameScene: SKScene {
var auswahl : CarMenu!
var spieler: Hero!
var rechts = SKSpriteNode(imageNamed: "rechts")
var links = SKSpriteNode(imageNamed: "links")
var jumpPic = SKSpriteNode(imageNamed: "jump")
var shootPic = SKSpriteNode(imageNamed: "shoot")
var isTouchedRight: Bool = false
var isTouchedLeft: Bool = false
var isJumping: Bool = false
override func didMove(to view: SKView) {
// soldat3()
gehLinks()
gehRechts()
jumpButton()
shootButton()
self.spieler = Hero()
self.auswahl = CarMenu()
print("\(Variables.counter)")
spieler.position = CGPoint(x: self.size.width / 2,y: self.size.height / 2)
addChild(spieler)
}
func gehRechts() {
rechts.size = CGSize(width: 80,height: 50)
rechts.position = CGPoint(x: self.size.width / 1.3,y: self.size.height / 2.4)
rechts.alpha = 0.20
self.addChild(rechts)
}
func gehLinks() {
links.size = CGSize(width: 80,height: 50)
links.position = CGPoint(x: self.size.width / 7,y: self.size.height / 2.4)
links.alpha = 0.20
self.addChild(links)
}
func jumpButton() {
jumpPic.size = CGSize(width: 80,height: 50)
jumpPic.position = CGPoint(x: self.size.width / 7,y: self.size.height / 2.1)
jumpPic.alpha = 0.20
self.addChild(jumpPic)
}
func shootButton() {
shootPic.size = CGSize(width: 80,height: 50)
shootPic.position = CGPoint(x: self.size.width / 1.12,y: self.size.height / 2.4)
shootPic.alpha = 0.20
self.addChild(shootPic)
}
// func soldat3() {
// if auswahl.counter == "Auto1" {
// print("\(auswahl)")
// } else {
// print("nichts gefunden")
// }
// }
override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
let touch = touches.first
let touchlocation = touch!.location(in: self)
if rechts.contains(touchlocation) {
isTouchedRight = true
isTouchedLeft = false
Animation.pressed = true
} else if links.contains(touchlocation) {
isTouchedLeft = true
isTouchedRight = false
Animation.pressed = true
} else if spieler.action(forKey: "jump") == nil && jumpPic.contains(touchlocation) {
isTouchedLeft = false
isTouchedRight = false
isJumping = true
}
}
override func touchesEnded(_ touches: Set<UITouch>,with event: UIEvent?) {
let touch = touches.first
let touchlocation = touch!.location(in: self)
if rechts.contains(touchlocation) {
isTouchedRight = false
isTouchedLeft = false
isJumping = false
Animation.pressed = false
} else if links.contains(touchlocation) {
isTouchedLeft = false
isTouchedRight = false
isJumping = false
Animation.pressed = false
} else if jumpPic.contains(touchlocation) {
isTouchedLeft = false
isTouchedRight = false
isJumping = false
}
}
override func update(_ currentTime: TimeInterval) {
if isTouchedRight == true {
spieler.position = CGPoint(x: spieler.position.x + 2,y: spieler.position.y)
} else if isTouchedLeft == true {
spieler.position = CGPoint(x: spieler.position.x - 2,y: spieler.position.y)
} else if isJumping == true {
let jumpup = SKAction.moveBy(x: 0,y: 50,duration: 0.4)
let fallBack = SKAction.moveBy(x: 0,y: -50,duration: 0.4)
spieler.run(SKAction.sequence([jumpup,fallBack]),withKey: "jump" )
}
}
}
================================================ ==================================================== /p>
My HeroClass
import Foundation
import SpriteKit
class Hero: SKSpriteNode {
var Spieler: CarMenu!
var player : SKSpriteNode
init() {
self.player = SKSpriteNode(imageNamed: "1")
super.init(texture: nil,color: UIColor.clear,size: CGSize(width: 32,height: 44))
// print("\(Spieler.counter)")
player = SKSpriteNode(imageNamed: "1")
player.size = CGSize(width: 100,height: 200)
player.position = CGPoint(x: self.size.width / 2,y: self.size.height / 2)
player.setScale(0.4)
animationPlayer()
Soldat2()
addChild(player)
}
func shoot() {
let bullet = SKSpriteNode(imageNamed: "bullet")
bullet.position.x = player.position.x + 20
bullet.position.y = player.position.y + 30
self.addChild(bullet)
let move = SKAction.moveBy(x: self.size.height + bullet.frame.size.height,y: 0,duration: 2.0)
let delete = SKAction.removeFromParent()
bullet.run(SKAction.sequence([move,delete]))
}
func moveRight() {
let moveRechts = SKAction.moveto(x: player.position.x + 20,duration: 2.0)
player.run(moveRechts)
}
func moveLeft() {
let moveLinks = SKAction.moveto(x: player.position.x - 20,duration: 2.0)
player.run(moveLinks)
}
func Soldat2() {
if Variables.counter == "Auto2" {
print("Klappt")
self.player = SKSpriteNode(imageNamed: "Soldat2")
player.size = CGSize(width: 70,height: 120)
} else if Variables.counter == "Auto1"{
print("normal")
self.player = SKSpriteNode(imageNamed: "1")
player.size = CGSize(width: 70,height: 120)
}
}
//这里是重点:
func animationPlayer() {
while Animation.pressed == true {
print("Tapped")
player = SKSpriteNode(imageNamed: "1")
var playerArray : [SKTexture] = []
for index in 1...6 {
print("Klappt")
playerArray.append(SKTexture(imageNamed: "\(index)"))
print("klappt2")
addChild(player)
}
player.run(SKAction.repeatForever(SKAction.animate(with: playerArray,timePerFrame: 0.8)))
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。