Swift 如何在三次点击后移除精灵
我正在学习在swift和spritekit上制作游戏,但还是停留在一个地方。 在第一次碰撞后,我设法把“敌人”和“子弹”移走了。 请告诉我,在一颗“子弹”击中“敌人”三次后,如何清除它。 我在网上寻找答案已经很久了,但是没有成功Swift 如何在三次点击后移除精灵,swift,sprite-kit,skspritenode,Swift,Sprite Kit,Skspritenode,我正在学习在swift和spritekit上制作游戏,但还是停留在一个地方。 在第一次碰撞后,我设法把“敌人”和“子弹”移走了。 请告诉我,在一颗“子弹”击中“敌人”三次后,如何清除它。 我在网上寻找答案已经很久了,但是没有成功 import SpriteKit import GameplayKit class enemiesValue: SKSpriteNode { var health: Int = 3 } class bulletValue: SKSpriteNode {
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()
}
}
导入SpriteKit
导入游戏工具包
类enemiesValue:SKSpriteNode{
变量运行状况:Int=3
}
类bulletValue:SKSpriteNode{
var损坏:Int=1
}
类游戏场景:SKScene,SKPhysicContactDelegate{
var玩家:斯普林泰诺德!
var touchLocation:CGPoint!
var timeSpawnEnemies:计时器!
var timeSpawnBullet:计时器!
结构物理分类{
静态let-enemyCategory:UInt32=0x1
由于您的enemiesValue
(顺便说一句,它应该大写并重命名。我将其称为EnemySprite
)是SKSpriteNode
的后代。您可以将此类型作为参数类型放入collisionElementsEnemies
中。然后降低敌人的生命值,并在其生命值降至零时将其从父级移除
您可以这样调用此方法:
collisionElementsEnemies(enemyNode: enemyBody.node as! enemiesValue)
嗨,丹尼斯。参考一些网站/地点,在那里你试图找到解决方案,你是如何植入它们的(包括错误代码,如果需要的话)。如果它的外部堆叠重叠,你可以考虑粘贴要点(你和他们之间的措辞上的差异)。
collisionElementsEnemies(enemyNode: enemyBody.node as! enemiesValue)