Created
October 19, 2010 09:59
-
-
Save jankeesvw/633947 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package { | |
import flash.display.DisplayObject; | |
import flash.events.Event; | |
import flash.events.KeyboardEvent; | |
import flash.system.System; | |
import flash.ui.Keyboard; | |
/** | |
* @author Jankees van Woezik | Base42.nl | |
* | |
* Usage: | |
* new PositionDebugBehavior(_arrow); | |
* | |
*/ | |
public class PositionDebugBehavior { | |
private var _displayObject:DisplayObject; | |
public function PositionDebugBehavior(inDisplayObject:DisplayObject) { | |
_displayObject = inDisplayObject; | |
if(_displayObject.stage) { | |
initialize(); | |
} else { | |
_displayObject.addEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); | |
} | |
} | |
private function handleAddedToStage(event:Event):void { | |
_displayObject.removeEventListener(Event.ADDED_TO_STAGE, handleAddedToStage); | |
initialize(); | |
} | |
private function initialize():void { | |
error("POSITION BEHAVIOUR INITIALIZED, REMOVE WHEN READY"); | |
_displayObject.stage.addEventListener(KeyboardEvent.KEY_UP, handleKeyEvent); | |
} | |
private function handleKeyEvent(event:KeyboardEvent):void { | |
var x:int = 0; | |
var y:int = 0; | |
switch (event.keyCode) { | |
case Keyboard.LEFT: | |
x = -1; | |
break; | |
case Keyboard.RIGHT: | |
x = 1; | |
break; | |
case Keyboard.UP: | |
y = -1; | |
break; | |
case Keyboard.DOWN: | |
y = 1; | |
break; | |
} | |
if(event.shiftKey) { | |
x *= 5; | |
y *= 5; | |
} | |
if(event.ctrlKey) { | |
x *= 10; | |
y *= 10; | |
} | |
_displayObject.x += x; | |
_displayObject.y += y; | |
debug("x: " + _displayObject.x + ", y:" + _displayObject.y); | |
System.setClipboard("x = " + _displayObject.x + "; \ny = " + _displayObject.y + ";"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment