三击后如何删除精灵
Posted
技术标签:
【中文标题】三击后如何删除精灵【英文标题】:how to remove the sprite after three hits on it 【发布时间】:2018-12-22 10:48:47 【问题描述】:我正在学习使用 swift 和 spritekit 制作游戏,但卡在一个地方。 在第一次碰撞后,我设法将“敌人”和“子弹”移除。 请告诉我,如何在“子弹”击中三下后将“敌人”移除。 我在网上找了很久的答案,但没有成功。
import SpriteKit
import GameplayKit
class enemiesValue: SKSpriteNode
var health: Int = 3
class bulletValue: SKSpriteNode
var damage: Int = 1
class GameScene: SKScene, SKPhysicsContactDelegate
var player: SKSpriteNode!
var touchLocation: CGPoint!
var timeSpawnEnemies: Timer!
var timeSpawnBullet: Timer!
struct PhysicsCategory
static let enemyCategory: UInt32 = 0x1 << 1
static let bulletCategory: UInt32 = 0x1 << 0
override func didMove(to view: SKView)
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
self.physicsWorld.contactDelegate = self
//playerAdd()
timeSpawnEnemies = Timer.scheduledTimer(timeInterval: 2.75, target: self, selector: #selector(enemiesAdd), userInfo: nil, repeats: true)
@objc func enemiesAdd()
let enemyNode = enemiesValue(imageNamed: "enemy")
let randomPos = GKRandomDistribution(lowestValue: -350, highestValue: 350)
let pos = CGFloat(randomPos.nextInt())
enemyNode.position = CGPoint(x: pos, y: 800)
enemyNode.size = CGSize(width: 50, height: 50)
enemyNode.yScale = 1.5
enemyNode.xScale = 1.5
//enemyNode.userData = ["health": 3]
enemyNode.physicsBody = SKPhysicsBody(rectangleOf: enemyNode.size)
enemyNode.physicsBody?.isDynamic = true
enemyNode.physicsBody?.categoryBitMask = PhysicsCategory.enemyCategory
enemyNode.physicsBody?.contactTestBitMask = PhysicsCategory.bulletCategory
enemyNode.physicsBody?.collisionBitMask = 0
self.addChild(enemyNode)
let animDuration: TimeInterval = 16
var actions = [SKAction]()
actions.append(SKAction.move(to: CGPoint(x: pos, y: -800), duration: animDuration))
actions.append(SKAction.removeFromParent())
enemyNode.run(SKAction.sequence(actions))
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
//timeSpawnBullet = Timer.scheduledTimer(timeInterval: 0.2, target: self, selector: #selector(shoot), userInfo: nil, repeats: true)
shoot()
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?)
//timeSpawnBullet.invalidate()
@objc func shoot()
let bullet = bulletValue(imageNamed: "bullet")
bullet.size = CGSize(width: 75, height: 25)
bullet.position = player.position
bullet.physicsBody = SKPhysicsBody(rectangleOf: bullet.size)
bullet.physicsBody?.isDynamic = true
bullet.physicsBody?.categoryBitMask = PhysicsCategory.bulletCategory
bullet.physicsBody?.contactTestBitMask = PhysicsCategory.enemyCategory
bullet.physicsBody?.collisionBitMask = 0
bullet.physicsBody?.usesPreciseCollisionDetection = true
self.addChild(bullet)
let animDuration: TimeInterval = 0.3
var actions = [SKAction]()
actions.append(SKAction.move(to: CGPoint(x: player.position.x, y: 800), duration: animDuration))
actions.append(SKAction.removeFromParent())
bullet.run(SKAction.sequence(actions))
func didBegin(_ contact: SKPhysicsContact)
let enemyBody: SKPhysicsBody
let bulletBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
bulletBody = contact.bodyA
enemyBody = contact.bodyB
else
bulletBody = contact.bodyB
enemyBody = contact.bodyA
if (enemyBody.categoryBitMask & PhysicsCategory.enemyCategory) != 0 && (bulletBody.categoryBitMask & PhysicsCategory.bulletCategory) != 0
collisionElementsBullets(bulletNode: bulletBody.node as! SKSpriteNode)
collisionElementsEnemies(enemyNode: enemyBody.node as! SKSpriteNode)
func collisionElementsBullets(bulletNode: SKSpriteNode)
bulletNode.removeFromParent()
func collisionElementsEnemies(enemyNode: SKSpriteNode)
enemyNode.removeFromParent()
【问题讨论】:
嗨 Denis.. 请参考您尝试找到解决方案的一些站点/位置,以及您如何实施它们(如果需要,请包括错误代码)。如果它在 stackoverlfow 之外,您可以考虑粘贴要点(您和他们之间的措辞差异)。 【参考方案1】:func collisionElementsEnemies(enemyNode: enemiesValue)
enemyNode.health = enemyNode.health - 1
if enemyNode.health == 0
enemyNode.removeFromParent()
由于您的enemiesValue
(顺便说一句,它应该大写并重命名。我称之为EnemySprite
)是SKSpriteNode
的后代,您可以将此类型作为collisionElementsEnemies
中的参数类型。然后只是减少敌人的生命值并在其生命值降至零时将其从父级中移除
你可以像这样调用这个方法:
collisionElementsEnemies(enemyNode: enemyBody.node as! enemiesValue)
【讨论】:
以上是关于三击后如何删除精灵的主要内容,如果未能解决你的问题,请参考以下文章