Moving SKSpriteNode to location of the touch

Home / Uncategorized / Moving SKSpriteNode to location of the touch

Question:
Above is an image of my game. A top-down game. Wherever on the screen the player touches, I want the bullets to go to that location with a duration. I also want the player to be able to drag around his finger on the screen, and the same thing happens. So that the player doesn’t have to touch the screen everytime he wants to shoot.

I’ve tried some different stuff so far but nothing seems to work.

First off, I dont know if I should have a separate function for the bullet. But anyway, this is my bullet function.
func spawnBullets() {
let bullet = SKSpriteNode(imageNamed: "Bullet")
bullet.name = "Bullet"
bullet.zPosition = 4
bullet.position = CGPoint(x: player.position.x + 19, y:
player.position.y)
self.addChild(bullet)
}
I also have a "timer" for the bullet in the didMove function:var timer = Timer.scheduledTimer(timeInterval: 0.1, target: self,
selector: Selector("spawnBullets"), userInfo: nil, repeats: true)
And lastly, this is my touchesBegan function:override func touchesBegan(_ touches: Set, with event:
UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
let moveToPoint = SKAction.move(to: location, duration: 0.5)
let repeatAction = SKAction.repeatForever(moveToPoint)
bullet.run(moveToPoint)
}

}


Answer:

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *