Swift 如何让玩家在路径中移动到对方?
我希望当接触开始时,玩家(红色圆圈)移动到圆形路径的另一侧。我已经让玩家走上了一条路,但我还没有在互联网上找到我问题的答案Swift 如何让玩家在路径中移动到对方?,swift,path,sprite-kit,move,Swift,Path,Sprite Kit,Move,我希望当接触开始时,玩家(红色圆圈)移动到圆形路径的另一侧。我已经让玩家走上了一条路,但我还没有在互联网上找到我问题的答案 override func didMoveToView(view: SKView) { player = SKSpriteNode(imageNamed: "circulo") player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 170)
override func didMoveToView(view: SKView) {
player = SKSpriteNode(imageNamed: "circulo")
player.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 - 170)
player.color = colorGris
player.colorBlendFactor = 1
player.size = CGSize(width: 25, height: 25)
self.addChild(player)
player.zPosition = 3
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
if gameStarted == false {
gameStarted = true
moveClockWise()
movingClockWise = true
}
}
func moveClockWise(){
let dx = player.position.x - self.frame.width / 2
let dy = player.position.y - self.frame.height / 2
let rad = atan2(dy, dx)
path = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)
let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(follow).reversedAction())
}
覆盖func didMoveToView(视图:SKView){
player=SKSpriteNode(图像名为:“circulo”)
player.position=CGPoint(x:self.frame.width/2,y:self.frame.height/2-170)
player.color=colorGris
player.colorBlendFactor=1
player.size=CGSize(宽:25,高:25)
self.addChild(播放器)
player.zPosition=3
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*当触摸开始时调用*/
如果gameStarted==false{
gameStarted=true
顺时针移动()
顺时针移动=正确
}
}
func顺时针移动(){
设dx=player.position.x-self.frame.width/2
设dy=player.position.y-self.frame.height/2
设rad=atan2(dy,dx)
path=UIBezierPath(弧心:CGPoint(x:self.frame.width/2,y:self.frame.height/2),半径:170,星形:rad,端角:rad+CGFloat(M_PI*4),顺时针:true)
让follow=SKAction.followPath(path.CGPath,asOffset:false,orientToPath:true,速度:200)
player.runAction(SKAction.repeatActionForever(follow.reversedAction())
}
我认为您可以按照以下步骤操作:
- 停止播放机的
:运行操作
let follow = SKAction.followPath(path.CGPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(follow).reversedAction(),withKey:"followPath")
要停止,只需执行以下操作:
player.removeActionForKey("followPath")
- 重新构建路径,使其具有新的“移动点”(起点)和实际位置:
var myCircle : CGMutablePath! = CGPathCreateMutable()
let newDx = player.position.x - self.frame.width / 2
let newDy = player.position.y - self.frame.height / 2
let newRad = atan2(newDy, newDx)
let newPath = UIBezierPath(arcCenter: CGPoint(x:self.frame.width / 2, y: self.frame.height / 2) , radius: 170, startAngle: newRad, endAngle: newRad + CGFloat(M_PI * 4), clockwise: true)
- 镜像路径:
var mirroring = CGAffineTransformMakeScale(1.0, -1.0) // flip horizontal
var mirrorPath : CGMutablePath! = CGPathCreateMutable()
CGPathAddPath(mirrorPath, &mirroring, newPath.CGPath)
- 重新启动
:运行操作
let newFollow = SKAction.followPath(mirrorPath, asOffset: false, orientToPath: true, speed: 200)
player.runAction(SKAction.repeatActionForever(newFollow).reversedAction(),withKey:"followPath")
添加: 如果可以添加一些漂亮的动画,例如点位置和圆中镜像点之间的跳跃,则需要知道
CGPoint
目的地(在镜像路径中,它将是“moveToPoint”或第一个点)。您可以找到一个扩展来获取所有CGPath
点,以便:
var mirrorPoints = mirrorPath.getPathElementsPoints()
let destinationPoint = mirrorPoints.first!
在Sprite kit框架中,可用的SKAction
之间还没有jumpAction,因此您可以用很少的代码创建它。通常,“跳跃”是通过改变Y坐标来实现的,如果您的视图是从高处开始的,那么您可以进行缩放(缩放)。在圆形路径中移动对象的最简单方法之一是
SKNode
容器x
位置设置为圆形路径的半径x
位置// 1) Create the container node
let node = SKNode()
// 2) Create a sprite
let sprite = SKSpriteNode(color:SKColor.blueColor(),size:CGSizeMake(20,20))
var rotation:CGFloat = CGFloat(M_PI)
let radius:CGFloat = 50
override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill
node.position = view.center
// 3) Add the container to the scene
addChild(node)
// 4) Set the sprite's x position
sprite.position = CGPointMake(radius, 0)
// 5) Add the sprite to the container
node.addChild(sprite)
// 6) Rotate the container
rotate()
}
// Rotate the container
func rotate() {
let action = SKAction.rotateByAngle(rotation, duration: 4)
node.runAction(SKAction.repeatActionForever(action),withKey: "rotate")
}
// 8) Reverse the direction of the rotation
func reverse() {
rotation = -rotation
}
// Stop rotating the container
func stopRotation() {
if node.actionForKey("rotate") != nil {
node.removeActionForKey("rotate")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* 7) Change the sprite's x-position */
if sprite.actionForKey("move") == nil {
stopRotation()
let opposite = -sprite.position.x * 2
let move = SKAction.moveByX(opposite, y: 0, duration: 3)
let rotate = SKAction.runBlock {
self.rotate()
}
sprite.runAction(SKAction.sequence([move, rotate]), withKey: "move")
}
}
//1)创建容器节点
let node=SKNode()
//2)创建一个精灵
设sprite=SKSpriteNode(颜色:SKColor.blueColor(),大小:CGSizeMake(20,20))
变量旋转:CGFloat=CGFloat(M_PI)
让半径:CGFloat=50
覆盖func didMoveToView(视图:SKView){
scaleMode=.ResizeFill
node.position=view.center
//3)将容器添加到场景中
addChild(节点)
//4)设置精灵的x位置
sprite.position=CGPointMake(半径,0)
//5)将雪碧添加到容器中
node.addChild(sprite)
//6)旋转容器
轮换
}
//旋转容器
func rotate(){
让动作=SKAction.rotateByAngle(旋转,持续时间:4)
node.runAction(SKAction.repeatActionForever(action),带有键:“旋转”)
}
//8)反转旋转方向
func reverse(){
旋转=-旋转
}
//停止旋转容器
函数停止旋转(){
如果node.actionForKey(“旋转”)!=nil{
node.removeActionForKey(“旋转”)
}
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
/*7)更改精灵的x位置*/
如果sprite.actionForKey(“移动”)==nil{
停止旋转()
让反面=-sprite.position.x*2
让move=SKAction.moveByX(相反,y:0,持续时间:3)
让旋转=SKAction.runBlock{
self.rotate()
}
sprite.runAction(SKAction.sequence([move,rotate]),带有键:“move”)
}
}
谢谢亚历山德罗!让我试试!好的,当你想问我的时候,我就在这里。“镜像路径”无法将类型为“CGAffine Transform”的值转换为预期的参数类型“不安全点”它一直在告诉我是的,从objc到swift 2有一个转换问题,现在看,让我知道它是否有效..如果我想添加另一个精灵,它也会旋转?如果将精灵添加到容器中,它会旋转。如果您将其添加到场景中,它将不会改变Epsilon,但现在我如何将其添加到中心?您希望添加到中心的内容是什么?sprite.position=view.center