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

RotateGravity

インセプションみたいにならない
Get Adobe Flash player
by t2421 16 Dec 2010
/**
 * Copyright t2421 ( http://wonderfl.net/user/t2421 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/dqTb
 */

package  
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.utils.Timer;
    /**
     * ...
     * @author kiyo
     */
    public class Inception extends Sprite
    {
        private var ball:Ball = new Ball(30);
        private var box:Box;
        private var gravity:Number = 0.5;
        private var gx:Number = 0;
        private var gy:Number = 0;
        private var speed:Number = 0.045;
        private var bounce:Number = -0.95;
        private var angle:Number = 0;
        private var boxRect:Rectangle;
        private var timer:Timer;
        
        public function Inception() 
        {
            box = new Box(stage.stageHeight - 50);
            box.x = stage.stageWidth / 2 - box.width / 2;
            box.y = stage.stageHeight / 2 - box.height / 2;
            addChild(box);
            addChild(ball);
            ball.x = stage.stageWidth / 2;
            ball.y = stage.stageHeight / 2;
            
            boxRect = box.getRect(this);            
            addEventListener(Event.ENTER_FRAME, enterframeHandler);
            
        }
        
        private function enterframeHandler(e:Event):void 
        {
            gx = gravity * Math.sin(angle);
            gy = gravity * Math.cos(angle);
            ball.vx += gx;
            ball.vy += gy;
            ball.x += ball.vx;
            ball.y += ball.vy;
            angle += speed;
            checkBounce();
            
            
        }
        
        private function checkBounce():void
        {
            if (ball.x + ball.radius >= boxRect.right) {
                ball.vx = ball.vx*bounce;
                ball.x = boxRect.right - ball.radius;
            }
            
            if (ball.x - ball.radius <= boxRect.left) {
                ball.vx = ball.vx*bounce;
                ball.x = boxRect.left  + ball.radius;
            }
            
            if (ball.y+ball.radius >= boxRect.bottom) {
                ball.vy = ball.vy*bounce;
                ball.y = boxRect.bottom - ball.radius;
            }
            
            if (ball.y-ball.radius <= boxRect.top) {
                ball.vy = ball.vy*bounce;
                ball.y = boxRect.top + ball.radius;
            }
        }
        
    }

}

import flash.display.Sprite;
    /**
     * ...
     * @author kiyo
     */
    class Box extends Sprite
    {
        private var _length:Number;
        
        public function Box(length:Number=10) 
        {
            _length = length;
            update();
        }
        
        private function update():void
        {
            graphics.clear();
            graphics.lineStyle(2);
            graphics.drawRect(0, 0, length, length);
            
        }
        public function setLength(num:Number):void
        {
            update();
        }
        
        public function get length():Number { return _length; }
        
    }
    
    
class Ball extends Sprite {
    public var vx:Number;
    public var vy:Number;
    private var _radius:Number;
    private var _color:Number;

    public function Ball(radius:Number = 10, color:uint = 0xff0000) {
        vx = 0;
        vy = 0;
        _radius=radius;
        _color=color;
        update();
    }

    private function update():void {
        graphics.clear();
        graphics.beginFill(color);
        graphics.drawCircle(0,0,radius);
        graphics.endFill();
    }

    public function setRadius(radius:Number):void {
        _radius=radius;
        update();
    }

    public function setColor(color:uint):void {
        _color=color;
        update();
    }

    public function get radius():Number {
        return _radius;
    }

    public function get color():Number {
        return _color;
    }
}