SpriteKit Game

OK I'm trying to make a sprite(model.Emmiter) that shoots balls(EnergyBalls) 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) {
        
    }
}

Hi,

You might want to try using an SKEmitterNode. This is a specialized SpriteKit node that will emit particles. You can create a new emitter node in Xcode by going to File > New > File... (or with the keyboard shortcut ⌘N) and selecting the "SpriteKit Particle File". Xcode will ask you what you want to use as a template - you can select whichever one you think sounds most like the effect you're trying to achieve. After creating your particle file, you can use the inspector area to tweak the emitter. Make sure you have your "Energy" texture selected.

After you've finished editing your particle file, you can easily use the emitter in your code.

First, initialize an SKEmitterNode from the file you just created.

let emitterNode = SKEmitterNode(fileNamed: "YourFileName")!
emitterNode.position = .zero
addChild(emitterNode)

If an SKEmitterNode won't work in your case, though, I would review your code. First of all, the function touchDown(atPoint pos: CGPoint) isn't called anywhere. What is the difference between this function and the SpriteKit touchesBegan() function? Also, I might try adding your energyBall to the scene before changing its position and running the fly action. Finally, using SKAction.run() just runs a block of code, which wouldn't make sense in this context. Try using SKAction.sequence() to run SKActions in order. This might look like this:

let moveAction = SKAction.move(to: location, duration: 1)
let scaleAction = SKAction.scale(to: 0, duration: 0.125)

let fly = SKAction.sequence([moveAction, scaleAction, .removeFromParent()])

SKAction.sequence() will run actions in order, waiting for each action to finish before running the next one. For example, the scaleAction in this case will run after the moveAction is finished, meaning it will run 1 second after.

Good luck finding the solution to your problem, and remember it's best practice to not capitalize the beginnings of your variable and constant names. (let Emitter should be let emitter and let EnergyBall should be let energyBall)

Hi when I'm deleting blank lines from copy-and pasted code from xcode, I accidently deleted some of thfunc fireEnergyBallsection... and not realize it😂🫠

SpriteKit Game
 
 
Q