作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在制作一款 spaceship 游戏,其中敌方飞船会生成并向玩家移动。
我希望敌方 spaceship 每 x 秒发射一次子弹。我尝试使用在 villain 方法内部实现的 SKAction,但它不起作用。
我想如果我能得到敌舰节点的位置我就能做到,但我做不到,因为敌舰节点在一个函数中,我无法在函数之外得到它的位置。
这是子弹方法:
func Laser(){
let LaserBullet = SKSpriteNode(imageNamed: "Laser")
//Physics World
LaserBullet.physicsBody = SKPhysicsBody(rectangleOf: LaserBullet.size)
LaserBullet.physicsBody?.categoryBitMask = NumberingPhysics.Laser
LaserBullet.physicsBody?.contactTestBitMask = NumberingPhysics.UpperV | NumberingPhysics.YellowUpper | NumberingPhysics.UpperBlue
LaserBullet.physicsBody?.affectedByGravity = false
LaserBullet.physicsBody?.isDynamic = true
LaserBullet.zPosition = 1
LaserBullet.position = CGPoint(x: SpaceShip.position.x, y: SpaceShip.position.y). // I can put the villains location here if I can get it but I don't know how
let LaserThrown = SKAction.moveTo(y: frame.maxY + SpaceShip.size.height, duration: 1)
let LaserEnded = SKAction.removeFromParent()
run(LaserSound)
addChild(LaserBullet)
LaserBullet.run(SKAction.sequence([LaserThrown,LaserEnded]))
}
这是 Enemy 方法:
func RedSpawn(){
let VillainR = Villain()
VillainR.zPosition = 2
VillainR.zRotation = CGFloat(Double.pi)
let min = 0 + VillainR.size.width
let max = self.frame.width - VillainR.size.width
let spawn = UInt32(max-min)
VillainR.position = CGPoint(x: CGFloat(arc4random_uniform(spawn)),y: self.size.height)
let movement = SKAction.moveTo(y: self.frame.minY + 20, duration: 6.5)
let remove = SKAction.removeFromParent()
VillainR.run(SKAction.sequence([movement,remove]))
//Physics World
VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
VillainR.physicsBody?.categoryBitMask = NumberingPhysics.UpperV
VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
VillainR.physicsBody?.affectedByGravity = false
VillainR.physicsBody?.isDynamic = true
VillainR.physicsBody?.collisionBitMask = 0
addChild(VillainR)
}
这是敌人类(我认为你们不需要这个):
public class Villain: SKSpriteNode {
var life = 1 //Number of lives for the red villain
var hitThisFrame = false //Not used
init(){
let texture = SKTexture(imageNamed: "Villain")
// print("number of lives: ", life)
super.init(texture: texture, color: SKColor.clear, size: texture.size())
}
required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
我到处搜索,但找不到答案。
提前致谢!!
最佳答案
如果我明白了,您希望每 X 秒执行一个随机的 spawnRed。正如我在您的代码中看到的,您的 spawnRed 位置是一个随机值(至少是 x 位置值)。我建议你使用 SKAction
,这是我的想法。
你的 villains 函数应该是这样的(设置和创建 villains 是单独的函数):
func villains(){
let VillainR = Villain()
VillainR.zPosition = 2
VillainR.zRotation = CGFloat(Double.pi)
let min = 0 + VillainR.size.width
let max = self.frame.width - VillainR.size.width
let spawn = UInt32(max-min)
VillainR.position = CGPoint(x: CGFloat(arc4random_uniform(spawn)),y: self.size.height)
//Physics World
VillainR.physicsBody = SKPhysicsBody(rectangleOf: VillainR.size)
VillainR.physicsBody?.categoryBitMask = NumberingPhysics.UpperV
VillainR.physicsBody?.contactTestBitMask = NumberingPhysics.Laser | NumberingPhysics.SpaceShip
VillainR.physicsBody?.affectedByGravity = false
VillainR.physicsBody?.isDynamic = true
VillainR.physicsBody?.collisionBitMask = 0
let m = SKAction.moveTo(y: self.frame.minY + 20, duration: 6.5)
let remove = SKAction.removeFromParent()
VillainR.run(SKAction.sequence([movement,remove]))
addChild(VillainR)
}
对于重复的 SKAction,我认为你应该将它设置在你的 didMove()
函数中,或者作为将在 didMove()
函数中执行的另一个函数,我由于舒适和美观,更喜欢将其设置为不同的功能:
func spawnReds() {
let wait = SKAction.wait(forDuration: 3) //the time in seconds between each action
let action = SKAction.run {
self.villains() //create a villain
}
run(SKAction.repeatForever((SKAction.sequence([wait, action])))) // repeat this action forever = the villains function will executed every 3 seconds, which means that every 3 seconds a new villains will shout.
}
如果您在单独的函数中执行此操作,您应该在 didMove 中调用该函数,如下所示:
override func didMove(to view: SKView) {
spawnReds()
}
可以看看apple doc了解更多信息。
关于swift - 如何让生成的敌舰每 x 秒发射一次子弹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47345455/
我是一名优秀的程序员,十分优秀!