I learn book 2 d games by tutorials and I passed chapter 2 Manual Movement where i made sprite move to the point which player touched.Using math and Geometry now I want to do the same thing but using actions.How can I do that?
Hi @misterdave, when you mean using “actions” do you mean using SKAction? If so, you could do something like this to move a sprite:
func movePlayer (moveBy: CGFloat, forTheKey: String) {
let moveAction = SKAction.moveBy(x: moveBy, y: 0, duration: 1)
let repeatForEver = SKAction.repeatForever(moveAction)
let seq = SKAction.sequence([moveAction, repeatForEver])
// Player action
player.run(seq, withKey: forTheKey)
}
// TouchesBegan:
if leftButton.contains(pointTouched) {
movePlayer(moveBy: -30, forTheKey: "left")
}
if rightButton.contains(pointTouched) {
movePlayer(moveBy: 30, forTheKey: "right")
}
// TouchesEnded:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
player.removeAction(forKey: "left")
player.removeAction(forKey: "right")
}
Hope this points you in the right direction! Happy coding,
Gina
This topic was automatically closed after 166 days. New replies are no longer allowed.