ゲームとかで得点をした際の表現はいろいろあるけれども、加算する点数やカウントアップする様子を強調するものを作ってみた。ステージをクリックするたびにカウンターに加算されます。あと、はじめてGreenSockのTweenLiteを使ってみた。
tweenerは重過ぎるのでなるべく使いたくないし、BetweenAS3はFlashPlayerのバージョンを気にしないといけないことがあり、ちょっと使いにくい場面があったりするので、今後はGreenSockシリーズを使う場面を増やそうかと。
CountUp – wonderfl build flash online
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
package { import flash.display.Sprite; import flash.events.Event; /** * CountUp * ステージをクリックするたびにカウンターに加算されます。 * @author umhr */ public class WonderflMain extends Sprite { public function WonderflMain():void { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point addChild(new Canvas()); } } } import flash.display.Sprite; import flash.events.Event; import flash.events.MouseEvent; /** * ... * @author umhr */ class Canvas extends Sprite { private var _counter:Counter = new Counter(6); public function Canvas() { if (stage) init(); else addEventListener(Event.ADDED_TO_STAGE, init); } private function init(e:Event = null):void { removeEventListener(Event.ADDED_TO_STAGE, init); // entry point stage.addEventListener(MouseEvent.CLICK, onClick); _counter.x = (stage.stageWidth - _counter.width) * 0.5; _counter.y = 50; addChild(_counter); } private function onClick(e:MouseEvent):void { _counter.addCount(int(1000 * Math.random())); } } import com.greensock.easing.Sine; import com.greensock.TweenLite; import flash.display.Sprite; import flash.text.TextField; import flash.text.TextFormat; /** * ... * @author umhr */ class Counter extends Sprite { private var _count:Object = { score:0, current:0 }; private var _textFeild:TextField = new TextField(); private var _fixedLength:int; private var _digitNum:int; private var _isFixed:Boolean; private var _subCount:int = 0; public function Counter(fixedLength:int = 0) { _isFixed = (fixedLength == 0); this.fixedLength = fixedLength; init(); } private function init():void { _textFeild.defaultTextFormat = new TextFormat("_typewriter", 48, 0xFF0000); _textFeild.selectable = false; _textFeild.autoSize = "right"; addCount(0); _textFeild.x = 0; addChild(_textFeild); } public function addCount(num:int):void { var time:Number = Math.min(1, num / 30); _count.score += num; TweenLite.to(_count, time, { current:_count.score, onUpdate:onUpdate } ); if (_isFixed) { fixedLength = String(_count.score).length; } var subCounter:TextField = new TextField(); subCounter.defaultTextFormat = new TextFormat("_typewriter", 48, 0xFF0000); subCounter.selectable = false; subCounter.autoSize = "right"; subCounter.text = String(num); subCounter.x = _textFeild.x + _textFeild.width - subCounter.width; subCounter.y = 42 + _subCount * 28; addChild(subCounter); if (_subCount > 0) { time = 1; } TweenLite.to(subCounter, time, { y:0, alpha:0,onComplete:onComplete, onCompleteParams:[subCounter],ease:Sine.easeIn} ); _subCount ++; } private function onComplete(subCounter:TextField):void { _subCount --; removeChild(subCounter); } private function onUpdate():void { _textFeild.text = String(_digitNum + Math.floor(_count.current)).substr(1); } public function get fixedLength():int { return _fixedLength; } public function set fixedLength(value:int):void { _digitNum = Math.pow(10, value); _fixedLength = value; } } |