ころころボール
ころころボール
/**
* Copyright ProjectNya ( http://wonderfl.net/user/ProjectNya )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/fwmh
*/
////////////////////////////////////////////////////////////////////////////////
// ころころボール
////////////////////////////////////////////////////////////////////////////////
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Rectangle;
import flash.events.Event;
import flash.events.MouseEvent;
[SWF(backgroundColor="#FFFFFF", width="465", height="465", frameRate="30")]
public class Main extends Sprite {
private var ball0:Ball;
private var ball1:Ball;
private var ball2:Ball;
private var ball3:Ball;
private var ball4:Ball;
private var ball5:Ball;
//private var angle:Number = 0;
private static var radian:Number = Math.PI/180;
private var playBtn:Btn;
private var stopBtn:Btn;
public function Main() {
init();
}
private function init():void {
graphics.beginFill(0xEEEEEE);
graphics.drawRect(0, 140, 465, 325);
graphics.endFill();
//
ball0 = new Ball(20, 0x666666);
addChild(ball0);
ball0.x = 232;
ball0.y = 140;
ball1 = new Ball(25, 0xFF0066);
addChild(ball1);
ball1.x = 232;
ball1.y = 170;
ball2 = new Ball(30, 0xFF9900);
addChild(ball2);
ball2.x = 232;
ball2.y = 206;
ball3 = new Ball(35, 0x339900);
addChild(ball3);
ball3.x = 232;
ball3.y = 248;
ball4 = new Ball(40, 0x3366CC);
addChild(ball4);
ball4.x = 232;
ball4.y = 296;
ball5 = new Ball(45, 0xFFFFFF);
addChild(ball5);
ball5.x = 232;
ball5.y = 350;
//
playBtn = new Btn();
addChild(playBtn);
playBtn.x = 192;
playBtn.y = 440;
playBtn.init({label: "play"});
playBtn.addEventListener(MouseEvent.CLICK, play, false, 0, true);
stopBtn = new Btn();
addChild(stopBtn);
stopBtn.x = 272;
stopBtn.y = 440;
stopBtn.init({label: "stop"});
stopBtn.addEventListener(MouseEvent.CLICK, stop, false, 0, true);
stopBtn.enabled = false;
}
private function play(evt:MouseEvent):void {
playBtn.clicked = true;
stopBtn.enabled = true;
addEventListener(Event.ENTER_FRAME, update, false, 0, true);
}
private function stop(evt:MouseEvent):void {
playBtn.clicked = false;
stopBtn.enabled = false;
removeEventListener(Event.ENTER_FRAME, update);
}
private function update(evt:Event):void {
//angle += 2;
ball0.angle += 2;
ball1.angle -= 2;
ball2.angle += 2;
ball3.angle -= 2;
ball4.angle += 2;
ball5.angle -= 2;
ball0.x = 232 + 120*Math.sin(ball0.angle*radian);
ball1.x = 232 + 150*Math.sin(ball1.angle*radian);
ball2.x = 232 + 180*Math.sin(ball2.angle*radian);
ball3.x = 232 + 210*Math.sin(ball3.angle*radian);
ball4.x = 232 + 240*Math.sin(ball4.angle*radian);
ball5.x = 232 + 270*Math.sin(ball5.angle*radian);
}
}
}
//////////////////////////////////////////////////
// Ballクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.filters.DropShadowFilter;
import flash.filters.GlowFilter;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.InterpolationMethod;
import flash.display.BlendMode;
class Ball extends Sprite {
private var radius:uint;
private var color:uint;
private var ball0:Sprite;
private var base:Shape;
private var shadow:Shape;
private var light:Shape;
private var reflection:Shape;
private var shade:Shape;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
public var angle:Number = 0;
public function Ball(r:uint, c:uint) {
radius = r;
color = c;
draw();
}
private function draw():void {
shade = new Shape();
addChild(shade);
shade.y = radius;
createShade();
ball0 = new Sprite();
addChild(ball0);
base = new Shape();
ball0.addChild(base);
shadow = new Shape();
ball0.addChild(shadow);
light = new Shape();
ball0.addChild(light);
reflection = new Shape();
ball0.addChild(reflection);
createBase();
createShadow();
createLight();
createReflect();
}
private function createBase():void {
base.graphics.clear();
base.graphics.beginFill(color, 0.8);
base.graphics.drawCircle(0, 0, radius);
base.graphics.endFill();
}
private function createShadow():void {
var colors:Array = [sColor, sColor, sColor];
var alphas:Array = [0, 0.2, 0.3];
var ratios:Array = [0, 191, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(radius*3.2, radius*3.2, 0, -radius*2, -radius*2);
shadow.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
shadow.graphics.drawCircle(0, 0, radius);
shadow.graphics.endFill();
shadow.blendMode = BlendMode.HARDLIGHT;
}
private function createLight():void {
var colors:Array = [bColor, bColor, bColor];
var alphas:Array = [1, 0.2, 0];
var ratios:Array = [0, 191, 255];
var matrix:Matrix = new Matrix();
matrix.createGradientBox(radius*3.2, radius*3.2, 0, -radius*2, -radius*2);
light.graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
light.graphics.drawCircle(0, 0, radius);
light.graphics.endFill();
light.blendMode = BlendMode.OVERLAY;
}
private function createReflect():void {
var colors:Array = [bColor, bColor];
var alphas:Array = [0.7, 0];
var ratios:Array = [0, 191];
var matrix:Matrix = new Matrix();
var w:Number = radius*1.44;
var h:Number = radius*1.35;
var yOffset:Number = radius*0.95;
matrix.createGradientBox(w, h, 0.5*Math.PI, -w*0.5, -yOffset);
reflection.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, matrix, SpreadMethod.PAD, InterpolationMethod.RGB, 0);
reflection.graphics.drawEllipse(-w*0.5, -yOffset, w, h);
reflection.graphics.endFill();
}
private function createShade():void {
shade.graphics.beginFill(sColor, 0.6);
shade.graphics.drawEllipse(-radius*0.75, -radius*0.1775, radius*1.5, radius*0.375);
shade.graphics.endFill();
var shadow:DropShadowFilter = new DropShadowFilter(0, 90, sColor, 0.5, radius*0.15, radius*0.15, 1, 3, false, false, true);
shade.filters = [shadow];
}
}
//////////////////////////////////////////////////
// Btnクラス
//////////////////////////////////////////////////
import flash.display.Sprite;
import flash.display.Shape;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.text.AntiAliasType;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.filters.GlowFilter;
import flash.events.MouseEvent;
class Btn extends Sprite {
public var id:uint;
private var shade:Shape;
private var bottom:Shape;
private var light:Shape;
private var base:Shape;
private var txt:TextField;
private var label:String = "";
private static var fontType:String = "_ゴシック";
private var _width:uint = 60;
private static var _height:uint = 20;
private static var corner:uint = 5;
private var type:uint = 1;
private static var bColor:uint = 0xFFFFFF;
private static var sColor:uint = 0x000000;
private static var upColor:uint = 0x666666;
private static var overColor:uint = 0x333333;
private static var offColor:uint = 0x999999;
private static var gColor:uint = 0x0099FF;
private var blueGlow:GlowFilter;
private var shadeGlow:GlowFilter;
private var _clicked:Boolean = false;
private var _enabled:Boolean = true;
public function Btn() {
}
public function init(option:Object):void {
if (option.id != undefined) id = option.id;
if (option.label != undefined) label = option.label;
if (option.width != undefined) _width = option.width;
if (option.type != undefined) type = option.type;
draw();
}
private function draw():void {
switch (type) {
case 1 :
bColor = 0xFFFFFF;
sColor = 0x000000;
upColor = 0x666666;
overColor = 0x333333;
offColor = 0x999999;
break;
case 2 :
bColor = 0x000000;
sColor = 0xFFFFFF;
upColor = 0x666666;
overColor = 0x999999;
offColor = 0x333333;
break;
}
blueGlow = new GlowFilter(gColor, 0.6, 5, 5, 2, 3, false, true);
shadeGlow = new GlowFilter(sColor, 0.3, 4, 4, 2, 3, false, true);
shade = new Shape();
bottom = new Shape();
light = new Shape();
base = new Shape();
txt = new TextField();
addChild(shade);
addChild(bottom);
addChild(light);
addChild(base);
addChild(txt);
createBase(shade, _width, _height, corner, sColor);
shade.filters = [shadeGlow];
createBase(bottom, _width, _height, corner, sColor, 0.3);
createBase(light, _width, _height, corner, gColor);
light.filters = [blueGlow];
createBase(base, _width, _height, corner, bColor);
txt.x = -_width*0.5;
txt.y = -_height*0.5;
txt.width = _width;
txt.height = _height - 1;
txt.type = TextFieldType.DYNAMIC;
txt.selectable = false;
//txt.embedFonts = true;
//txt.antiAliasType = AntiAliasType.ADVANCED;
var tf:TextFormat = new TextFormat();
tf.font = fontType;
tf.size = 12;
tf.align = TextFormatAlign.CENTER;
txt.defaultTextFormat = tf;
txt.text = label;
enabled = true;
mouseChildren = false;
}
private function rollOver(evt:MouseEvent):void {
_over();
}
private function rollOut(evt:MouseEvent):void {
_up();
}
private function press(evt:MouseEvent):void {
_down();
}
private function release(evt:MouseEvent):void {
_up();
}
private function click(evt:MouseEvent):void {
}
private function _up():void {
txt.y = -_height*0.5;
txt.textColor = upColor;
base.y = -1;
light.visible = false;
light.y = -1;
}
private function _over():void {
txt.y = -_height*0.5;
txt.textColor = overColor;
base.y = -1;
light.visible = true;
light.y = -1;
}
private function _down():void {
txt.y = -_height*0.5 + 1;
txt.textColor = overColor;
base.y = 0;
light.visible = true;
light.y = 0;
}
private function _off():void {
txt.y = -_height*0.5 + 1;
txt.textColor = offColor;
base.y = 0;
light.visible = false;
light.y = 0;
}
public function get clicked():Boolean {
return _clicked;
}
public function set clicked(param:Boolean):void {
_clicked = param;
enabled = !_clicked;
if (_clicked) {
_down();
} else {
_up();
}
}
public function get enabled():Boolean {
return _enabled;
}
public function set enabled(param:Boolean):void {
_enabled = param;
buttonMode = _enabled;
mouseEnabled = _enabled;
useHandCursor = _enabled;
if (_enabled) {
_up();
addEventListener(MouseEvent.MOUSE_OVER, rollOver, false, 0, true);
addEventListener(MouseEvent.MOUSE_OUT, rollOut, false, 0, true);
addEventListener(MouseEvent.MOUSE_DOWN, press, false, 0, true);
addEventListener(MouseEvent.MOUSE_UP, release, false, 0, true);
addEventListener(MouseEvent.CLICK, click, false, 0, true);
} else {
_off();
removeEventListener(MouseEvent.MOUSE_OVER, rollOver);
removeEventListener(MouseEvent.MOUSE_OUT, rollOut);
removeEventListener(MouseEvent.MOUSE_DOWN, press);
removeEventListener(MouseEvent.MOUSE_UP, release);
removeEventListener(MouseEvent.CLICK, click);
}
}
private function createBase(target:Shape, w:uint, h:uint, c:uint, color:uint, alpha:Number = 1):void {
target.graphics.beginFill(color, alpha);
target.graphics.drawRoundRect(-w*0.5, -h*0.5, w, h, c*2);
target.graphics.endFill();
}
}