×

[SceneKit专题]16.Actions动作

96
史前图腾
2017.04.16 15:22* 字数 695

说明

本系列文章是对<3D iOS Games by Tutorials>一书的学习记录和部分翻译,此书对应的代码地址:https://github.com/XanderXu/3D-iOS-Games-by-Tutorials-code
系列文章目录:http://www.jianshu.com/p/fd32aa0d896a

动作可以让你操作节点的位置,缩放,旋转和透明度.比如左滑,场景中的小猪向左转并跳一步.
基本分类有四种:Move(移动), Scale(缩放), Rotate(旋转)和Fade(透明渐变).还有两种特殊的动作:Sequence(序列,即顺序执行)和Group(组,即同时执行).
** 注意,关于物理形体physics body**
当需要物理引擎来控制节点的移动和旋转时,应设置为dynamic.如果不需要移动,但仍能参与物理模拟,应设置为static.如果你既想控制某些节点的移动和旋转,同时仍能参与物理模拟,应设置为kinematic.

Move actions移动动作

QQ20170416-150550@2x.png
QQ20170416-150550@2x.png
  • Move Action:在当前位置上移动一段
  • MoveTo Action:移动到指定位置,与当前位置无关


    QQ20170416-165013@2x.png
    QQ20170416-165013@2x.png

Scale actions缩放动作

QQ20170416-150642@2x.png
QQ20170416-150642@2x.png
  • Scale Action: 在当前缩放基础上缩放.
  • ScaleTo Action:缩放到指定比例,与当前比例无关


    QQ20170416-165026@2x.png
    QQ20170416-165026@2x.png

Rotate actions旋转动作

QQ20170416-150736@2x.png
QQ20170416-150736@2x.png
  • Rotate Action:在当前旋转基础上再旋转
  • RotateTo Action:旋转到指定角度,与当前旋转无关
  • RotateTo Action (Shortest):旋转到指定角度,走最短路径
  • RotateBy Axis Angle Action:绕某个轴旋转
  • RotateTo Axis Angle Action:绕某个轴旋转到指定角度


    QQ20170416-165038@2x.png
    QQ20170416-165038@2x.png

Fade actions褪色动作

QQ20170416-152151@2x.png
QQ20170416-152151@2x.png
  • FadeOut Action:淡出,从当前透明度渐渐变完全透明
  • FadeIn Action:淡入,从当前透明度渐渐显示出来,完全不透明
  • FadeOpacityTo Action:改变不透明度到指定值


    QQ20170416-165118@2x.png
    QQ20170416-165118@2x.png

Sequenced and grouped actions序列和组动作

序列和组都是一系列动作的组合,可以让动作更复杂


QQ20170416-151058@2x.png
QQ20170416-151058@2x.png
  • Actions:左侧的每一个都是一个动作,上跳,下落,右移,右转
  • Sequence:蓝色区域内就是序列,上跳动作执行1秒完成后,执行下落动作,按顺序
  • Group:红色区域就是组,将会并发同时执行


    QQ20170416-151245@2x.png
    QQ20170416-151245@2x.png

Linear线性,Ease-In渐入,Ease-Out渐出,East-In-Out渐入渐出

QQ20170416-151324@2x.png
QQ20170416-151324@2x.png

The action editor动作编辑器

QQ20170416-151351@2x.png
QQ20170416-151351@2x.png
  • Expand/Collapse Action Editor:展开/收缩动作编辑器
  • Timeline:时间线
  • Time Cursor:时间游标
  • Time Zoom:时间缩放
  • Drag&Drop Actions:拖放动作

动作创建

拖放一个旋转动作到节点,设置开始时间和持续时长,指定为绕z轴旋转


QQ20170416-151418@2x.png
QQ20170416-151418@2x.png

右击选中Create Loop,创建循环


QQ20170416-151432@2x.png
QQ20170416-151432@2x.png

选中∞无限循环,当它变蓝时就好了. 不要点X,那会取消
QQ20170416-151451@2x.png
QQ20170416-151451@2x.png

拖动游标预览效果


QQ20170416-151504@2x.png
QQ20170416-151504@2x.png

代码添加动作

// 1.时长
let duration = 0.2
// 2.上下移动动作
let bounceUpAction = SCNAction.moveByX(0, y: 1.0, z: 0, duration:
duration * 0.5)
let bounceDownAction = SCNAction.moveByX(0, y: -1.0, z: 0, duration:
duration * 0.5)
// 3.时间模式
bounceUpAction.timingMode = .EaseOut
bounceDownAction.timingMode = .EaseIn
// 4.动作序列
let bounceAction = SCNAction.sequence([bounceUpAction, bounceDownAction])
// 5.四周移动动作
let moveLeftAction = SCNAction.moveByX(-1.0, y: 0, z: 0, duration:
duration)
let moveRightAction = SCNAction.moveByX(1.0, y: 0, z: 0, duration:
duration)
let moveForwardAction = SCNAction.moveByX(0, y: 0, z: -1.0, duration:
duration)
let moveBackwardAction = SCNAction.moveByX(0, y: 0, z: 1.0, duration:
duration)
// 6.旋转动作
let turnLeftAction = SCNAction.rotateToX(0, y: convertToRadians(-90), z:0, duration: duration, shortestUnitArc: true)
let turnRightAction = SCNAction.rotateToX(0, y: convertToRadians(90), z:
0, duration: duration, shortestUnitArc: true)
let turnForwardAction = SCNAction.rotateToX(0, y: convertToRadians(180),
z: 0, duration: duration, shortestUnitArc: true)
let turnBackwardAction = SCNAction.rotateToX(0, y: convertToRadians(0),
z: 0, duration: duration, shortestUnitArc: true)
// 7.动作组
jumpLeftAction = SCNAction.group([turnLeftAction, bounceAction,
moveLeftAction])
jumpRightAction = SCNAction.group([turnRightAction, bounceAction,
moveRightAction])
jumpForwardAction = SCNAction.group([turnForwardAction, bounceAction,
moveForwardAction])
jumpBackwardAction = SCNAction.group([turnBackwardAction, bounceAction,
moveBackwardAction])
SceneKit+ARKit
Web note ad 1