0

I am trying to make a choppping motion based on the accelerometer of my tablet and showing it using away3D box. Essentially what I want to do it every time the user shakes the device on accelerationY the box moves up and then right away moves back down. Think of a knife chopping motion. Right now I have the following code but it seems to cancel itself out and just remain in the same place. Any help would be much appreciated!

protected function onAccUpdate(event:AccelerometerEvent):void{
            var threshold:Number = 1.5;
            if(event.accelerationY > threshold){
                targetY = event.accelerationY *10;
                knife.y += targetY;
                trace(knife.y);
                if (knife.y >0){
                    knife.y -= targetY; 
                    trace(knife.y);
                }

1 Answers1

0

In the execution order of your function, as soon as you set knife.y += targetY; the if (knife.y >0) returns true and sets knife.y back to the original position. Therefore the knife looks like it never moved.

You should use an onEnterFrame update function to ease the knife towards the target position. Then in your onAccUpdate function if the knife needs to move set targetY to the new position. In your update function, as soon as the knife has reached the new position set targetY to 0 and animate the knife back.

Here's an example of onEnterFrame code:

var diff:Number = targetY - knife.y;
knife.y += diff * 0.05; // 0.05 is to ease/smooth the animation
if( Math.abs( diff ) <= 1 ) targetY = 0; // we reached the target position, now go back to 0