BetweenAS3 test(1)
ステージをクリックすると、そこからトランジションっぽい動きを開始します
// ステージをクリックすると、そこからトランジションっぽい動きを開始します
package {
/**
* @author YOSHIDA, Akio
*/
import flash.display.Sprite;
import net.hires.debug.Stats;
[SWF(width = "465", height = "465", frameRate = "60", backgroundColor = "#ffffff")]
public class Main extends Sprite {
public function Main() {
const WIDTH_OF_PIECE:Number = 7.5;
const HEIGHT_OF_PIECE:Number = 7.5;
const NUM_OF_COL:uint = 62;
const NUM_OF_ROW:uint = 62;
var subject:Subject = new Subject();
addChild(subject);
subject.setSizeOfTile(WIDTH_OF_PIECE, HEIGHT_OF_PIECE);
subject.setNumOfEdge(NUM_OF_COL, NUM_OF_ROW);
subject.build();
subject.x = (stage.stageWidth - subject.width) / 2;
subject.y = (stage.stageHeight - subject.height) / 2;
addChild(new Stats());
}
}
}
import flash.display.Sprite;
import flash.events.MouseEvent;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.tweens.ITween;
class Subject extends Sprite {
private var _observable:Observable; // オブザーバブル・オブジェクト
private var widthOfTile:Number = 20; // Tile 幅
private var heightOfTile:Number = 20; // Tile 高
private var numOfCol:uint = 5; // ステージ横方向に並べる Tile の数
private var numOfRow:uint = 5; // ステージ縦方向に並べる Tile の数
private var aryTween:Array;
public function Subject() {
_observable = new Observable();
_observable.subject = this;
mouseChildren = false;
}
// Tile の幅と高をセット
public function setSizeOfTile(widthOfTile:Number, heightOfTile:Number):void {
if (widthOfTile != 0) {
this.widthOfTile = widthOfTile;
}
if (heightOfTile != 0) {
this.heightOfTile = heightOfTile;
}
}
// Tile の縦横それぞれの配置数をセット
public function setNumOfEdge(numOfCol:uint, numOfRow:uint):void {
if (numOfCol != 0) {
this.numOfCol = numOfCol;
}
if (numOfRow != 0) {
this.numOfRow = numOfRow;
}
}
// Tile の生成と配置
public function build():void {
for (var i:int = 0; i < numOfRow; i++) {
for (var j:int = 0; j < numOfCol; j++) {
var tile:Tile = new Tile(widthOfTile, heightOfTile, j, i);
_observable.addObserver(tile);
addChild(tile);
}
}
stage.addEventListener(MouseEvent.CLICK, clickHandler);
}
private function clickHandler(e:MouseEvent):void {
var targetX:uint = mouseX / widthOfTile; // 小数点以下切り捨てのため uint
var targetY:uint = mouseY / heightOfTile; // 同上
aryTween = [];
_observable.noitfyObservers([targetX, targetY]);
var tween:ITween = BetweenAS3.parallel.apply(null, aryTween);
tween.play();
}
// Tile オブジェクトから呼び出される
public function addTween(tile:Tile):void {
var scale:Number = tile.scale;
var delay:Number = tile.delay / 25;
var tween:ITween = BetweenAS3.tween(tile, { scaleX:scale, scaleY:scale }, null, 0.5, null, delay);
aryTween.push(tween);
}
}
import flash.display.Shape;
import flash.display.Sprite;
class Tile extends Sprite implements IObserver {
private var widthOfThis:Number; // 幅
private var heightOfThis:Number;// 高
private var myPositionX:uint; // 配置位置(X座標上何番目か)
private var myPositionY:uint; // 配置位置(Y座標上何番目か)
private var _myDelay:Number;
private var _myScale:Number;
private var flg:Boolean;
public function Tile(widthOfThis:Number, heightOfThis:Number, myPositionX:uint, myPositionY:uint) {
this.widthOfThis = widthOfThis;
this.heightOfThis = heightOfThis;
this.myPositionX = myPositionX;
this.myPositionY = myPositionY;
x = widthOfThis * (myPositionX + 0.5);
y = heightOfThis * (myPositionY + 0.5);
draw();
}
public function update(observable:Observable, arg:Object = null):void {
// push 型 Observable
var targetPositionX:uint = arg[0]; // クリックされた Tile の場所(X座標上)
var targetPositionY:uint = arg[1]; // クリックされた Tile の場所(Y座標上)
// delay 計算
var delayX:int = myPositionX - targetPositionX;
var delayY:int = myPositionY - targetPositionY;
if (delayX < 0) { delayX *= -1; }
if (delayY < 0) { delayY *= -1; }
_myDelay = delayX + delayY;
//_myDelay = (delayX > delayY) ? delayX : delayY;
_myScale = (flg) ? 1 : 0;
flg = !flg;
observable.subject.addTween(this);
}
private function draw():void {
var drawWidth:Number = widthOfThis;
var drawHeight:Number = heightOfThis;
graphics.clear();
graphics.beginFill(0xcc0033);
graphics.drawRect(-drawWidth / 2, -drawHeight / 2, drawWidth, drawHeight);
graphics.endFill();
}
public function get scale():Number { return _myScale; }
public function get delay():Number { return _myDelay; }
}
import flash.utils.Dictionary;
class Observable {
private var _subject:Subject;
private var dictObservers:Dictionary = new Dictionary();
public function Observable() {}
// オブザーバー登録
public function addObserver(observer:IObserver):void {
dictObservers[observer] = true;
}
// オブザーバー解除
public function removeObserver(observer:IObserver):void {
delete dictObservers[observer];
}
// 登録オブザーバーへの通知
public function noitfyObservers(arg:Object = null):void {
for (var key:Object in dictObservers) {
var observer:IObserver = IObserver(key);
observer.update(this, arg);
}
}
public function get subject():Subject { return _subject; }
public function set subject(value:Subject):void { _subject = value; }
}
interface IObserver {
function update(observable:Observable, arg:Object = null):void;
}