drawHeart
メイン処理
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.Timer;
import flash.events.TimerEvent;
/**
* メイン処理
*/
public class Main extends Sprite
{
private const CENTER_X:Number = stage.stageWidth / 2;
private const CENTER_Y:Number = stage.stageHeight / 2;
public function Main():void
{
var timer:Timer = new Timer(120);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();
}
private function onTimer(e:Event):void {
var heart:Heart = new Heart(CENTER_X, CENTER_Y - 100);
stage.addChild(heart);
}
}
}
import flash.display.Sprite;
import flash.events.Event;
/**
* ハートクラス
*/
class Heart extends Sprite
{
private const BALL_RADIUS:Number = 7;
private const GRAVITY:Number = 0.2;
private const SPEED:Number = 4;
private const VALPHA:Number = 0.008;
private const CROSS_POINT:Number = 450;
private const TURNING_POINT:Number = 130;
private const TURNING_VX:Number = 0.28;
private var _right:Ball;
private var _left:Ball;
private var _startX:Number;
private var _startY:Number;
public function Heart(x:Number, y:Number) {
//開始ポジションをセット
_startX = x;
_startY = y;
//右側のボール生成
_right = createBall(SPEED);
addChild(_right);
//左側のボール生成
_left = createBall(SPEED * -1);
addChild(_left);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function createBall(vx:Number):Ball {
var ball:Ball = new Ball(BALL_RADIUS, Math.random() * 0xffffff);
ball.setPosition(_startX, _startY);
ball.vx = vx;
ball.vy = SPEED * -1;
ball.valpha = VALPHA;
return ball;
}
private function onEnterFrame(e:Event):void {
if (_right.returnX) _right.vx -= TURNING_VX;
if (_left.returnX) _left.vx += TURNING_VX;
move(_right);
move(_left);
}
private function move(ball:Ball):void {
if (ball.vy < 6) ball.vy += GRAVITY;
//折り返し
if (Math.abs(ball.x - _startX) > TURNING_POINT) ball.returnX = true;
//移動
ball.x += ball.vx;
ball.y += ball.vy;
//交差するY座標の450を超えたら透明
if (ball.y > CROSS_POINT) {
ball.alpha = 0;
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
} else {
ball.alpha -= ball.valpha;
}
}
}
/**
* ボールクラス
*/
class Ball extends Sprite
{
private var _radius:Number;
private var _color:uint;
private var _vx:Number = 0;
private var _vy:Number = 0;
private var _valpha:Number = 0;
private var _returnX:Boolean = false;
public function Ball(radius:Number = 40, color:uint = 0x0000ff) {
this._radius = radius;
this._color = color;
init();
}
private function init():void {
graphics.beginFill(_color);
graphics.drawCircle(0, 0, _radius);
graphics.endFill();
}
public function setPosition(x:Number, y:Number):void {
this.x = x;
this.y = y;
}
public function set radius( value:Number ):void{
_radius= value;
}
public function get radius():Number{
return _radius;
}
public function set vx( value:Number ):void{
_vx = value;
}
public function get vx():Number{
return _vx;
}
public function set vy( value:Number ):void{
_vy = value;
}
public function get vy():Number{
return _vy;
}
public function set returnX( value:Boolean ):void{
_returnX = value;
}
public function get returnX():Boolean{
return _returnX;
}
public function set valpha( value:Number ):void{
_valpha = value;
}
public function get valpha():Number{
return _valpha;
}
}