OK I'm trying to make a sprite(model.Emmiter
) that shoots balls(EnergyBall
s) and the balls wont emit at the touch location:
import SpriteKit
import GameplayKit
class Sprites {
var Emmiter: SKSpriteNode = .init(imageNamed: "Emmiter")
}
class GameScene: SKScene {
var model: Sprites = .init()
var Emmiter = Sprites().Emmiter
var playableRect: CGRect = .zer
var lastTouch: CGPoint = .zero
override func didMove(to view: SKView) {
Emmiter.position = CGPoint(x: size.width / 2, y: size.width/* view.frame.minY + 100 */)
print(Emmiter.position)
self.addChild(Emmiter)
}
func touchDown(atPoint pos : CGPoint) {
lastTouch = pos
let rotation = -atan2(
lastTouch.x - Emmiter.position.x,
lastTouch.y - Emmiter.position.y
)
Emmiter.run(
.rotate(
toAngle: rotation,
duration: 0.25
)
)
fireEnergyBall(atPoint: lastTouch)
}
func touchMoved(toPoint pos : CGPoint) {
}
func touchUp(atPoint pos : CGPoint) {
}
func fireEnergyBall(atPoint location: CGPoint) {
let EnergyBall = SKSpriteNode(imageNamed: "Energy")
EnergyBall.position = Emmiter.position
print(EnergyBall.position)
let fly: SKAction = .run {
EnergyBall.run(.move(to: location, duration: 1))
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
EnergyBall.un(.sequence([.scale(to: 0, duration: 0.125), .removeFromParent()]))
}
}
EnergyBall.run(fly)
self.addChild(EnergyBall)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let rotation = -atan2(
touches.first!.location(
in: self
).x - Emmiter.position.x,
touches.first!.location(
in: self
).y - Emmiter.position.y
)
Emmiter.run(
.rotate(
toAngle: rotation,
duration: 0.25
)
)
fireEnergyBall(atPoint: touches.first!.location(in: self.view))
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: TimeInterval) {
}
}