【发布时间】:2014-06-13 10:09:23
【问题描述】:
在我的堆栈上工作时,在编辑模式下,我可以通过箭头键轻推任何控件。
当我的堆栈使用“move”命令运行时,我已经取得了一些成功的移动控制,例如
move button 1 to 100,100
有没有更有效的方法在运行时移动控件?
【问题讨论】:
-
有十亿种方法可以更有效地做到这一点。您要解决的实际问题是什么?
标签: livecode
在我的堆栈上工作时,在编辑模式下,我可以通过箭头键轻推任何控件。
当我的堆栈使用“move”命令运行时,我已经取得了一些成功的移动控制,例如
move button 1 to 100,100
有没有更有效的方法在运行时移动控件?
【问题讨论】:
标签: livecode
您可以采用多种方法,具体取决于您希望动画的流畅程度。在最简单的层面上,您需要通过设置与位置相关的属性来移动脚本中的对象:top、left、right、bottom、loc 和 rect。
set the top of button 1 to 10
如果您要在多个方向上移动对象,您需要执行以下操作:
on moveObject
lock screen
lock messages
set the top of button 1 to 10
set the left of button 1 to 20
unlock messages
unlock screen
end moveObject
如果你想要连续的动画,你会想用这样的东西让它循环:
on moveObject
lock screen
lock messages
local tX, tY
# Calculate new position (tX and tY)
# Move objects
set the loc of button 1 to tX, tY
unlock messages
unlock screen
# If animation is not finished, loop
if tEndCondition not true then
send "moveObject" to me in 5 milliseconds
end if
end moveObject
最后,如果您想要一个非常流畅的动画,您需要扩展此循环以根据时间计算对象的位置:
on animationLoop pTime
lock screen
lock messages
local tX, tY
# Calculate new position (tX and tY) based on time
# Move objects
set the loc of button 1 to tX, tY
unlock messages
unlock screen
if tEndCondition not true then
# Calculate when the next frame should be based on your target frame rate
send "moveObject" && tTime to me in tNextFrameTime
end if
end animationLoop
如果某个帧的计算和渲染时间过长,则最后一种方法提供了一种跳过帧的方法。最终结果是一个流畅的动画,反映了用户的期望。
【讨论】:
如果您询问如何在运行(浏览)模式下使用箭头键轻推对象,这是一种方法(处理程序将进入卡片脚本):
on arrowKey pWhich
# determine some way to designate which object is to be nudged
put the long id of btn "test" into tSelObj # for example
switch pWhich
case "left"
put -1 into tXamount
put 0 into tYamount
break
case "up"
put 0 into tXamount
put -1 into tYamount
break
case "right"
put 1 into tXamount
put 0 into tYamount
break
case "down"
put 0 into tXamount
put 1 into tYamount
break
end switch
move tSelObj relative tXamount,tYamount
end arrowKey
【讨论】: