Spawn Enemy Code Not working

Hello,

I am fairly new to Xcode & swift.

I have been following a tutorial on YouTube entitled "solo mission"

In this tutorial it uses a slightly older version of Xcode which means some of the code needs to be updated. I have managed to update the code to which there is no errors, but the "spawnEnemy" section is not spawning the enemy's as described in the video.

This is the code I have for the spawning enemy's;

   ///SPAWNING NOT WORKING///

    

    func spawnEnemy(){

        

        let randomXStart = random(min: gameArea.minX, max: gameArea.maxX)

        let randomXEnd = random(min: gameArea.minX, max: gameArea.maxX)

        

        let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)

        let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)

            

        let enemy = SKSpriteNode(imageNamed: "enemyShip")

        enemy.setScale(1)

        enemy.position = startPoint

        enemy.zPosition = 2

        self.addChild(enemy)

            

        let moveEnemy = SKAction.move(to: endPoint, duration: 1.5)

        let deleteEnemy = SKAction.removeFromParent()

        let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])

        enemy.run(enemySequence)

            

        let dx = endPoint.x - startPoint.x

        let dy = endPoint.y - startPoint.y

        let amountToRotate = atan2(dy,dx)

        enemy.zRotation = amountToRotate

            

    }

Can anyone help me figure out why this is not working?

Thank you in advance

Not working is not precise enough.

  • Does it compile (I guess no)
  • What do you get ? What did you expect ?

.

  • Have you check that spawnEnemy is even called ?
  • have you checked positions are correct ?
  • Do you need randomXEnd to be larger than randomXStart ?

So add a few print and correct errors on how you use of random:

func spawnEnemy(){

    print("Start spawn")  // <-- Add this
    // Replace this -->      let randomXStart = random(min: gameArea.minX, max: gameArea.maxX)
    let randomXStart = (gameArea.minX...gameArea.maxX).randomElement() ?? gameArea.minX

    // Replace this -->       let randomXEnd = random(min: gameArea.minX, max: gameArea.maxX)
    let randomXEnd = (gameArea.minX...gameArea.maxX).randomElement() ?? gameArea.maxX

    // If you need randomXEnd to be larger than randomXStart, replace line above with
    // let randomXEnd = (randomXStart...gameArea.maxX).randomElement() ?? gameArea.maxX

    let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
    let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)
    print("startPoint", startPoint, "endPoint", endPoint)

    let enemy = SKSpriteNode(imageNamed: "enemyShip")

    enemy.setScale(1)
    enemy.position = startPoint
    enemy.zPosition = 2

    self.addChild(enemy)

    let moveEnemy = SKAction.move(to: endPoint, duration: 1.5)

    let deleteEnemy = SKAction.removeFromParent()

    let enemySequence = SKAction.sequence([moveEnemy, deleteEnemy])

    enemy.run(enemySequence)

    let dx = endPoint.x - startPoint.x
    let dy = endPoint.y - startPoint.y
    let amountToRotate = atan2(dy,dx)

    print("dx", dx, "dy", dy, "rotation", amountToRotate)  // <-- Add this
    enemy.zRotation = amountToRotate

}

You should also check amountToRotate

  • what happens if dy = 0
  • is it in the right order of parameters ? If dx = 0 do you want zero rotation ?

Test and report what you get. If it does not work as expected, please explain ; otherwise, don't forget to close the thread on the correct answer. Good luck.

it kind worked and i dont know if youll be able to help but now its saying

Referencing instance method 'randomElement()' on 'ClosedRange' requires that 'CGFloat.Stride' (aka 'CGFloat') conform to 'SignedInteger'

Requirement from conditional conformance of 'ClosedRange<CGFloat>' to 'Collection' (Swift.ClosedRange)

this is the code with the problem

let randomXStart = (gameArea.minX...gameArea.maxX).randomElement() ?? gameArea.minX let randomXEnd = (gameArea.minX...gameArea.maxX).randomElement() ?? gameArea.maxX

help

this is all the code func spawnEnemy (){ print("Start spawn")

let randomXStart = (gameArea.minX...gameArea.maxX).randomElement() ?? gameArea.minX
let randomXEnd = (gameArea.minX...gameArea.maxX).randomElement() ?? gameArea.maxX

let startPoint = CGPoint(x: randomXStart, y: self.size.height * 1.2)
let endPoint = CGPoint(x: randomXEnd, y: -self.size.height * 0.2)

let dino = SKSpriteNode(imageNamed: "BlueDino")
dino.setScale(1)
dino.position = startPoint
dino.zPosition = 2
self.addChild(dino)

let moveDino = SKAction.moveTo(endPoint, duration: 1.5)
let deleteDino = SKAction.removeFromParent()
let dinoSequence = SKAction.sequence([moveDino, deleteDino])
dino.run(dinoSequence)

let dx = endPoint.x - startPoint.x
let dy = endPoint.y - startPoint.y
let amountToRotate = atan2(dy, dx)

print("dx", dx, "dy", dy, "rotation", amountToRotate)
dino.zRotation = amountToRotate
Spawn Enemy Code Not working
 
 
Q