I am making a space ship game in which enemy ships spawn and move towards the player.
I want the enemy space ships to fire bullets every x seconds. I tried using an SKAction that was implemented inside of the villain method but it does not work.
I think if I could get the enemy ship node location I could do it but I can't since the enemy ship node is in a function and I can't get its location outside of the function.
This is the bullet method:
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]))
}
This is the Enemy method:
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)
}
And this is the enemy class (I don't think you guys need this):
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")
}
}
I tried searching everywhere but I cannot find the answer.
Thank You In Advance !!