3

I'm making an animation for mobile that moves an image across the screen when the device is tipped. Here's the code I've used to achieve that:

var fl_Accelerometer:Accelerometer = new Accelerometer();
fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
{
    Image1.x -=  event.accelerationX * 300;
    if (Image1.x < 56.75)
    {
        Image1.x = 56.75;
    }
    else if (Image1.x > 1856.75)
    {
        Image1.x = 1856.75;
    }
}

I want to be able to turn Accelerometer input on and off when I click different buttons on the menu screen.

What I thought was to have an integer equal to 1 before I click any buttons; so I do (accelerationX*300)*[that integer] and the accelerometer is enabled. Then I click a 'disable' button, that 1 becomes a 0 and the accelerometer is disabled. Then click another button and the integer becomes 1 again.

How do I do this and is there an easier way?

3 Answers 3

4

What I thought was to have an integer equal to 1 before I click any buttons;

A boolean would be the better choice, but you don't really need an additional variable

To disable the accelerometer in the handler function, simply remove the listener in it:

fl_Accelerometer.removeEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);
Sign up to request clarification or add additional context in comments.

1 Comment

That looks much simpler. I'm such a noob.
2

@null is right as usual...

A boolean would be the better choice, but you don't really need an additional variable

You may declare the

var accControl:Boolean = true; // Variables names should start with a lowercase letter

Then, you change it as this:

accControl = !accControl; // if true => false if false => true

Or with an Integer, you may do

var accControl:int = 1;

then

accControl *= -1; // if 1 => -1 if -1 => 1

If You have a lot of Buttons a Boolean is perhaps the better choice.

Even null's code is more clean AMO, this may helps someone I hope.

Just an example in flash which contains a CustomButton in the Library:

tl, dr;

var marginX:int = 20;
var spacerX:int;
var accControl:Boolean = true; // Variables names should start with a lowercase letter
var accControlInt:int = 1; // Variables names should start with a lowercase letter
var buttons:Vector.<SimpleButton> = new Vector.<SimpleButton>();
function addButtonsFromLib():void{
    for(var i:int=0 ; i<10 ; i++){
    buttons.push(new CustomButton);
    addChild(buttons[i]);
    buttons[i].name = "customButton_" + (i+1)
    spacerX = buttons[i].width;
    buttons[i].x = marginX + spacerX*i + buttons[i].width*i;
    buttons[i].y = 20;
    buttons[i].addEventListener(MouseEvent.MOUSE_DOWN,onClickInt);
    buttons[i].addEventListener(MouseEvent.MOUSE_DOWN,onClickBool);
    }
}
addButtonsFromLib();
function onClickBool(me:MouseEvent):void{
    accControl = !accControl; // if true => false if false => true
    trace(me.target.name + " cliqued => Boolean value is set to : " + accControl);
}
function onClickInt(me:MouseEvent):void{
    accControlInt *= -1; // if 1 => -1 if -1 => 1
    trace(me.target.name + " cliqued => int value is set to : " + accControlInt);
} 

1 Comment

This avoid You to create two methods if You use a Boolean or an integer. Just chose if You want to use a Boolean or an int. The trace method is just useful for debugging. You can forget this @user7083681 ! In place of trace, call another method! It was just an example!
0

This works:

var AccControl:int;
AccControl = 1;
{

MenuScreen.ToggleOffAcc.addEventListener(MouseEvent.CLICK, AccOff);
function AccOff(event:MouseEvent):void
{
    AccControl = AccControl + 1;
}

MenuScreen.ToggleOnAcc.addEventListener(MouseEvent.CLICK, AccOn);
function AccOn(event:MouseEvent):void
{
    AccControl = AccControl - 1;
    MenuScreen.ToggleOnAcc.visible=false;
}
}

var fl_Accelerometer:Accelerometer = new Accelerometer();
fl_Accelerometer.addEventListener(AccelerometerEvent.UPDATE, fl_AccelerometerUpdateHandler);

function fl_AccelerometerUpdateHandler(event:AccelerometerEvent):void
{
    Street.x -=  ((event.accelerationX * 300)* AccControl);

    if (Street.x < 56.75)
    {
        Street.x = 56.75;
    }
    else if (Street.x > 1856.75)
    {
        Street.x = 1856.75;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.