gpt4 book ai didi

ios - 雪碧包 : how to make newly spawned enemy ships shoot bullets at player?

转载 作者:行者123 更新时间:2023-11-30 12:55:24 25 4
gpt4 key购买 nike

我需要帮助想出一些方法来让新生成的敌方舰艇都发射子弹。问题是我实际上不知道该怎么做。我尝试使用可变计时器,但是它只能射出一条直线,而且我无法真正控制它产生的位置。

Game Picture: There are more enemies spawning我尝试过的:

var EnemyTimer = Foundation.Timer.scheduledTimer(timeInterval:0.3,目标:self,选择器:#selector(GameScene.spawnBullet3),userInfo:nil,重复:true)

还尝试过:

 }
func spawnBullet3(){
let Bullet = SKSpriteNode(imageNamed: "eBullet.png")
Bullet.setScale(1)
Bullet.zPosition = -1

Bullet.position = CGPoint(x: enemy.position.x, y: enemy.position.y)

let action = SKAction.moveTo(y: self.size.height + 100, duration: 0.5)
let actionDone = SKAction.removeFromParent()
Bullet.run(SKAction.sequence([action, actionDone]))

Bullet.physicsBody = SKPhysicsBody(rectangleOf: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategories.Bullet
Bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
Bullet.physicsBody?.contactTestBitMask = PhysicsCategories.Enemy
Bullet.physicsBody?.affectedByGravity = true
Bullet.physicsBody?.isDynamic = false
self.addChild(Bullet)

}

最佳答案

好吧,对于初学者来说,(正如 LearnCocos2D 精彩地解释的 here ) 从不使用 TimerperformSelector:afterDelay: ,或 Grand Central Dispatch(GCD,即任何 dispatch_... 方法。

在进行任何操作之前,您必须设置几件事:

首先,将参数添加到您的spawnBullet3函数中(如果您使用自定义类,请将enemy:SKSpriteNode替换为enemy:yourCustomClass) .

func spawnBullet3(enemy:SKSpriteNode){
let Bullet = SKSpriteNode(imageNamed: "eBullet.png")
Bullet.setScale(1)
Bullet.zPosition = -1

Bullet.position = CGPoint(x: enemy.position.x, y: enemy.position.y)

let action = SKAction.moveTo(y: self.size.height + 100, duration: 0.5)
let actionDone = SKAction.removeFromParent()
Bullet.run(SKAction.sequence([action, actionDone]))

Bullet.physicsBody = SKPhysicsBody(rectangleOf: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategories.Bullet
Bullet.physicsBody!.collisionBitMask = PhysicsCategories.None
Bullet.physicsBody?.contactTestBitMask = PhysicsCategories.Enemy
Bullet.physicsBody?.affectedByGravity = true
Bullet.physicsBody?.isDynamic = false
self.addChild(Bullet)
}

其次,创建一个公共(public)敌人数组(同样,如果您使用自定义类,请将 SKSpriteNode 替换为您的自定义类名称)

var enemies:[SKSpriteNode] = []

接下来,每次创建新敌人时,将敌人追加到数组中

enemies.append(newEnemy)

现在您有两个选择:

选项 1:使用 SKAction

使用 SKAction 计时器自动每隔几秒射击一次敌人(将 SKAction.waitForDuration(2.5) 替换为 SKAction.waitForDuration(yourAmountOfSeconds)).

for e in enemies
{
var wait = SKAction.waitForDuration(2.5)
var run = SKAction.runBlock {
spawnBullet3(e)
}

e.runAction(SKAction.repeatActionForever(SKAction.sequence([wait, run])))
}

选项 2:使用更新功能

使用update函数中的currentTime(将currentTime.truncatingRemainder(dividingBy: 3)替换为currentTime.truncatingRemainder(dividingBy: yourAmountOfSeconds)).

override func update(_ currentTime: TimeInterval) {
[...]

if currentTime.truncatingRemainder(dividingBy: 3) == 0
{
for e in enemies
{
spawnBullet3(e)
}
}
}

关于ios - 雪碧包 : how to make newly spawned enemy ships shoot bullets at player?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40458266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com