gpt4 book ai didi

Swift:玩家跳跃动画

转载 作者:行者123 更新时间:2023-11-30 12:30:38 27 4
gpt4 key购买 nike

我试图让我的播放器在触摸开始时播放跳跃动画。玩家位于他自己的 swift 文件中,我让 GameScene.swift 调用他的文件,因此与他的移动相关的所有内容都在player.swift 文件中。任何帮助都会很棒!

import SpriteKit




struct ColliderType {
static let PLAYER: UInt32 = 0
static let GROUND: UInt32 = 1
static let ROCKET_AND_COLLECTABLES: UInt32 = 2


}



class Player: SKSpriteNode {

private var textureAtlas = SKTextureAtlas()
private var playerAnimation = [SKTexture]()
private var animatePlayerAction = SKAction()
let jumpAnimation1 = [SKTexture]()
let jumpAnimation2 = [SKTexture]()

var jumpAnimation = SKAction ()

func initializePlayer() {
name = "Player"

for i in 1...7 {
let name = "Player \(i)"
playerAnimation.append(SKTexture(imageNamed: name))



}


animatePlayerAction = SKAction.animate(with: playerAnimation,

timePerFrame: 0.08, resize: true, restore: false)

//Animate the player forever.
self.run(SKAction.repeatForever(animatePlayerAction))


physicsBody = SKPhysicsBody(rectangleOf: CGSize(width:
self.size.width-60, height: self.size.height-80))
physicsBody?.affectedByGravity = true
physicsBody?.allowsRotation = false
//Restitution is Boucniess
physicsBody?.restitution = 0
physicsBody?.categoryBitMask = ColliderType.PLAYER
physicsBody?.collisionBitMask = ColliderType.GROUND
physicsBody?.contactTestBitMask =

ColliderType.ROCKET_AND_COLLECTABLES

}




func move (){
self.position.x += 10
}

func reversePlayer() {
physicsBody?.velocity = CGVector(dx: 0, dy: 0)
physicsBody?.applyImpulse(CGVector(dx: 0, dy: 300))

playerNode?.removeAction(forKey:"animate") playerNode?.run(attackAnimation,completion:{
self.playerNode?.run(self.animation,withKey: "animate")
}




}

最佳答案

使用以下代码片段替换设置纹理、创建并运行操作的部分。它非常适合我,并且是最简单的方法。

let hero = SKSpriteNode(imageNamed: "hero")
hero.size = CGSize(width: 45, height: 125)
let f1 = SKTexture.init(imageNamed: "h1")
let f2 = SKTexture.init(imageNamed: "h2")
let f3 = SKTexture.init(imageNamed: "h3")
let frames: [SKTexture] = [f1, f2, f3]
let hero = SKSpriteNode(imageNamed: "h1")
let animation = SKAction.animate(with: frames, timePerFrame: 0.1)
hero.run(SKAction.repeatForever(animation))

此外,您的动画 Action 可能无法访问,因为它位于另一个文件中。全局声明播放器和动画,即在任何类或函数之外。希望它对您有用!

关于Swift:玩家跳跃动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43601011/

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