我试图让一个行走的角色死去,但垂死的精灵在比例上与行走的精灵不同。
首先我在func didMove上创建一个SKSpriteNode(查看:SKView){并将其添加到场景中:
//create monster, add physics to detect collision with projectile and add to scene with walking animation
let monster = SKSpriteNode(imageNamed: "zombie")
monster.yScale = CGFloat(-0.58)
monster.xScale = CGFloat(0.58)
monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size)
monster.physicsBody?.isDynamic = true
monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster
monster.physicsBody?.contactTestBitMask = PhysicsCategory.Projectile
monster.physicsBody?.collisionBitMask = PhysicsCategory.None
// Determine where to spawn the monster along the Y axis
let actualY = random(min: monster.size.height/2, max: size.height - (monster.size.height/2)-20)
// Position the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)
// Add the monster to the scene
worldNode.addChild(monster)
// Determine speed of the monster
let actualDuration = random(min: CGFloat(2.0)-CGFloat(self.level/10), max: CGFloat(4.0)-CGFloat(self.level/10))
// Create the actions
let actionMove = SKAction.move(to: CGPoint(x: -monster.size.width/2, y: actualY), duration: TimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
let atlas = SKTextureAtlas(named: monsterVariant)
var m0 = SKTexture()
var m1 = SKTexture()
var m2 = SKTexture()
var m3 = SKTexture()
var m4 = SKTexture()
var m5 = SKTexture()
m0 = atlas.textureNamed("\(monsterVariant)_Walk1") //222x396
m1 = atlas.textureNamed("\(monsterVariant)_Walk2")//222x366
m2 = atlas.textureNamed("\(monsterVariant)_Walk3")//250x375
m3 = atlas.textureNamed("\(monsterVariant)_Walk4")//237×379
m4 = atlas.textureNamed("\(monsterVariant)_Walk5")//221×371
m5 = atlas.textureNamed("\(monsterVariant)_Walk6")//228×402
let textures = [m0,m1,m2,m3,m4,m5]
let walkAnimation = SKAction.animate(with:textures, timePerFrame: 0.07)
monster.run(SKAction.repeatForever(walkAnimation))
然后,当一个抛射物和一个怪物发生碰撞时,我想让死亡动画播放,怪物消失。请注意评论中的纹理尺寸:
func projectileDidCollideWithMonster(projectile: SKSpriteNode, monster: SKSpriteNode) {
let dyingMonsterPosition = monster.position
addToScoreBasedOnPosition(monster:monster)
projectile.removeFromParent()
let atlas = SKTextureAtlas(named: String(monster.name!))
print(String(monster.name!))
var m1 = SKTexture()
var m2 = SKTexture()
var m3 = SKTexture()
var m4 = SKTexture()
var m5 = SKTexture()
var m6 = SKTexture()
var m7 = SKTexture()
var m8 = SKTexture()
m1 = atlas.textureNamed("\(String(monster.name!))_Dead1")//220 × 374
m2 = atlas.textureNamed("\(String(monster.name!))_Dead2")//272 × 377
m3 = atlas.textureNamed("\(String(monster.name!))_Dead3")//349 × 355
m4 = atlas.textureNamed("\(String(monster.name!))_Dead4")//384 × 296
m5 = atlas.textureNamed("\(String(monster.name!))_Dead5")//380 × 248
m6 = atlas.textureNamed("\(String(monster.name!))_Dead6")//406 × 218
m7 = atlas.textureNamed("\(String(monster.name!))_Dead7")//465 × 211
m8 = atlas.textureNamed("\(String(monster.name!))_Dead8")//475 × 193
let textures = [m1,m2,m3,m4,m5,m6,m7,m8]
let playSound = SKAction.run {
SKAction.playSoundFileNamed("163442__under7dude__man-dying.wav", waitForCompletion: false)
}
let dieAnimation = SKAction.animate(with: textures, timePerFrame: 0.1, resize: false, restore: true)
let removeMonster = SKAction.run {
monster.removeFromParent()
}
let monsterDieSequence = SKAction.sequence([playSound,dieAnimation,removeMonster])
monster.removeAllActions()
monster.run(monsterDieSequence)
}
如果我在SKAction.animate中使用resize:true我有一个完美的动画,但我失去了原始的怪物.xScale和monster.yScale。怪物“长大成人”。
如果我使用resize:false,动画会变形:
这个问题有一个可能的答案: animate-an-skspritenode-with-textures-that-have-a-size-different-from-the-origin
但我认为让所有精灵都相同,添加透明度不是解决这个问题的唯一选择。
答案 0 :(得分:0)
您是否尝试过不使用旋转的精灵?,我的意思是,绘制人物肖像的整个死亡序列,并使用zAction方法将zRotation方法与动画中的代码一起在死亡序列中执行旋转效果,使用SKAction.group( [旋转,动画])。