让雪碧慢慢地向触摸移动



我试图让"玩家"精灵慢慢地向屏幕上的触摸移动。我现在拥有的代码立即将精灵移动到屏幕上发生触摸的位置。这是我当前的代码。谢谢!

//
//  GameScene.swift
//  testing
//
//  Created by Matthew Jahnes on 7/3/18.
//  Copyright © 2018 Matthew Jahnes. All rights reserved.
//
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var player = SKSpriteNode()

override func didMove(to view: SKView) {
self.anchorPoint = CGPoint(x:0.5, y:0.5)

player = SKSpriteNode(color: UIColor.red, size: CGSize(width: 90, height:90))
player.position = CGPoint(x:0, y:0)
self.addChild(player)

}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let location = touch.location(in: self)
player.position.x = location.x
player.position.y = location.y
}
}

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
}

}

你需要

  1. 创建用于将玩家移动到目标点的操作。
  2. 停止上一个move操作(如果存在(
  3. 运行新的move操作

这是代码

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let touch = touches.first else { return }
let destination = touch.location(in: self)
let move = SKAction.move(to: destination, duration: 2)
player.removeAction(forKey: "move")
player.run(move, withKey: "move")
}

最新更新