gpt4 book ai didi

swift - SpriteKit 动画没有按预期加载。我究竟做错了什么?

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

我正在制作一个简单的游戏。英雄应该跳过敌人,如果他们发生碰撞 - 敌人应该吃掉英雄。我有 4 种类型的敌人随机生成在英雄面前。我使用每个敌人阵列的第一个 Sprite 作为主要 Sprite ,其余的用于动画。但是当它们碰撞时——什么也没有发生,它仍然是第一个 Sprite 。 我就是这样做的:

let mouseAtlas = SKTextureAtlas(named: "mouse") 
mouseArray.append(mouseAtlas.textureNamed("mouse_0"));
mouseArray.append(mouseAtlas.textureNamed("mouse_1"));
mouseArray.append(mouseAtlas.textureNamed("mouse_2"));
mouseArray.append(mouseAtlas.textureNamed("mouse_3"));

老鼠是敌人, cookies 是英雄。

mouse = SKSpriteNode(texture: mouseArray[0]);
self.mouse.position = CGPointMake(CGRectGetMaxX(self.frame) + self.cookie.size.width, self.cookieSpot + self.cookie.size.height + self.cookie.size.height / 2)
self.mouse.size = CGSizeMake(self.cookie.size.width + self.cookie.size.width / 2, self.cookie.size.height + cookie.size.height / 2)

self.addChild(cookie)
self.addChild(mouse)

然后我将物理体应用于它们:

self.cookie.physicsBody = SKPhysicsBody(circleOfRadius: CGFloat(self.cookie.size.width / 2))
self.cookie.physicsBody?.affectedByGravity = false
self.cookie.physicsBody?.categoryBitMask = ColliderType.Cookie.rawValue
self.cookie.physicsBody?.collisionBitMask = ColliderType.Pet.rawValue
self.cookie.physicsBody?.contactTestBitMask = ColliderType.Pet.rawValue

self.mouse.physicsBody = SKPhysicsBody(rectangleOfSize: self.mouse.size)
self.mouse.physicsBody?.dynamic = false
self.mouse.physicsBody?.categoryBitMask = ColliderType.Pet.rawValue
self.mouse.physicsBody?.contactTestBitMask = ColliderType.Cookie.rawValue
self.mouse.physicsBody?.collisionBitMask = ColliderType.Cookie.rawValue

然后我尝试在接触开始时制作动画:

func didBeginContact(contact: SKPhysicsContact) {
eatenByMouse(); eatenByHamster(); eatenByRabbit(); eatenByCat()
}

func eatenByMouse() {
self.groundSpeed = 0
self.cookie.hidden = true
let animateAction = SKAction.animateWithTextures(self.mouseArray, timePerFrame: 0.1)
}

正如我所说,有 4 种类型的敌人,但它们实际上是相同的。我做错了什么?我也很感兴趣,我可以只在一侧制作敌人的物理 body 吗?我的意思是,只有当我的英雄碰到敌人嘴巴时,怎么才能让他吃掉?如果它碰到尾部,它会继续滚动和跳跃吗?非常感谢!

最佳答案

创建后必须运行 SKAction

func eatenByMouse() {
self.groundSpeed = 0
self.cookie.hidden = true
let animateAction = SKAction.animateWithTextures(self.mouseArray, timePerFrame: 0.1)
self.mouse.runAction(animateAction) // added line.
}

您可以使用 SKPhysicsContact.contactPoint 检测碰撞点。

关于swift - SpriteKit 动画没有按预期加载。我究竟做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28984115/

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