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.
Topic:
Programming Languages
SubTopic:
Swift
Tags: