animation.setPosition()

类型 函数
返回值
修订 版本 2024.3703
关键词 动画, 补间动画, 时间轴, 插值, setPosition
另请参阅 动画 — 补间动画和时间轴 (指南)
补间动画
时间轴

概述

animation.setPosition() 方法将根据传入的参数移动/设置播放位置。

重要

将忽略对 时间轴 内特定子补间动画调用此方法。时间轴补间动画由父时间轴控制/拥有,因此您应该通过此方法或 object:setPosition() 设置时间轴本身的位置。

语法

animation.setPosition( position )
animation.setPosition( tweenObject, position )
animation.setPosition( displayObject, position )
animation.setPosition( tagName, position )
animation.setPosition( timelineObject, position )
position (必填)

数字字符串 要将播放移动/设置到的位置(时间或标记),如下所示

  • 对于普通补间动画或时间轴,这可以是指示要将播放移动到的时间的 数字,以毫秒为单位。
  • 特别是对于时间轴,这可以是与时间轴关联的时间标记的 字符串 名称。
tweenObject (可选)

补间动画 要在其上移动/设置位置的特定 补间动画

displayObject (可选)

显示对象 要在其上移动/设置所有补间动画位置的显示对象。

tagName (可选)

字符串 标签名称;所有具有此标签的补间动画/时间轴都将受到影响。

timelineObject (可选)

时间轴 要在其上移动/设置位置的特定 时间轴

示例

所有补间动画/时间轴
local object1 = display.newRect( 50, 50, 100, 100 )
local object2 = display.newRect( 50, 150, 100, 100 )

local tween1 = animation.to( object1, { y=300 }, { time=4000 } )
local tween2 = animation.to( object2, { y=400 }, { time=2000 } )

-- Set the position of all tweens to 1 second in
animation.setPosition( 1000 )
特定补间动画
local object1 = display.newRect( 50, 50, 100, 100 )
local object2 = display.newRect( 50, 150, 100, 100 )

local tween1 = animation.to( object1, { y=300 }, { time=4000 } )
local tween2 = animation.to( object2, { y=400 }, { time=2000 } )

-- Set the position of a specific tween
animation.setPosition( tween1, 1000 )
显示对象补间动画
local object1 = display.newRect( 50, 50, 100, 100 )

local tween1 = animation.to( object1, { y=300 }, { time=4000 } )
local tween2 = animation.to( object1, { rotation=90 }, { time=2000, delay=2000 } )

-- Set the position of all tweens on the object
animation.setPosition( object1, 3000 )
带标签的补间动画
local object1 = display.newRect( 50, 50, 100, 100 )
local object2 = display.newRect( 50, 150, 100, 100 )

local tween1 = animation.to( object1, { y=300 }, { time=4000, tag="tweenTag" } )
local tween2 = animation.to( object2, { y=400 }, { time=2000, tag="tweenTag" } )

-- Set the position of all tweens with the tag "tweenTag"
animation.setPosition( "tweenTag", 1000 )
时间轴时间位置
local object1 = display.newRect( 50, 50, 100, 100 )

-- Create a timeline object
local timelineParams = {
    tweens = {
        { startTime=0, tween={ object1, { x=400 }, { time=4000, iterations=5 } } },
        { startTime=1000, tween={ object1, { y=400 }, { time=4000, easing=easing.outQuad } } }
    }
}
local newTimeline = animation.newTimeline( timelineParams )

-- Set the timeline playing
newTimeline:resume()

-- Sometime later, set the timeline position to a specific time
animation.setPosition( newTimeline, 2000 )
时间轴标记位置
local object1 = display.newRect( 50, 50, 100, 100 )

-- Create a timeline object
local timelineParams = {
    tweens = {
        { startTime=0, tween={ object1, { x=400 }, { time=4000, iterations=5 } } },
        { startTime=1000, tween={ object1, { y=400 }, { time=4000, easing=easing.outQuad } } }
    },
    markers = {
        { name="marker_2000", time=2000 }
    }
}
local newTimeline = animation.newTimeline( timelineParams )

-- Set the timeline playing
newTimeline:resume()

-- Sometime later, set the timeline position to a specific time marker
animation.setPosition( newTimeline, "marker_2000" )