Difference between revisions of "Accelerometer"

From GiderosMobile
Line 11: Line 11:
 
=== <translate>Examples</translate> ===
 
=== <translate>Examples</translate> ===
 
'''Example'''<br/>
 
'''Example'''<br/>
<source lang="lua">--create instance
+
<source lang="lua">
 +
local filter = 0.03
 +
local fx, fy, fz = 0, 0, 0
 +
--create instance
 
local accelerometer = Accelerometer.new()
 
local accelerometer = Accelerometer.new()
 
--start receiving data
 
--start receiving data
 
accelerometer:start()
 
accelerometer:start()
---get values for example on each enter frame event
 
local x, y, z = accelerometer:getAcceleration()
 
print(x, y, z)
 
  
--once you don't need it, stop it
+
function onEnterFrame(event)
accelerometer:stop()</source>
+
        ---get values for example on each enter frame event
 +
local x, y, z = accelerometer:getAcceleration()
 +
fx = x * filter + fx * (1 - filter)
 +
fy = y * filter + fy * (1 - filter)
 +
fz = z * filter + fz * (1 - filter)
 +
print("Y: "..math.floor(fy*90*-1))
 +
print("X: "..math.floor(fx*90*-1))
 +
print("Z: "..math.floor(fz*90*-1))
 +
end
 +
stage:addEventListener("enterFrame",onEnterFrame,self)
 +
</source>
 
{|-
 
{|-
 
| style="width: 50%; vertical-align:top;"|
 
| style="width: 50%; vertical-align:top;"|
 +
 
=== <translate>Methods</translate> ===
 
=== <translate>Methods</translate> ===
 
[[Special:MyLanguage/Accelerometer.isAvailable|Accelerometer.isAvailable]] ''<translate>does the accelerometer available?</translate>''<br/><!-- GIDEROSMTD:Accelerometer.isAvailable() does the accelerometer available? -->
 
[[Special:MyLanguage/Accelerometer.isAvailable|Accelerometer.isAvailable]] ''<translate>does the accelerometer available?</translate>''<br/><!-- GIDEROSMTD:Accelerometer.isAvailable() does the accelerometer available? -->

Revision as of 20:08, 6 November 2019


Supported platforms: Platform android.pngPlatform ios.pngPlatform winrt.png
Available since: Gideros 2012.8
Inherits from: Object

Description


The Accelerometer class is used to access accelerometer data.

Examples

Example

local filter = 0.03
local fx, fy, fz = 0, 0, 0
--create instance
local accelerometer = Accelerometer.new()
--start receiving data
accelerometer:start()

function onEnterFrame(event)
        ---get values for example on each enter frame event
	local x, y, z = accelerometer:getAcceleration()
		fx = x * filter + fx * (1 - filter)
		fy = y * filter + fy * (1 - filter)
		fz = z * filter + fz * (1 - filter)
	print("Y: "..math.floor(fy*90*-1))
	print("X: "..math.floor(fx*90*-1))
	print("Z: "..math.floor(fz*90*-1))
end
stage:addEventListener("enterFrame",onEnterFrame,self)

Methods

Accelerometer.isAvailable does the accelerometer available?
Accelerometer.new Creates new Accelerometer instance
Accelerometer:getAcceleration returns the 3-axis acceleration measured by the accelerometer
Accelerometer:start starts accelerometer updates
Accelerometer:stop starts accelerometer updates

Events

Constants