I have a problem using the Accelerometer in Flash AS3. The script works fine however the ball goes off screen only to the left and bottom and I would like it to be contained within the stage.
what am I doing wrong?
Here is my script:
import flash.sensors.Ac开发者_如何学Ccelerometer;
import flash.events.AccelerometerEvent;
var my_acc:Accelerometer = new Accelerometer();
my_acc.setRequestedUpdateInterval(50);
my_acc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate);
function onAccUpdate(e:AccelerometerEvent):void{
ball.x -= (e.accelerationX*30);
ball.y += (e.accelerationY*30);
if (ball.x < 0) {
ball.x = 0;
} else if (ball.x > stage.stageWidth) {
ball.x = stage.stageWidth;
}
if (ball.y < 0) {
ball.y = 0;
} else if (ball.y > stage.stageHeight) {
ball.y = stage.stageHeight;
}
}
The problem is because
you are decreasing the x value by accX*30,
which means if you get the ball x value to zero i.e if <0 condition meets ,
then it again goes less than zero beyond the stage.
Whereas in >stagewidth condition , it is decreasing by which the ball moves away from the stage border.
*Same is happening with the y value.
Set the .x<0 condition value to 30 or ball.width instead of 0 and .y>stagewidth value to stagewidth-30 or stagewidth-ball.width
精彩评论