forked from: RotateGravity
...
@author kiyo, PePea
/**
* Copyright PePea_28 ( http://wonderfl.net/user/PePea_28 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/eaaX
*/
// forked from kamibana's RotateGravity
package
{
import flash.text.TextFieldAutoSize;
import flash.text.TextField;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Rectangle;
import flash.utils.Timer;
/**
* ...
* @author kiyo, PePea
*/
public class Inception extends Sprite
{
private const speedRotation:Number = 0.020;
private var ball:Ball = new Ball(30);
private var box:Box;
private var gravity:Number = 1;
private var gx:Number = 0;
private var gy:Number = 0;
private var speed:Number = 0.045;
private var bounce:Number = -0.85;
private var angle:Number = 0;
private var boxRect:Rectangle;
private var timer:Timer;
private var containerBox:MovieClip;
private var totalContainer:MovieClip;
private var debug:TextField;
public function Inception()
{
containerBox = new MovieClip();
totalContainer = new MovieClip();
box = new Box(stage.stageHeight - 50);
containerBox.addChild(box);
containerBox.addChild(ball);
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
boxRect = box.getRect(this);
addEventListener(Event.ENTER_FRAME, enterframeHandler);
containerBox.x = -box.width/2;
containerBox.y = -box.height/2;
totalContainer.addChild(containerBox);
totalContainer.x = stage.stageWidth/2;
totalContainer.y = stage.stageHeight/2;
this.addChild(totalContainer);
/* DEBUG */
debug = new TextField();
debug.autoSize = TextFieldAutoSize.LEFT;
this.addChild(debug);
}
private function enterframeHandler(e:Event):void
{
angle += speedRotation;
gx = gravity * Math.cos(+Math.PI/2-angle);
gy = gravity * Math.sin(+Math.PI/2-angle);
debug.text = '';//'gx : '+gx;
ball.vx += gx;
ball.vy += gy;
ball.x += ball.vx;
ball.y += ball.vy;
// gravity line
/*
containerBox.graphics.clear();
containerBox.graphics.lineStyle(1, 0x0000FF);
containerBox.graphics.moveTo(containerBox.width/2, containerBox.height/2);
containerBox.graphics.lineTo(containerBox.height/2 + 180 * gx,
containerBox.height/2 + 180 * gy );
//*/
checkBounce();
// box rotation
totalContainer.rotation += 57.4*speedRotation;
}
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;
}
}