SpriteKit 动画没有按预期加载。我究竟做错了什么?
SpriteKit Animation isn't loading when it supposed to be. What am I doing wrong?
我正在制作一个简单的游戏。英雄应该跳过敌人,如果他们发生碰撞 - 敌人应该吃掉英雄。我有 4 种类型的敌人随机生成在英雄面前。我使用每个 enemy-array 的第一个精灵作为主要精灵,其余的用于动画。但是当它们碰撞时——什么也没有发生,它仍然是第一个精灵。
我就是这样做的:
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"));
老鼠是敌人,饼干是英雄。
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
检测碰撞点。
我正在制作一个简单的游戏。英雄应该跳过敌人,如果他们发生碰撞 - 敌人应该吃掉英雄。我有 4 种类型的敌人随机生成在英雄面前。我使用每个 enemy-array 的第一个精灵作为主要精灵,其余的用于动画。但是当它们碰撞时——什么也没有发生,它仍然是第一个精灵。 我就是这样做的:
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"));
老鼠是敌人,饼干是英雄。
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
检测碰撞点。