月と太陽と地球と俺とお前と大五郎 forked from: flash on 2009-8-5
forked from 円の周りを動く円 (diff: 46)
月の公転方向ってどっちだっけ
ActionScript3 source code
/**
* Copyright undo ( http://wonderfl.net/user/undo )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/aeWb
*/
// forked from kamip's flash on 2009-8-5
// 月の公転方向ってどっちだっけ
package {
//円の弧度(?)とかを出すサンプル
//(数学とか分からない人なので間違っている場合はご指摘いただけると助かります)
//http://d.hatena.ne.jp/kamip/20090804
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(backgroundColor="0xffffff", width="465", height="465", frameRate="30")]
public class Ball extends Sprite {
//stageの中心点
private var _center_x:Number = stage.stageWidth / 2;
private var _center_y:Number = stage.stageHeight / 2;
//中心の円のSprite
private var _centerCircle:Sprite = new Sprite();
//周りを回ってる円のSprite
private var _moveCircle:Sprite = new Sprite();
private var _moveCircle_x:Number = 0;
private var _moveCircle_y:Number = 0;
//さらに周りを回ってる円のSprite
private var _moveCircle2:Sprite = new Sprite();
private var _moveCircle2_x:Number = 0;
private var _moveCircle2_y:Number = 0;
//半径
private var _radius:Number = 150;
private var _radius2:Number = 50;
//角度
private var _degree:Number = 0;
private var _degree2:Number = 0;
//弧度
private var _radian:Number = 0;
private var _radian2:Number = 0;
//回転方向
private var _type:Boolean = true;
private var _type2:Boolean = true;
public function Ball() {
init();
}
private function init():void {
//とりあえず周りを回ってる円をaddChildする
this.addChild(_moveCircle);
this.addChild(_moveCircle2);
//中心の円は動かないので座標固定でaddChildする
_centerCircle.x = _center_x;
_centerCircle.y = _center_y;
_centerCircle.graphics.beginFill(0xff0000, 1);
_centerCircle.graphics.drawCircle(0, 0, 30);
_centerCircle.graphics.endFill();
this.addChild(_centerCircle);
//毎フレームごとにイベントを発生させる
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
//周りを回ってる円のSprite(非動的なパラメーターはここで指定
_moveCircle.graphics.beginFill(0x0022ff, 1);
_moveCircle.graphics.drawCircle(0, 0, 15);
_moveCircle.graphics.endFill();
_moveCircle2.graphics.beginFill(0xbbbb00, 1);
_moveCircle2.graphics.drawCircle(0, 0, 10);
_moveCircle2.graphics.endFill();
}
//毎フレームごとに発生するイベント
private function onEnterFrame(e:Event):void {
//周りを回っている円のx,y座標を取得
_radian = Math.PI / 180 * _degree;
_moveCircle_x = _radius * Math.cos(_radian) + _center_x;
_moveCircle_y = _radius * Math.sin(_radian) + _center_y;
//取得したx,y座標に円を描く
_moveCircle.x = _moveCircle_x;
_moveCircle.y = _moveCircle_y;
//動かすために弧度に変化をつける
//数値を大きくすれば早く回る
if ( _type == true) {
_degree += 1;
}else {
_degree -= 1;
}
//周りを回っている円のx,y座標を取得
_radian2 = Math.PI / 180 * _degree2;
_moveCircle2_x = _radius2 * Math.cos(_radian2) + _moveCircle_x;
_moveCircle2_y = _radius2 * Math.sin(_radian2) + _moveCircle_y;
//取得したx,y座標に円を描く
_moveCircle2.x = _moveCircle2_x;
_moveCircle2.y = _moveCircle2_y;
//動かすために弧度に変化をつける
//数値を大きくすれば早く回る
if ( _type == true) {
_degree += 0.1;
_degree2 += 5;
}else {
_degree -= 0.1;
_degree2 -= 5;
}
}
//マウスでクリックされたら逆回転になる
//連打するとドッキリ大作戦みたいだよね
private function onMouseDown(e:MouseEvent):void {
if ( _type == true ) {
_type = false;
}else {
_type = true;
}
}
}
}
