In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

ScreenWrapTest

use keyboard : ↑ ↓ → ←
Get Adobe Flash player
by 0xABCDEF 06 Feb 2011
    Embed
/**
 * Copyright 0xABCDEF ( http://wonderfl.net/user/0xABCDEF )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/2NB4
 */

package
{
    import flash.display.Sprite;
    import flash.display.DisplayObject;
    import flash.geom.Rectangle;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.ui.Keyboard;
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.display.Graphics;
    import flash.events.MouseEvent;
    [ SWF( backgroundColor=0x000000, frameRate=60, width=465, height=465 ) ]
    public class ScreenWrapTest extends Sprite
    {
        private var dx:Number;
        private var dy:Number;
        private var speed:Number;
        private var friction:Number;
        private var screen:Rectangle;
        private var downList:Vector.< Boolean >;
        private var character:Shape;
        private var p0:Sprite;
        private var p1:Sprite;
        
        public function ScreenWrapTest()
        {
            init();
        }

        private function screenWrap( target:DisplayObject, screen:Rectangle ):void
        {
            target.x = ( target.x-screen.x+screen.width )%screen.width+screen.x;
            target.y = ( target.y-screen.y+screen.height )%screen.height+screen.y;
        }
        
        private function init():void
        {
            stage.addEventListener( KeyboardEvent.KEY_DOWN, KEY_DOWN );
            stage.addEventListener( KeyboardEvent.KEY_UP, KEY_UP );
            addEventListener( Event.ENTER_FRAME, ENTER_FRAME );
            speed = 1;
            friction = 0.05;
            dx = 0;
            dy = 0;
            screen = new Rectangle( 100, 100, stage.stageWidth-200, stage.stageHeight-200 );
            downList = new Vector.< Boolean >( 250, true );
            character = addChild( new Shape ) as Shape;
            drawCircle( character.graphics, 0xFF, 20 );
            p0 = addChild( new Sprite ) as Sprite;
            p1 = addChild( new Sprite ) as Sprite;
            drawCircle( p0.graphics );
            drawCircle( p1.graphics );
            p0.addEventListener( MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
            p0.addEventListener( MouseEvent.MOUSE_UP, MOUSE_UP );
            p1.addEventListener( MouseEvent.MOUSE_DOWN, MOUSE_DOWN );
            p1.addEventListener( MouseEvent.MOUSE_UP, MOUSE_UP );
            p0.x = screen.x;
            p0.y = screen.y;
            p1.x = screen.x+screen.width;
            p1.y = screen.y+screen.height;
        }
        
        private function drawScreen():void
        {
            graphics.lineStyle( 1, 0xFF0000 );
            graphics.drawRect( screen.x, screen.y, screen.width, screen.height );
        }
        
        private function drawCircle( graphics:Graphics, color:uint = 0xFF0000, size:Number = 10 ):void
        {
            with( graphics )
            {
                beginFill( color );
                drawCircle( 0, 0, size );
            }
        }
        
        private function MOUSE_DOWN( e:MouseEvent ):void
        {
            Sprite( e.target ).startDrag();
        }
        
        private function MOUSE_UP( e:MouseEvent ):void
        {
            Sprite( e.target ).stopDrag();
        }
        
        private function KEY_DOWN( e:KeyboardEvent ):void
        {
            downList[ e.keyCode ] = true;
        }
        private function KEY_UP( e:KeyboardEvent ):void
        {
            downList[ e.keyCode ] = false;
        }
        
        private function ENTER_FRAME( e:Event ):void
        {
            if( downList[ Keyboard.LEFT ] ) dx -= speed;
            if( downList[ Keyboard.RIGHT ] ) dx += speed;
            if( downList[ Keyboard.UP] ) dy -= speed;
            if( downList[ Keyboard.DOWN ] ) dy += speed;
            character.x += dx;
            character.y += dy;
            dx *= 1-friction;
            dy *= 1-friction;
            screen.x = p0.x;
            screen.y = p0.y;
            screen.right = p1.x;
            screen.bottom = p1.y;
            screenWrap( character, screen );
            graphics.clear();
            drawScreen();
        }
    }

}