Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import flash.desktop.NativeApplication;
- import flash.display.DisplayObject;
- import flash.display.DisplayObjectContainer;
- import flash.events.Event;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageScaleMode;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.geom.Rectangle;
- import flash.ui.Keyboard;
- import flash.ui.Multitouch;
- import flash.ui.MultitouchInputMode;
- var DEFAULT_WIDTH:Number;
- var DEFAULT_HEIGHT:Number;
- var MIN_SCALE:Number = 100;
- var MAX_SCALE:Number = 800;
- var MOVE_SPEED: Number = 6;
- var SIZE_SCALE:Number = 6;
- var clickMore: Boolean = false;
- var clickLess: Boolean = false;
- var clickLeft: Boolean = false;
- var clickRight: Boolean = false;
- var clickUp: Boolean = false;
- var clickDown: Boolean = false;
- var clickCenter: Boolean = false;
- var keys:Object = {};
- /////////////// VERY IMPORTANT ///////////
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- /////////////////////////////////////////
- addChild(myMCimg);
- //Set default width and height for the object so when clicking CENTER button it will default to that.
- DEFAULT_WIDTH = myMCimg.width;
- DEFAULT_HEIGHT = myMCimg.height;
- //Alignment constants
- var Align:Object = {
- CENTER: "center",
- LEFT: "left",
- RIGHT: "right",
- TOP:"top",
- BOTTOM: "bottom",
- CURRENT_PIVOT: {}
- }
- var Scaling:Object = {
- UP: "up",
- DOWN: "down",
- scale: function($obj:Object, $scaling:String, $percentage:Number):Object{
- $percentage = $percentage/100;
- var width:Number = $obj.width * $percentage;
- var height:Number = $obj.height * $percentage;
- var scalingWidth:Number = $scaling == Scaling.UP ? + width : - width;
- var scalingHeight:Number = $scaling == Scaling.UP ? + height : - height;
- $obj.width += scalingWidth;
- $obj.height += scalingHeight;
- return $obj;
- }
- }
- function resize($obj:DisplayObject, $scaling:String, $percentage:Number, $min:Number=1, $max:Number=3000):void
- {
- var size:Object = Scaling.scale({width:$obj.width, height:$obj.height}, $scaling, $percentage);
- if (size.width < $min || size.height < $min) return;
- if (size.width > $max || size.height > $max) return;
- $obj.width = size.width;
- $obj.height = size.height;
- }
- /* Function for setting the object's registration/pivot/anchor point
- Note: Call setPivot() function only ONCE along the code to set the object's
- registration/pivot/anchor point to desired position.*/
- function setPivot(s:DisplayObjectContainer, alignX:String = "center", alignY:String = "center"):Object{
- Align.CURRENT_PIVOT[s.name] = {x:alignX, y:alignY};
- var obj:Object = {x:s.x, y:s.y};
- var rect:Rectangle = s.getBounds(s);
- var _x:Number;
- var _y:Number;
- if (alignX == Align.CENTER) {
- _x = s.width / 2;
- } else if (alignX == Align.LEFT) {
- _x = 0;
- }
- else if (alignX == Align.RIGHT) {
- _x = s.width;
- }
- if (alignY == Align.CENTER) {
- _y = s.height / 2;
- } else if (alignY == Align.TOP) {
- _y = 0;
- }
- else if (alignY == Align.BOTTOM) {
- _y = s.height;
- }
- var x_offset:Number = _x + rect.x;
- var y_offset:Number = _y + rect.y;
- obj.x -= x_offset;
- obj.y -= y_offset;
- return obj;
- }
- //Function to center object on stage
- function centerImage(image:DisplayObjectContainer):void {
- var x:Number = (stage.fullScreenWidth - image.width)/2;
- var y:Number = (stage.fullScreenHeight - image.height) / 2;
- image.x = x;
- image.y = y;
- }
- /*Function to show the current registration/pivot/anchor point of the object.
- It will appear as a 10x10 blue square. */
- function showPivot(image:DisplayObjectContainer):void {
- if (!Align.CURRENT_PIVOT[image.name]) return;
- var spr:Sprite = new Sprite();
- spr.graphics.beginFill(0x0000ff);
- spr.graphics.drawRect(0, 0, 10, 10);
- addChild(spr);
- var x:Number, y:Number;
- var pivotX:String = Align.CURRENT_PIVOT[image.name].x;
- var pivotY:String = Align.CURRENT_PIVOT[image.name].y;
- if (pivotX == Align.CENTER) {
- x = image.x + ((image.width - spr.width)/2);
- } else if (pivotX == Align.LEFT) {
- x = image.x;
- } else if (pivotX == Align.RIGHT) {
- x = image.x + (image.width - (spr.width));
- }
- if (pivotY == Align.CENTER) {
- y = image.y + ((image.height - spr.height) / 2);
- } else if (pivotY == Align.TOP) {
- y = image.y;
- } else if (pivotY == Align.BOTTOM) {
- y = image.y + (image.height-(spr.height));
- }
- spr.x = x;
- spr.y = y;
- }
- //INIT
- /* Note: Call setPivot() function only ONCE along the code to set the object's
- registration/pivot/anchor point to desired position.*/
- myMCimg.x = setPivot(myMCimg, Align.LEFT, Align.BOTTOM).x;
- myMCimg.y = setPivot(myMCimg, Align.LEFT, Align.BOTTOM).y;
- //Center object on stage on when app runs for the first time
- centerImage(myMCimg);
- //You can uncomment the below function call to show the object's current registration/pivot/anchor point
- //showPivot(myMCimg);
- // --- Click Zoom more:
- btMore.addEventListener(MouseEvent.ROLL_OUT, stopMoreZoom);
- btMore.addEventListener(MouseEvent.MOUSE_DOWN, moreZoom);
- function moreZoom(event: MouseEvent): void {
- clickMore = true;
- }
- btMore.addEventListener(MouseEvent.MOUSE_UP, stopMoreZoom);
- function stopMoreZoom(event: MouseEvent): void {
- clickMore = false;
- }
- // --- Click Zoom less:
- btLess.addEventListener(MouseEvent.ROLL_OUT, stopLessZoom);
- btLess.addEventListener(MouseEvent.MOUSE_DOWN, lessZoom);
- function lessZoom(event: MouseEvent): void {
- clickLess = true;
- }
- btLess.addEventListener(MouseEvent.MOUSE_UP, stopLessZoom);
- function stopLessZoom(event: MouseEvent): void {
- clickLess = false;
- }
- // --- Click Move left:
- btLeft.addEventListener(MouseEvent.ROLL_OUT, stopLeftMove);
- btLeft.addEventListener(MouseEvent.MOUSE_DOWN, leftMove);
- function leftMove(event: MouseEvent): void {
- clickLeft = true;
- }
- btLeft.addEventListener(MouseEvent.MOUSE_UP, stopLeftMove);
- function stopLeftMove(event: MouseEvent): void {
- clickLeft = false;
- }
- // --- Click Move right:
- btRight.addEventListener(MouseEvent.ROLL_OUT, stopRightMove);
- btRight.addEventListener(MouseEvent.MOUSE_DOWN, rightMove);
- function rightMove(event: MouseEvent): void {
- clickRight = true;
- }
- btRight.addEventListener(MouseEvent.MOUSE_UP, stopRightMove);
- function stopRightMove(event: MouseEvent): void {
- clickRight = false;
- }
- // --- Click Move up:
- btUp.addEventListener(MouseEvent.ROLL_OUT, stopUpMove);
- btUp.addEventListener(MouseEvent.MOUSE_DOWN, upMove);
- function upMove(event: MouseEvent): void {
- clickUp = true;
- }
- btUp.addEventListener(MouseEvent.MOUSE_UP, stopUpMove);
- function stopUpMove(event: MouseEvent): void {
- clickUp = false;
- }
- // --- Click Move Down:
- btDown.addEventListener(MouseEvent.ROLL_OUT, stopDownMove);
- btDown.addEventListener(MouseEvent.MOUSE_DOWN, downMove);
- function downMove(event: MouseEvent): void {
- clickDown = true;
- }
- btDown.addEventListener(MouseEvent.MOUSE_UP, stopDownMove);
- function stopDownMove(event: MouseEvent): void {
- clickDown = false;
- }
- // --- Click Move Center:
- btCenter.addEventListener(MouseEvent.ROLL_OUT, stopCenterMove);
- btCenter.addEventListener(MouseEvent.MOUSE_DOWN, centerMove);
- function centerMove(event: MouseEvent): void {
- clickCenter = true;
- }
- btCenter.addEventListener(MouseEvent.MOUSE_UP, stopCenterMove);
- function stopCenterMove(event: MouseEvent): void {
- clickCenter = false;
- }
- // --- Click Zoom Wheel:
- stage.addEventListener(MouseEvent.MOUSE_WHEEL, handleMouseWheel);
- function handleMouseWheel(event: MouseEvent): void {
- if(event.delta > 0) {
- //Going up
- resize(myMCimg, Scaling.UP, Math.abs(event.delta) * SIZE_SCALE, MIN_SCALE, MAX_SCALE);
- } else if(event.delta < 0){
- //Going down
- resize(myMCimg, Scaling.DOWN, Math.abs(event.delta) * SIZE_SCALE, MIN_SCALE, MAX_SCALE);
- }
- centerImage(myMCimg);
- trace("Using the mouse wheel!");
- // --- Actions:
- }
- addEventListener(Event.ENTER_FRAME, enterFrame);
- function enterFrame(event: Event): void {
- if (keys[Keyboard.LEFT]) {
- clickLeft = true;
- }
- if (keys[Keyboard.RIGHT]) {
- clickRight = true;
- }
- if (keys[Keyboard.UP]) {
- clickUp = true;
- }
- if (keys[Keyboard.DOWN]) {
- clickDown = true;
- }
- if (keys[Keyboard.ENTER]) {
- // --- Centralize:
- clickCenter = true;
- }
- if (clickMore == true) {
- resize(myMCimg, Scaling.UP, SIZE_SCALE, MIN_SCALE, MAX_SCALE);
- centerImage(myMCimg);
- }
- if ((clickLess == true)) {
- resize(myMCimg, Scaling.DOWN, SIZE_SCALE, MIN_SCALE, MAX_SCALE);
- centerImage(myMCimg);
- }
- if (clickLeft == true) {
- myMCimg.x -= MOVE_SPEED;
- }
- if (clickRight == true) {
- myMCimg.x += MOVE_SPEED;
- }
- if (clickUp == true) {
- myMCimg.y -= MOVE_SPEED;
- }
- if (clickDown == true) {
- myMCimg.y += MOVE_SPEED;
- }
- if (clickCenter == true) {
- // --- Centralize:
- myMCimg.width = DEFAULT_WIDTH;
- myMCimg.height = DEFAULT_HEIGHT;
- clickCenter = false;
- centerImage(myMCimg);
- }
- setChildIndex(myMCimg, 0);
- }
- // --- Click keys directions keyboard:
- stage.addEventListener(KeyboardEvent.KEY_DOWN, pressKeys);
- function pressKeys(event:KeyboardEvent): void {
- keys[event.keyCode] = true;
- }
- stage.addEventListener(KeyboardEvent.KEY_UP, releaseKeys);
- function releaseKeys(event:KeyboardEvent):void {
- keys[event.keyCode] = false;
- }
- // --- Click Drag:
- myMCimg.addEventListener(MouseEvent.MOUSE_DOWN, dragMove);
- function dragMove(event: MouseEvent): void {
- myMCimg.startDrag();
- }
- myMCimg.addEventListener(MouseEvent.MOUSE_UP, stopDragMove);
- function stopDragMove(event: MouseEvent): void {
- stopDrag();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement