跳ね返り3D

by _wonder forked from ボールを動かす3D (diff: 34)
♥0 | Line 108 | Modified 2010-07-30 12:48:09 | MIT License
play

ActionScript3 source code

/**
 * Copyright _wonder ( http://wonderfl.net/user/_wonder )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6Jtf
 */

// forked from _wonder's ボールを動かす3D
// forked from _wonder's 遠ざかるボール
// forked from _wonder's base
package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    
    public class Perspective extends Sprite {
        private var ball:Ball;
        private var xpos:Number = 0;
        private var ypos:Number = 0;
        private var zpos:Number = 0;
        private var vx:Number = Math.random()*10-5;
        private var vy:Number = Math.random()*10-5;
        private var vz:Number = Math.random()*10-5;
        private var friction:Number = 0.98;
        private var fl:Number = 250;
        private var vpX:Number = stage.stageWidth / 2;
        private var vpY:Number = stage.stageHeight / 2;
        private var top:Number = -100;
        private var bottom:Number = 100;
        private var left:Number = -100;
        private var right:Number = 100;
        private var front:Number = 100;
        private var back:Number = -100;
        
        public function Perspective() {
            ball = new Ball();
            addChild( ball );
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        }
        
        private function onEnterFrame(e:Event):void {
            xpos += vx;
            ypos += vy;
            zpos += vz;
            
            var radius:Number = ball.radius;
            if( xpos + radius > right ){
                xpos = right - radius;
                vx *= -1;
            } else if ( xpos - radius < left ){
                xpos = left + radius;
                vx *= -1;
            }
            
            if( ypos + radius > bottom ){
                ypos = bottom - radius;
                vy *= -1;
            } else if ( ypos - radius < top ){
                ypos = top + radius;
                vy *= -1;
            }
            
            if( zpos + radius > front ){
                zpos = front - radius;
                vz *= -1;
            } else if ( zpos - radius < back ){
                zpos = back + radius;
                vz *= -1;
            }
            
            if( zpos > -fl ){
                var scale:Number = fl / ( fl + zpos );
                ball.scaleX = ball.scaleY = scale;
                ball.x = vpX + xpos * scale;
                ball.y = vpX + ypos * scale;
                ball.visible = true;
            } else {
                ball.visible = false;
            }

        }
        
        private function onKeyDown(e:KeyboardEvent):void {
            switch( e.keyCode ){
                case Keyboard.UP:
                    vy -= 1;
                    break;
                case Keyboard.DOWN:
                    vy += 1;
                    break;
                case Keyboard.LEFT:
                    vx -= 1;
                    break;
                case Keyboard.RIGHT:
                    vx += 1;
                    break;
                case Keyboard.SHIFT:
                    vz += 1;
                    break;
                case Keyboard.CONTROL:
                    vz -= 1;
                    break;
                default:
                    break;
            }
        }
    }
}

import flash.display.Sprite;

class Ball extends Sprite {
    public var radius:Number;
    public var color:uint;
    public var vx:Number = 0;
    public var vy:Number = 0;
    
    public function Ball(radius:Number=40, color:uint=0Xff0000){
        this.radius = radius;
        this.color = color;
        init();
    }
    
    public function init():void {
        graphics.beginFill(color);
        graphics.drawCircle(0, 0, radius);
        graphics.endFill();
    }
}

Forked