as3初級者です。
最近packageの外にクラスを書くのが大好きなのですが、
その練習で時計を作成したので載せてみます。
動きがイマイチなのですが、クラスの練習なのでご容赦下さい。
全体が見やすくなるみたいです。
packageの外にクラスを書く練習2 – wonderfl build flash online
時計を表示する仕組みとしては、
まず、TextFieldをBitmapDataにdrawします。
[sourcecode language=”as3″]
var _bitmapData:BitmapData = new BitmapData(30, 30, false, 0xcFFFFF);
_bitmapData.draw(_tf);
var _bitmap:Bitmap = new Bitmap(_bitmapData);
[/sourcecode]
そして、getPixelによってピクセル毎の色を調べ、
黒が返ってくる座標を配列に記憶しています。
色は使い慣れている16進数にしてあります。
[sourcecode language=”as3″]
pixelValue = _bitmapData.getPixel(i, j);
pixelValue16 = pixelValue.toString(16);
if (pixelValue16 == "0") {
_pixelsX.push(i * 7);
_pixelsY.push(j * 7);
}
[/sourcecode]
全体のコード↓
もう少しすっきりさせれるように頑張ります。
[sourcecode language=”as3″]
package {
import flash.display.*;
import flash.events.*;
import flash.text.TextField;
import flash.text.TextFormat;
/**
* …
* @author masaoka
*/
[SWF(frameRate = "30", width = "465", height = "465")]
public class Main extends Sprite {
private var _pixelsX:Array;
private var _pixelsY:Array;
private var _canvasSec01:Sprite;
private var _canvasSec10:Sprite;
private var _canvasMin01:Sprite;
private var _canvasMin10:Sprite;
private var _canvashour01:Sprite;
private var _canvashour10:Sprite;
private var _sec1:String;
private var _sec10:String;
private var _min1:String;
private var _min10:String;
private var _hour1:String
private var _hour10:String;
private var _preSec01:Number;
private var _preSec10:Number;
private var _preMin01:Number;
private var _preMin10:Number;
private var _prehour01:Number;
private var _prehour10:Number;
public function Main() {
_canvasSec01 = new Sprite();
_canvasSec10 = new Sprite();
_canvasMin01 = new Sprite();
_canvasMin10 = new Sprite();
_canvashour01 = new Sprite();
_canvashour10 = new Sprite();
this.addChild(_canvasSec01);
this.addChild(_canvasSec10);
this.addChild(_canvasMin01);
this.addChild(_canvasMin10);
this.addChild(_canvashour01);
this.addChild(_canvashour10);
this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void {
var date:Date = new Date();
var sec:uint = date.getSeconds();
var min:uint = date.getMinutes();
var hour:uint = date.getHours();
var hour10:uint = Math.floor(hour / 10);
var hour1:uint = hour – hour10 * 10;
var min10:uint = Math.floor(min / 10);
var min1:uint = min – min10 * 10;
var sec10:uint = Math.floor(sec / 10);
var sec1:uint = sec – sec10 * 10;
_hour10 = new String(hour10);
_hour1 = new String(hour1);
_min10 = new String(min10);
_min1 = new String(min1);
_sec10 = new String(sec10);
_sec1 = new String(sec1);
if (hour10 != _prehour10) {
this.removeChild(_canvashour10);
_canvashour10 = new Sprite();
creatBalls(_hour10, _canvashour10, -20, 100);
}
_prehour10 = hour10;
if (hour1 != _prehour01) {
this.removeChild(_canvashour01);
_canvashour01 = new Sprite();
creatBalls(_hour1, _canvashour01, 45, 100);
}
_prehour01 = hour1;
if (min10 != _preMin10) {
this.removeChild(_canvasMin10);
_canvasMin10 = new Sprite();
creatBalls(_min10, _canvasMin10, 130, 100);
}
_preMin10 = min10;
if (min1 != _preMin01) {
this.removeChild(_canvasMin01);
_canvasMin01 = new Sprite();
creatBalls(_min1, _canvasMin01, 195, 100);
}
_preMin01 = min1;
if (sec10 != _preSec10) {
this.removeChild(_canvasSec10);
_canvasSec10 = new Sprite();
creatBalls(_sec10, _canvasSec10, 280, 100);
}
_preSec10 = sec10;
if (sec1 != _preSec01) {
this.removeChild(_canvasSec01);
_canvasSec01 = new Sprite();
creatBalls(_sec1, _canvasSec01, 345, 100);
}
_preSec01 = sec1;
}
private function creatBalls(str:String, canvas:Sprite, _x:Number, _y:Number):void {
getColor(str);
var pixelsLength:uint = _pixelsX.length;
var i:uint;
var j:uint;
var ball:Ball;
for (i = 0; i < pixelsLength; i++ ) {
canvas.addChild(new Ball(_pixelsX[i], _pixelsY[i]));
}
this.addChild(canvas);
canvas.x = _x;
canvas.y = _y;
}
private function getColor(str:String):void {
_pixelsX = [];
_pixelsY = [];
var format:TextFormat = new TextFormat();
format.size = 25;
format.bold = true;
format.font = "_typewriter"
var _tf:TextField = new TextField();
_tf.defaultTextFormat = format;
_tf.text = str;
var _bitmapData:BitmapData = new BitmapData(30, 30, false, 0xcFFFFF);
_bitmapData.draw(_tf);
var _bitmap:Bitmap = new Bitmap(_bitmapData);
var pixelValue:Number;
var pixelValue16:String;
var i:uint;
var j:uint;
for (i = 3; i <= 28; i++ ) {
for (j = 3; j <= 28; j++ ) {
pixelValue = _bitmapData.getPixel(i, j);
pixelValue16 = pixelValue.toString(16);
if (pixelValue16 == "0") {
_pixelsX.push(i * 7);
_pixelsY.push(j * 7);
}
}
}
this.addChild(_bitmap);
}
}
}
//円を描く
import flash.display.*;
import caurina.transitions.Tweener;
class Ball extends Sprite{
function Ball (_x:Number, _y:Number) {
this.graphics.beginFill(0xFF9900, 1 – 0.8 * Math.random());
this.graphics.drawCircle(0, 0, 10 – 5 * Math.random());
this.graphics.endFill();
this.scaleX = this.scaleY = 0;
this.x = _x + Math.random() * (5 – 10 * Math.random());
this.y = _y + Math.random() * (5 – 10 * Math.random());
Tweener.addTween(this, { scaleX:1, scaleY:1, time:0.2, delay:(0.7 – 0.5 * Math.random()), transition:"easeInOut" } );
}
}
[/sourcecode]