function sysCall_init()
a=0
-- This is executed exactly once, the first time this script is executed
bubbleRobBase=sim.getObjectAssociatedWithScript(sim.handle_self) -- this is bubbleRob's handle
leftMotor=sim.getObjectHandle("bubbleRob_leftMotor") -- Handle of the left motor
rightMotor=sim.getObjectHandle("bubbleRob_rightMotor") -- Handle of the right motor
noseSensor=sim.getObjectHandle("bubbleRob_sensingNose") -- Handle of the proximity sensor
minMaxSpeed={50*math.pi/180,300*math.pi/180} -- Min and max speeds for each motor
backUntilTime=-1 -- Tells whether bubbleRob is in forward or backward mode
-- Create the custom UI:
xml = '<ui title="'..sim.getObjectName(bubbleRobBase)..' linmianhaospeed" closeable="false" resizeable="false" activate="false">'..[[
<label text=" speed: " style="* {margin-right: 300px;}" id="2"/>
<hslider minimum="0" maximum="100" on-change="speedChange_callback" id="1"/>
</ui>
]]
ui=simUI.create(xml)
speed=(minMaxSpeed[1]+minMaxSpeed[2])*0.5
simUI.setSliderValue(ui,1,150*(speed-minMaxSpeed[1])/(minMaxSpeed[2]-minMaxSpeed[1]))
--simUI.setLabelText(ui,2,speed)
--simUI.setLabelText(ui,2,'speed: '..a..' velocity')
--simUI.setSliderValue(ui,2,100*(speed-minMaxSpeed[1])/(minMaxSpeed[2]-minMaxSpeed[1]))
end
function speedChange_callback(ui,id,newVal)
speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100
simUI.setLabelText(ui,2,'speed: '..speed..' velocity')
end
1.on-change
当滑动滑条改变滑条的值的时候就会触发speedChange_callback()这个函数,滑条的值就是newValue,然后在
speedChange_callback()这个函数里处理,改变speed的值,同时在label里显示速度的值。代码如下所示:
on-change="speedChange_callback"
Name of a Lua function to handle the change event. Arguments of the function are: (uiHandle, id, newValue).
function speedChange_callback(ui,id,newVal)
speed=minMaxSpeed[1]+(minMaxSpeed[2]-minMaxSpeed[1])*newVal/100
simUI.setLabelText(ui,2,'speed: '..speed..' velocity')
end