sách gpt4 ai đã đi

swift - 如何在多次点击时删除节点

In lại 作者:行者123 更新时间:2023-11-28 06:17:30 28 4
mua khóa gpt4 Nike

我正在制作一款太空入侵者游戏,其中有许多敌舰向您驶来,您必须向它们射击。

当玩家触摸屏幕时,玩家飞船会向敌舰发射子弹。

我明白了,每当一颗子弹碰到敌舰时,它就会从父舰上移除。但是我无法得到它,因此需要 2 颗子弹才能将敌舰从母舰上移走。出于某种原因,每当另一艘敌舰被召唤到现场时,敌人的生命就会自行重置。怎样才能让每艘敌舰都有自己独立的生命,并且不影响其他敌舰的生命?

这是敌人类:

public class Villain: SKSpriteNode {

var life = 2

init(){

let texture = SKTexture(imageNamed: "Villain")
var life = 2
print("number of lives: ", life)
super.init(texture: texture, color: SKColor.clear, size: texture.size())
self.name = "villain"
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

这里是调用Enemy类的GameScene类

func VillainRight(){
let TooMuch = self.size.width
let point = UInt32(TooMuch)

let VillainR = Villain()

VillainR.zPosition = 2
VillainR.position = CGPoint(x: self.frame.minX,y: CGFloat(arc4random_uniform(point)))

//This code makes the villain's Zposition point towards the SpaceShip
let angle = atan2(SpaceShip.position.y - VillainR.position.y, SpaceShip.position.x - VillainR.position.x)
VillainR.zRotation = angle - CGFloat(M_PI_2)

let MoveToCenter = SKAction.move(to: CGPoint(x: self.frame.midX, y: self.frame.midY), duration: 15)

//Physics World
VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
VillainR.physicsBody?.categoryBitMask = NumberingPhysics.RightV
VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
VillainR.physicsBody?.affectedByGravity = false
VillainR.physicsBody?.isDynamic = true

VillainR.run(MoveToCenter)
addChild(VillainR)
}

这里是 didBeginContact 方法的一部分:

 //LASERS HIT ENEMY CHECK

if BodyOne.categoryBitMask == NumberingPhysics.Laser && BodyTwo.categoryBitMask == NumberingPhysics.LeftV{
run(VillainGone)
ToNextLevel -= 1

if BodyTwo.node != nil{
MakeExplosions(BodyTwo.node!.position)
}

BodyTwo.node?.removeFromParent()
BodyOne.node?.removeFromParent()
}

if BodyOne.categoryBitMask == NumberingPhysics.Laser && BodyTwo.categoryBitMask == NumberingPhysics.RightV{

ToNextLevel -= 1

if BodyTwo.node != nil{
MakeExplosions(BodyTwo.node!.position)
}

run(VillainGone)
BodyOne.node?.removeFromParent()
BodyTwo.node?.removeFromParent()
}
}

回顾:

我想要做的就是让敌舰在 2 颗子弹碰到它时从 Parent 中移除。并且敌方生命相互独立(如果一艘敌舰还剩下 1 条生命,那么如果另一艘敌舰被召唤到现场,它不会重置回 2)。

1 Câu trả lời

这是您问题的示例解决方案(这是一个 macOS 项目,如果您要转换,只需将 mouseDown 替换为 touchesBegan).

点击屏幕观看反派生命值耗尽,当达到 0 时反派将死亡并从场景中移除:

let category1 = UInt32(1)
let category2 = UInt32(2)


class Villain: SKSpriteNode {

var lives = 2
var hitThisFrame = false

init(color: SKColor, size: CGSize) {
super.init(texture: nil, color: color, size: size)
let pb = SKPhysicsBody(rectangleOf: self.size)
pb.categoryBitMask = category1
pb.contactTestBitMask = category2
self.physicsBody = pb
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class Hero: SKSpriteNode {

init(color: SKColor, size: CGSize) {
super.init(texture: nil, color: color, size: size)
let pb = SKPhysicsBody(rectangleOf: self.size)
pb.categoryBitMask = category2
pb.contactTestBitMask = category1
self.physicsBody = pb
}
required init?(coder aDecoder: NSCoder) { fatalError() }
}


class GameScene: SKScene, SKPhysicsContactDelegate {

let villain = Villain(color: .blue, size: CGSize(width: 50, height: 50))
let hero = Hero (color: .green, size: CGSize(width: 50, height: 50))

override func didMove(to view: SKView) {
physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector.zero

hero.position.y -= 100
addChild(villain)
addChild(hero)
}

func didBegin(_ contact: SKPhysicsContact) {
let contactedBodies = contact.bodyA.categoryBitMask + contact.bodyB.categoryBitMask

if contactedBodies == category1 + category2 {

// Find which one of our contacted nodes is a villain:
var vil: Villain
if let foundVil = contact.bodyA.node as? Villain {
vil = foundVil
} else if let foundVil = contact.bodyB.node as? Villain {
vil = foundVil
} khác {
fatalError("one of the two nodes must be a villain!!")
}


if vil.hitThisFrame {
// Ignore a second contact if already hit this frame:
trở lại
} khác {
// Damage villain:
vil.lives -= 1
print(" vil lives: \(vil.lives)")
vil.hitThisFrame = true
if vil.lives == 0 {
// Kill villain:
print("villain is dead!!!")
vil.physicsBody = nil
vil.removeFromParent()
}
}
}
}

override func didSimulatePhysics() {
// Reset hero position (so as to not trigger another didBegin()
hero.position = CGPoint(x: 0, y: -100)
// Allow villain to be hit again next frame:
villain.hitThisFrame = false
}
override func mouseDown(with event: NSEvent) {
// Trigger didBegin():
hero.position = villain.position
}
}

关于swift - 如何在多次点击时删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44731410/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com