How do I detect a shaking motion using LiveCode mobile?
This lesson will show you how to start using the 'motion' features provided by the accelerometer on an iOS or Android device. Specifically we will look at how you might make your application perform a certain action when the device is given a good shake.
You can download the sample stack from this url: https://tinyurl.com/ybtsv965
Create a stack with a field to display your messages
1) Create a stack
2) Set it to by 320px wide and 460px tall
3) Drag out a new field object
4) Set its name to "messages"
5) untick the 'Focusable' or 'traversalOn' setting
New 'Motion' Messages
We have added two now messages that allow your stacks to be notified when a 'motion' on the device starts and when one stops. Place these two handlers on the card or stack script of your stack.
on motionStart pMotion
# This handler is called when a 'motion' starts
end motionStart
on motionEnd pMotion
# This handler is called when a 'motion' ends
end motionEnd
Implement Shake
Lets start by making sure that we are getting the messages we expect.
on motionStart pMotion
put empty into field "messages"
put "motion start:" && pMotion into line 1 of field "messages"
end motionStart
on motionEnd pMotion
put "motion end:" && pMotion into line 2 of field "messages"
end motionEnd
Testing
The Android simulator does not provide a feature allowing you to test shake motions. To test on Android you will need to test directly on a device, of course the device must have an accelerometer.
Firstly ensure your device is set up and connected as described in the lessons
How do I become an Android Developer on a PC?
How do I become an Android Developer on a Mac?
And that you have set up LiveCode for Android depolyment.
1) Select your Android device from the Development Menu
2) Click Test
The application is deployed to your device and is ready to be tested.
For iOS devices simply click on the Simulate button in the menuBar. Once the simulator has loaded select Shake Gesture from the simulator's Hardware menu.
The Result
So we are getting start and end motion messages. Lets expand out to time how long the user shakes their phone for.
Time how long the shake lasts
Update the card script to the following. It will check if the motion messages are 'shake' and if so record the time of the start and end to get the final result:
# Variable to hold when the shake started
local sStartTime
on motionStart pMotion
# If the motion is a shake do this
if pMotion is "shake" then
put empty into field "messages"
# Store the moment the shake started
put the milliseconds into sStartTime
put "Shake Started" into line 1 of field "messages"
end if
end motionStart
on motionEnd pMotion
if pMotion is "shake" then
put "Shake Ended" into line 2 of field "messages"
# The total length of the shake is the current time - the start time
put "Shake Lasted" && the milliseconds - sStartTime && "milliseconds" into line 3 of field "messages"
end if
end motionEnd
The Result
The simulator sends a consistent shake message but you should be able to try out various lengths of shake once you deploy this little app to you phone.
0 Comments
Add your comment