Skip to content

Instantly share code, notes, and snippets.

@PrimaryFeather
Created September 18, 2017 09:18
Show Gist options
  • Save PrimaryFeather/a71e24185e139a0b4c85c5e92826211e to your computer and use it in GitHub Desktop.
Save PrimaryFeather/a71e24185e139a0b4c85c5e92826211e to your computer and use it in GitHub Desktop.
A demo that shows how small mouse movements are not captured correctly when mouse lock is enabled in Adobe AIR / Flash.
package flash
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageDisplayState;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.ui.Keyboard;
public class MouseDeltaDemo extends Sprite
{
private var _object:Shape;
public function MouseDeltaDemo()
{
_object = new Shape();
_object.graphics.beginFill(0xff0000);
_object.graphics.drawCircle(0, 0, 20);
_object.graphics.endFill();
addChild(_object);
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
private function onAddedToStage(e:Event):void
{
_object.x = stage.stageWidth / 2;
_object.y = stage.stageHeight / 2;
var textField:TextField = new TextField();
textField.width = stage.stageWidth;
textField.height = 100;
textField.text = "Hit 'F' to switch between window- and fullscreen mode.\n" +
"Hit 'M' to toggle mouse lock.";
textField.x = textField.y = 10;
addChildAt(textField, 0);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onMouseMove(event:MouseEvent):void
{
if (stage.mouseLock)
{
_object.x += event.movementX;
_object.y += event.movementY;
if (_object.x < 0) _object.x = 0;
if (_object.y < 0) _object.y = 0;
if (_object.x > stage.stageWidth) _object.x = stage.stageWidth;
if (_object.y > stage.stageHeight) _object.y = stage.stageHeight;
}
else
{
_object.x = event.localX;
_object.y = event.localY;
}
}
private function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.F)
{
if (stage.displayState == StageDisplayState.NORMAL)
stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
else
stage.displayState = StageDisplayState.NORMAL;
}
if (event.keyCode == Keyboard.M &&
stage.displayState == StageDisplayState.FULL_SCREEN_INTERACTIVE)
{
stage.mouseLock = !stage.mouseLock;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment