动画(Animation)
字段(Fields)
duration
动画的时长。
function advance(self: AnimationExample, seconds: number): boolean
if self.anim
print(self.anim.duration)
end
return true
end
方法(Methods)
advance
advance(seconds: number) -> boolean
按给定秒数推进动画。如果动画尚未到达终点则返回 true。 如果动画设置为循环(loop)或乒乓(ping-pong),通常会始终返回 true。
type AnimationExample = {
-- Artboard input, assigned in the Rive editor by the designer
character: Input<Artboard<nil>>,
-- The live instance and its animation
artboardInstance: Artboard<nil>?,
anim: Animation?,
}
function init(self: AnimationExample, context: Context): boolean
self.artboardInstance = self.character:instance()
if self.artboardInstance then
self.anim = self.artboardInstance:animation('Idle')
end
return true
end
function advance(self: AnimationExample, seconds: number): boolean
if self.anim and self.artboardInstance then
-- Advance the animation to apply keyframe values to the artboard
self.anim:advance(seconds * 2)
-- Advance the artboard with 0 to propagate changes without double-counting time
self.artboardInstance:advance(0)
end
return true
end
function draw(self: AnimationExample, renderer: Renderer)
if self.artboardInstance then
renderer:save()
self.artboardInstance:draw(renderer)
renderer:restore()
end
end
return function(): Node<AnimationExample>
return {
character = late(),
artboardInstance = nil,
anim = nil,
init = init,
advance = advance,
draw = draw,
}
end
setTime
setTime(time: number) -> ()
以秒为单位设置动画时间。
function advance(self: AnimationExample, seconds: number): boolean
if self.anim and self.artboardInstance then
-- Set the animation to a specific time in seconds
self.anim:setTime(0.3)
-- Advance the artboard with 0 to propagate changes without double-counting time
self.artboardInstance:advance(0)
end
return true
end
setTimeFrames
setTimeFrames(frames: number) -> ()
以帧为单位设置动画时间。
function advance(self: AnimationExample, seconds: number): boolean
if self.anim and self.artboardInstance then
-- Set the animation to a specific frame
self.anim:setTimeFrames(3)
-- Advance the artboard with 0 to propagate changes without double-counting time
self.artboardInstance:advance(0)
end
return true
end
setTimePercentage
setTimePercentage(percentage: number) -> ()
按时长百分比设置动画时间。
function advance(self: AnimationExample, seconds: number): boolean
if self.anim and self.artboardInstance then
-- Set the animation to a specific percentage of the duration.
self.anim:setTimePercentage(.5)
-- Advance the artboard with 0 to propagate changes without double-counting time
self.artboardInstance:advance(0)
end
return true
end