ボールを投げる
物理学っぽい動きをする何かシリーズ1
物理の何かシリーズ。2D簡易バージョン
@author naoto koshikawa
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
[SWF(width = "465", height = "465", backgroundColor = "0xFFFFFF", frameRate = "30")]
/**
* 物理学っぽい動きをする何かシリーズ1
* 物理の何かシリーズ。2D簡易バージョン
*
* @author naoto koshikawa
*/
public class PhysicalSomething1 extends MovieClip
{
// _____________________________________________________ Property
/** 重力の設定 大きくすると重力アップ。小さくすると重力ダウン */
private const GRAVITY:Number = 0.9;
/** 跳ね返り係数。跳ね返った後、減るスピードの率 */
private const BOUNCE:Number = -0.7;
/** 摩擦係数。摩擦により減っていくスピードの率 */
private const FRICTIION:Number = 0.9;
/** animation target */
private var _target:Sprite;
/** speed */
private var _speed:Point;
/** accerator */
private var _acc:Point;
/** mouse point */
private var _mouse:Point;
// _____________________________________________________ Method
/**
* constructor
*/
public function PhysicalSomething1()
{
var txt:TextField = new TextField();
txt.text = "ドラッグするとそっち(ドラッグ時に出る赤い線方向)にぶっ飛びます。";
txt.autoSize = TextFieldAutoSize.CENTER;
txt.x = stage.stageWidth / 2 - txt.width / 2;
addChild(txt);
_speed = new Point();
_acc = new Point();
_mouse = new Point();
_target = new Particle();
_target.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
_target.x = Math.random() * (stage.stageWidth - _target.width) + _target.width / 2;
addChild(_target);
addEventListener(Event.ENTER_FRAME, enterFrameListener1);
}
// _____________________________________________________ Listener
/**
* MouseEvent.MOUSE_DOWN event listener
* @param event
*/
private function mouseDownListener(event:MouseEvent):void
{
_target.startDrag();
_mouse.x = mouseX;
_mouse.y = mouseY;
event.currentTarget.removeEventListener(event.type, arguments.callee);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpListener);
removeEventListener(Event.ENTER_FRAME, enterFrameListener1);
addEventListener(Event.ENTER_FRAME, enterFrameListener2);
}
/**
* MouseEvent.MOUSE_UP event listener
* @param event
*/
private function mouseUpListener(event:MouseEvent):void
{
_target.stopDrag();
event.currentTarget.removeEventListener(event.type, arguments.callee);
_target.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownListener);
removeEventListener(Event.ENTER_FRAME, enterFrameListener2);
addEventListener(Event.ENTER_FRAME, enterFrameListener1);
}
/**
* Event.ENTER_FRAME event listener
* @param event
*/
private function enterFrameListener1(event:Event):void
{
graphics.clear();
_speed.y += GRAVITY; // 重力
_speed.x *= FRICTIION; // 摩擦
_target.x += _speed.x;
_target.y += _speed.y;
// 衝突時には、反対方向のスピードを加算する
if (stage.stageWidth < _target.x + _target.width / 2)
{
// right
_target.x = stage.stageWidth - _target.width / 2;
_speed.x *= BOUNCE;
}
else if (_target.x - _target.width / 2 < 0)
{
// left
_target.x = _target.width / 2;
_speed.x *= BOUNCE;
}
if (stage.stageHeight < _target.y + _target.height / 2)
{
// bottom
_target.y = stage.stageHeight - _target.height / 2;
_speed.y *= BOUNCE;
}
else if (_target.y - _target.height / 2 < 0)
{
// top
_target.y = _target.height / 2;
_speed.y *= BOUNCE;
}
}
/**
* Event.ENTER_FRAME event listener
* during _target is dragging
* @param event
*/
private function enterFrameListener2(event:Event):void
{
_speed.x = _target.x - _mouse.x;
_speed.y = _target.y - _mouse.y;
// ドラッグ方向へ線分を描写
graphics.clear();
graphics.lineStyle(1, 0xFF0000);
graphics.moveTo(_target.x, _target.y);
graphics.lineTo(_mouse.x, _mouse.y);
}
}
}
import flash.display.Graphics;
import flash.display.MovieClip;
class Particle extends MovieClip {
// _____________________________________________________ Method
/**
* constructor
*/
public function Particle():void
{
var gr:Graphics = graphics;
gr.beginFill(0xCCCCCC * Math.random() + 0xCCCCCC, 0.9);
gr.drawCircle(0, 0, 20);
}
}