If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Adobe AIR Development

Reply
Developer
RapsFan
Posts: 442
Registered: ‎08-02-2010
My Device: Z10
My Carrier: Telus
Accepted Solution

Accelerometer shake trigger in Flash

I've been experimenting with the accelerometer class in Flash CS5 and made a simple test app of a ball that moves depending on the device tilt (tested in Adobe Device Central). What I want to do now is detect a shake to trigger a method.

 

I created a maagic 8 ball in Flash that displays a response via an event:MouseClick. Rather than using the mouseclick I want to detect a shake. Any ideas? All my AS3 code is in the actions layer, no code in the class definition.

Please use plain text.
New Contributor
newdawnguitar
Posts: 3
Registered: ‎01-23-2011
My Device: Not Specified

Re: Accelerometer shake trigger in Flash

I was looking for the the same thing today and stumbled across the answer here:

 

http://www.russtarleton.com/2010/05/detecting-a-shake-on-a-flash-10-1-enabled-android-device-using-a...

 

If you are coding in the actions layer only, you'll want to remove the class stuff (ie, the 'private' before the functions and vars)...but it's essentially all the same. 

 

I have used this method on iPhone and worked fine. Although I could not get the shake_wait to work. Once i deleted all shake_wait references, i was able to get it work.

Please use plain text.
Developer
RapsFan
Posts: 442
Registered: ‎08-02-2010
My Device: Z10
My Carrier: Telus

Re: Accelerometer shake trigger in Flash

Thanks newdawn. I acctually came across this the other day but couldn't get it working, so I'm glad to know about shake_wait but I'm still having trouble getting this code working.

 

Here is my code:

 

import flash.events.AccelerometerEvent;
import flash.sensors.Accelerometer;

// Add these two private variables to your class
var _accl                 : Accelerometer;
var _lastShake             : Number = 0;

// Put this where you initialize your class – it detects if accelerometer is supported on the device and adds the listener
if (Accelerometer.isSupported)
{
_accl = new Accelerometer();
_accl.addEventListener(AccelerometerEvent.UPDATE, onAccelerometer);
}

// Put this where you destroy your class – it detects if accelerometer is supported on the device and removes the listener
if (Accelerometer.isSupported)
{
_accl.removeEventListener(AccelerometerEvent.UPDATE, onAccelerometer);
}

// Add this method to your class – it is the callback for the accelerometer
function onAccelerometer(e:AccelerometerEvent) : void
{
if (e.accelerationX >= 2 || e.accelerationY >= 2 || e.accelerationZ >= 2)
{
showAnswers(); // Replace “doShake();” with a call to the method you wish to execute when the shake occurs.
_lastShake = getTimer(); // Reset the timer so that this code block doesn’t happen again until _SHAKE_WAIT milliseconds later.
}
}

 

showAnswers(); is my method I replaced doShake(); with. I also tried

 trace("X axis = "+e.accelerationX); to see if the code was working but it isn't, device central showed nothing from the trace.

Please use plain text.
New Contributor
newdawnguitar
Posts: 3
Registered: ‎01-23-2011
My Device: Not Specified

Re: Accelerometer shake trigger in Flash

Are these all placed exactly as-is? If so, then you are removing the accelerometer event listener immediately after you add it...try deleting the following for now:

 

// Put this where you destroy your class – it detects if accelerometer is supported on the device and removes the listener
if (Accelerometer.isSupported)
{
_accl.removeEventListener(AccelerometerEvent.UPDATE, onAccelerometer);
}

 

 

Please use plain text.
Developer
RapsFan
Posts: 442
Registered: ‎08-02-2010
My Device: Z10
My Carrier: Telus

Re: Accelerometer shake trigger in Flash

Thanks :smileyhappy: That was glaringly obvious after you pointed it out hehe. Although it's still not working

 

var _accl                 : Accelerometer;
var _lastShake             : Number = 0;

 

if (Accelerometer.isSupported)
{
_accl = new Accelerometer();
_accl.addEventListener(AccelerometerEvent.UPDATE, onAccelerometer);
}

 

function onAccelerometer(e:AccelerometerEvent) : void
{
if (e.accelerationX >= 2 || e.accelerationY >= 2 || e.accelerationZ >= 2)
{
trace("X axis = "+e.accelerationX);
trace("Y axis = "+e.accelerationY);

_lastShake = getTimer(); // Reset the timer so that this code block doesn’t happen again until _SHAKE_WAIT milliseconds later.
}
}

 

This Actionscript is inside a frame and not in class description.

Please use plain text.
Developer
RapsFan
Posts: 442
Registered: ‎08-02-2010
My Device: Z10
My Carrier: Telus

Re: Accelerometer shake trigger in Flash

Got it :smileyhappy: Using an else statement with trace I discovered that I wasn't shaking fas enough in the simulator so I decreased the value from 2 to .001

 

 

function onAccUpdate(e:AccelerometerEvent):void{
if (e.accelerationX >= .001 || e.accelerationY >= .001 || e.accelerationZ >= .001)
{
 gotoAndStop("Answer");
    showAnswers();
trace("That's it!"); // Replace “doShake();” with a call to the method you wish to execute when the shake occurs.
_lastShake = getTimer(); // Reset the timer so that this code block doesn’t happen again until _SHAKE_WAIT milliseconds later.
}else{
 trace("Too slow");
}
}
Please use plain text.
New Contributor
newdawnguitar
Posts: 3
Registered: ‎01-23-2011
My Device: Not Specified

Re: Accelerometer shake trigger in Flash

nice!! actually that's helpful to know, as I have noticed i have to really shake it quite hard in order for it to work. I'm going to decrease it as well.  :smileyhappy:

Please use plain text.