Skip to content

Instantly share code, notes, and snippets.

@geekhunger
Created January 17, 2015 16:11
Show Gist options
  • Save geekhunger/1b1009e6026c388f5924 to your computer and use it in GitHub Desktop.
Save geekhunger/1b1009e6026c388f5924 to your computer and use it in GitHub Desktop.
Codea deviceShaking() function
-- Accelometer Shake
supportedOrientations(LANDSCAPE_RIGHT)
function setup()
print("Shake a few times until the rect() exceeds the ellipse().")
end
local function deviceShaking()
local acceleration = UserAcceleration
local intensity = 4 -- adjust min. shaking intensity to trigger this event!
if UserAcceleration.x > intensity or acceleration.y > intensity or acceleration.z > intensity then
_deviceShakeUpdatedAt = ElapsedTime
_deviceShakeBegunAt = _deviceShakeBegunAt or ElapsedTime
-- first shake triggers listening process
-- listen if shaking continues for [x] seconds then device is considered shaking on purpose
if (ElapsedTime - _deviceShakeBegunAt) >= .5 then return true end
end
if _deviceShakeUpdatedAt and (ElapsedTime > (_deviceShakeUpdatedAt + .5)) then
_deviceShakeBegunAt = nil
_deviceShakeUpdatedAt = nil
end
return false
end
function draw()
if deviceShaking() then
print("DEVICE SHAKING")
background(255, 60, 25)
else
background(100)
end
translate(WIDTH/2, HEIGHT/2)
noFill()
stroke(0)
strokeWidth(2)
rectMode(CENTER)
ellipse(0, 0, 100)
rect(UserAcceleration.x * 40, UserAcceleration.y * 40, 10)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment