Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let player = SKSpriteNode(imageNamed: "player-rocket.png")
- override func didMove(to view: SKView) {
- // this method is called when your game scene is ready to run
- let background = SKSpriteNode(imageNamed: "space.jpg")
- background.zPosition = -1
- addChild(background)
- if let particles = SKEmitterNode(fileNamed: "SpaceDust") {
- particles.position.x = 512
- particles.advanceSimulationTime(10)
- addChild(particles)
- }
- player.position.x = -400
- player.zPosition = 1
- addChild(player)
- let tapRecognizer = UITapGestureRecognizer(target: self,
- action: #selector(remoteTapped(_:)))
- tapRecognizer.allowedPressTypes =
- [NSNumber(value: UIPressType.select.rawValue)]
- view.addGestureRecognizer(tapRecognizer)
- }
- func touchDown(atPoint pos : CGPoint) {
- }
- func touchMoved(toPoint pos : CGPoint) {
- }
- func touchUp(atPoint pos : CGPoint) {
- }
- // Persist the initial touch position of the remote
- var touchPositionY: CGFloat = 0.0
- override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
- for touch in touches {
- touchPositionY = touch.location(in:self).y
- }
- }
- override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
- for touch in touches {
- let location = touch.location(in: self)
- if touchPositionY != 0.0 {
- // Calculate the movement on the remote
- let deltaY = touchPositionY - location.y
- // Calculate the new Sprite position
- var y = player.position.y - deltaY
- if y > self.frame.height / 2 {
- y = self.frame.height / 2
- let rotate = SKAction.rotate(byAngle: 1.57, duration: 0.1)
- let move = SKAction.moveTo(x: 10, duration: 0.1)
- let sequence = SKAction.sequence([rotate, move])
- player.run(sequence)
- } else if y < -1 * (self.frame.height / 2) {
- y = -1 * (self.frame.height / 2)
- }
- // Move the sprite
- player.position = CGPoint(x: -400, y: y)
- }
- // Persist latest touch position
- touchPositionY = location.y
- }
- }
- override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
- }
- override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
- }
- override func update(_ currentTime: TimeInterval) {
- // Called before each frame is rendered
- }
- // tvOS Events
- extension GameScene {
- @objc func remoteTapped(
- _ recognizer: UITapGestureRecognizer) {
- //print("Tapped")
- } }
Add Comment
Please, Sign In to add comment