我正在制作一艘太空船游戏,其中敌舰会产生并向玩家移动。
我希望敌人的太空船每x秒发射一次子弹。我尝试使用在恶棍方法中实现的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]))
}
这是敌人的方法:
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")
}
}
我尝试到处寻找,但我找不到答案。
提前感谢!!
答案 0 :(得分:0)
如果我找到你,你希望每隔X秒执行一次随机的spawnRed。正如我在你的代码中看到的那样,你的spawnRed位置是一个随机值(至少是x位置值)。我建议您使用SKAction
,这是我的想法。
你的恶棍功能应该是这样的(设置和创建恶棍是单独的功能):
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()
func中,或者作为另一个将在didMove()
func中执行的函数,我更喜欢将它设置为不同的功能因为舒适和美学:
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了解详情。