Working with speed

Hi, I’m trying to build an app in which a rectangle changes its width depending on the speed of the device.

I’ve written some code, but it doesn’t work. Here it is.

local function onSpeed ()

if event.speed = event.speed + 0.001  then

rectSpeed.x = rectSpeed.x + 1

end

–rectSpeed is the rectangle

event.speed will never equal event.speed + 0.001.  The “if” statement will always evaluate to false.  Perhaps you need to track the last event.speed and see if the current event.speed is greater than the last event.speed.

local lastSpeed

local function onSpeed()

     if event.speed > lastSpeed then

           lastSpeed = event.speed

           rectSpeed.x = restSpeed.x + 1

     end

end

or something like that.

Rob

There seem to be several things wrong with the code you posted. I don’t think it can compile at all.

local function onSpeed () if event.speed = event.speed + 0.001 then rectSpeed.x = rectSpeed.x + 1 end
  1. You’re missing an end statement (there should be two)

  2. Where does “event” come from? I would expect it to be an argument of the onSpeed function, i.e. onSpeed(event)

  3. “if event.speed = event.speed + 0.001 then” is not valid Lua syntax. You need to use == instead of =

event.speed will never equal event.speed + 0.001.  The “if” statement will always evaluate to false.  Perhaps you need to track the last event.speed and see if the current event.speed is greater than the last event.speed.

local lastSpeed

local function onSpeed()

     if event.speed > lastSpeed then

           lastSpeed = event.speed

           rectSpeed.x = restSpeed.x + 1

     end

end

or something like that.

Rob

There seem to be several things wrong with the code you posted. I don’t think it can compile at all.

local function onSpeed () if event.speed = event.speed + 0.001 then rectSpeed.x = rectSpeed.x + 1 end
  1. You’re missing an end statement (there should be two)

  2. Where does “event” come from? I would expect it to be an argument of the onSpeed function, i.e. onSpeed(event)

  3. “if event.speed = event.speed + 0.001 then” is not valid Lua syntax. You need to use == instead of =