単なるゲージのテストです。
/**
* Copyright kyab ( http://wonderfl.net/user/kyab )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/2p7e
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
public class Main extends Sprite{
public function Main():void{
var gauge:MyGauge = new MyGauge(100,20, 0xFF22FF);
gauge.value = 30;
gauge.x = 10;
gauge.y = 100;
addChild(gauge);
//update value in some interval
var isUpping:Boolean = true;
var skipCount:int = 0;
var timer:Timer = new Timer(20);
timer.addEventListener(TimerEvent.TIMER, function(evt:TimerEvent):void {
if (skipCount > 0) {
skipCount-- ;
return; //skip update
}
//値を上げたり下げたり。もうちょっと上手くかけないかな?
var newVal:int;
if (isUpping) {
newVal = gauge.value + 2;
if (newVal > 100) {
isUpping = false;
skipCount = 10;
}else {
gauge.value = newVal;
}
}else {
newVal = gauge.value - 2;
if ( newVal < 0) {
isUpping = true;
skipCount = 10;
}else {
gauge.value = newVal;
}
}
});
timer.start();
}
}
}
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.utils.Timer;
//横向きのゲージ。 値(value)は0~100の間。
class MyGauge extends Sprite
{
private var _value:uint;
private var _valueRect:Shape;
private var _width:uint;
private var _height:uint;
private var _color:uint; //color of value bar
public function MyGauge(width:uint, height:uint, color:uint)
{
_value = 0;
_width = width;
_height = height;
_color = color;
//draw background(with black)
var _back:Shape = new Shape;
with(_back){
graphics.beginFill(0x112233);
graphics.drawRect(0, 0, _width, _height);
graphics.endFill();
}
this.addChild(_back);
//init valueRect:
_valueRect = new Shape();
with(_valueRect){
_valueRect.graphics.beginFill(_color);
_valueRect.graphics.drawRect(0, 0, _width, _height);
_valueRect.graphics.endFill();
}
this.addChild(_valueRect);
}
public function get value():uint {
return _value;
}
public function set value(val:uint):void {
_value = val;
updateValuRect();
}
private function updateValuRect() : void {
_valueRect.scaleX = _value / 100.0;
}
}