2009年07月17日 03:01
パーマリンク | http://www.kantenna.com/cgi-bin/mt/mt-tb.cgi/478
ここからがいよいよ本番か?
swfファイルに関連付けられたクラス。 swfがロードされると同時に自動的にインスタンスが生成される。
ドキュメントクラスを利用することで flaファイルのキーフレームはまっさらでOK。
flaファイルは画像管理用に使うようにすると いいのかな?
ボタンクリックで円の透明度を変更
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.events.MouseEvent;
public class DocSample extends Sprite {
var circle:Shape;
var circle_state:Boolean = true;
public function DocSample():void {
// ボタン作成
var bt:SimpleButton = createButton();
bt.x = stage.stageWidth / 2;
bt.y = (stage.stageHeight / 2) + 100;
// 円作成
this.circle = createCircle();
this.circle.x = stage.stageWidth / 2;
this.circle.y = stage.stageHeight / 2;
// イベント追加
bt.addEventListener(MouseEvent.CLICK, onClick);
// 表示リストに追加
addChild(bt);
addChild(this.circle);
}
private function createButton():SimpleButton {
var bt:SimpleButton = new SimpleButton();
var sp:Shape = new Shape();
sp.graphics.beginFill(0xCCCCCC);
sp.graphics.lineStyle(0, 0x000000);
sp.graphics.drawRect(0, 0, 100, 20);
sp.graphics.endFill();
bt.upState = sp;
bt.overState = sp;
bt.hitTestState = sp;
return bt;
}
private function createCircle():Shape {
var sp:Shape = new Shape();
sp.graphics.beginFill(0x99CCFF);
sp.graphics.lineStyle(0, 0x000000);
sp.graphics.drawCircle(0, 0, 50);
sp.graphics.endFill();
return sp;
}
/**
* ボタンクリックで
* 表示/非表示を繰り返す
*/
public function onClick(event:MouseEvent):void {
if (this.circle_state) {
this.circle.alpha = 0.2;
circle_state = false;
} else {
this.circle.alpha = 1;
circle_state = true;
}
}
}
}
「swfがロードされると同時に自動的にインスタンスが生成される。」って説明だと なんでこれが表示されるのか不思議だった。 「swfがロードされると同時にインスタンスが生成されてstageにaddChild()される」って感じっぽい。
ただ作成されたドキュメントクラスのインスタンスは イベントを検知できないっぽい。
最初はできないのかと思い、それらしきプロパティないか調べたけどどうもできない。 Spriteを継承してるし出来そうなんだけどできない。
マウスイベントを検出するにはSpriteとか作るとできる。
package {
import flash.display.Sprite;
import flash.display.Shape;
import flash.display.SimpleButton;
import flash.text.TextField;
import flash.events.MouseEvent;
public class DocSample2 extends Sprite {
var circle:Shape;
var circle_state:Boolean = true;
public function DocSample2():void {
// ボタン作成
var bt:Sprite = createSprite();
bt.x = stage.stageWidth / 2;
bt.y = (stage.stageHeight / 2) + 100;
// 円作成
this.circle = createCircle();
this.circle.x = stage.stageWidth / 2;
this.circle.y = stage.stageHeight / 2;
// インスタンスに色々と設定
this.buttonMode = true;
this.graphics.beginFill(0xAAAFFF);
this.graphics.drawRect(30, 200, 150, 170);
this.graphics.endFill();
this.opaqueBackground = 0xCFCFCF;
this.addEventListener(MouseEvent.CLICK, onClick);
// テキストフィールド作成
var tf:TextField = new TextField();
tf.autoSize = "left";
tf.appendText("this.parent : " + this.parent + "\n");
tf.appendText("this.visible : " + this.visible + "\n");
tf.appendText("this.alpha : " + this.alpha + "\n");
tf.appendText("this.mouseEnabled : " + this.mouseEnabled + "\n");
tf.appendText("this.getRect(this) : " + this.getRect(this) + "\n");
tf.appendText("this.opaqueBackground : " + this.opaqueBackground + "\n");
tf.appendText("this.buttonMode : " + this.buttonMode);
addChild(bt);
addChild(tf);
addChild(this.circle);
}
private function createSprite():Sprite {
var bt:Sprite = new Sprite();
bt.graphics.beginFill(0xCCCCCC);
bt.graphics.lineStyle(0, 0x000000);
bt.graphics.drawRect(0, 0, 100, 20);
bt.graphics.endFill();
return bt;
}
private function createCircle():Shape {
var sp:Shape = new Shape();
sp.graphics.beginFill(0x99CCFF);
sp.graphics.lineStyle(0, 0x000000);
sp.graphics.drawCircle(0, 0, 50);
sp.graphics.endFill();
return sp;
}
public function onClick(event:MouseEvent):void {
if (this.circle_state) {
this.circle.alpha = 0.2;
circle_state = false;
} else {
this.circle.alpha = 1;
circle_state = true;
}
}
}
}
/ 金額 : ¥ 2,940
/ 金額 : ¥ 7,350
1-5
6-10
/ 金額 : ¥ 3,465
/ 金額 : ¥ 3,150
/ 金額 : ¥ 2,699
/ 金額 : ¥ 3,497
/ 金額 : ¥ 3,675