我在SpriteKit中玩了一下,我正试图让一个棒图运行。由于我缺乏经验,我相信我已经以非常规的方式开始了这个过程。
我没有绘制每个单独的运动框架,而是加载了棒图的分离图像(即头部,膝盖,大腿,身体,手臂等)并告诉他们相应地移动。到目前为止,除了在运行过程中膝盖的运动和旋转外,所有东西都要合在一起。
我的问题是:
当大腿向后拉动时,膝盖会脱离,并在旋转时沿直线行进到步幅的末端。
我的问题是:
有没有办法将大腿节点连接到小腿节点,好像它们在运动时以“膝盖”的方式连接在一起?或者是否有更有效的方法?
以下是我的代码片段,它可能比我更具描述性。
func thigh_run(){
//running config
//DONT CHANGE!
var thigh_rotation = CGFloat(M_PI)*0.8
//Can Change :)
var run_speed = NSTimeInterval(0.25)
// reset the action sequence... basically
let pos_nuetralize = SKAction.rotateByAngle(thigh_rotation/2, duration: run_speed)
let neg_nuetralize = SKAction.rotateByAngle(-thigh_rotation/2, duration: run_speed)
//strides
let thigh_pull = SKAction.rotateByAngle(-thigh_rotation, duration: run_speed*2)
let thigh_stride = SKAction.rotateByAngle(thigh_rotation/2, duration: run_speed)
// for the opposite leg
let b_thigh_pull = SKAction.rotateByAngle(-thigh_rotation/2, duration: run_speed)
let b_thigh_stride = SKAction.rotateByAngle(thigh_rotation, duration: run_speed*2)
//knee movement
let stride = SKAction.sequence([thigh_stride, thigh_pull, pos_nuetralize])
let pull = SKAction.sequence([b_thigh_pull, b_thigh_stride, neg_nuetralize])
r_arm.runAction(SKAction.repeatActionForever(stride))
f_leg_thigh.runAction(SKAction.repeatActionForever(stride))
b_leg_thigh.runAction(SKAction.repeatActionForever(pull))
//knee_run(run_speed)
}
func knee_run(speed: NSTimeInterval){
let knee_stride = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x+f_leg_thigh.size.height, f_leg_thigh.position.y), duration: speed)
let knee_reset = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x, f_leg_thigh.position.y-f_leg_thigh.size.height), duration: speed)
let knee_pull_rotate = SKAction.rotateToAngle(CGFloat(M_PI)/2, duration: speed, shortestUnitArc: true)
let knee_pull_rotate_half = SKAction.rotateToAngle(CGFloat(M_PI)*2, duration: speed, shortestUnitArc: true)
let knee_pull_half = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x-f_leg_thigh.size.height, f_leg_thigh.position.y/2), duration: speed)
let knee_pull = SKAction.moveTo(CGPointMake(f_leg_thigh.position.x-f_leg_thigh.size.height, f_leg_thigh.position.y), duration: speed)
//Grouping of the knee moving and rotating at the same time
let knee_pull_group_half = SKAction.group([knee_pull_half, knee_pull_rotate_half])
let knee_pull_group = SKAction.group([knee_pull, knee_pull_rotate])
let f_knee_stride_seq = SKAction.sequence([knee_stride,knee_reset,knee_pull,knee_reset])
let f_knee_pull_seq = SKAction.sequence([knee_pull_group_half, knee_reset, knee_stride, knee_reset])
f_leg_calf.runAction(SKAction.repeatActionForever(f_knee_stride_seq))
b_leg_calf.runAction(SKAction.repeatActionForever(f_knee_pull_seq))
}
我明白这可能很难读,我已经为自己做了很好的记录。 f_前缀用于“前”腿,b 用于“后退”_leg。然而把它想象成右腿和左腿。
反正!提前致谢!我感谢任何帮助或提示!评论任何进一步的解释! :)
答案 0 :(得分:2)
更有效的方法是使用SKPhysicsJoint将大腿节点连接到小腿节点。