package {
import flash.display.*;
import flash.events.*;
import flash.utils.*;
//タイマーの利用
[SWF(width=240, height=240, backgroundColor=0xFFFFFF)]
public class TimerEx extends Sprite {
//画像
[Embed(source='sample.gif')]
private var IMG_SAMPLE:Class;
//変数
private var image:Bitmap;//イメージ
private var vx:int = 3; //X速度
private var vy:int = 4; //Y速度
//コンストラクタ
public function TimerEx() {
//イメージの追加
image = new IMG_SAMPLE();
image.x = 120;
image.y = 120;
addChild(image);
//タイマーの追加
var timer:Timer = new Timer(30, 0);
timer.addEventListener(TimerEvent.TIMER, onTick);
timer.start();
}
//タイマーイベントの処理
private function onTick(evt:TimerEvent):void {
image.x += vx;//X座標
image.y += vy;//Y座標
if (image.x + 57 < 0 || 240 < image.x + 57) vx = -vx;//X反転
if (image.y + 57 < 0 || 240 < image.y + 57) vy = -vy;//Y反転
}
}
} |