2008年10月29日水曜日

ActionScript 3.0で少し早いクリスマス気分を味わう

このブログ記事をはてなブックマークに追加

クリスマスあたりを目処に簡単なゲームをActionScript 3.0で作ろうかと思って、その雛型を作ってみた。雛型なんでホントに形だけだけど。それでも赤と緑の色合いとクリスマスソングで気分だけはクリスマスっぽくなるなぁ。それにしても未だにActionScript 3.0の使い方がこんなんで良いのか疑問に思う。なんだか無駄の多いコードのような気がする。

以下のFlashコンテンツをマウスクリックなどでアクティブにして適当なキーを押すと始まる。MP3だけで2.5MBあるので少し重いかも。尚、音が鳴るので注意すること。因みに飛んでいる天使をクリックすると…。


BGMにtaitai studioフリーMp3素材集からHappy Christmas Partyを利用させてもらった。また、効果音は効果音gから。フォントについてはdafont.comからクリスマス関連のフォントを使用した。

以下、ソースコード。

ActionXmas.as

// ActionXmas.as // by nox, 2008.10.29 package { import flash.display.*; import flash.text.*; import flash.events.*; import flash.media.Sound; import flash.media.SoundChannel; import flash.utils.Timer; public class ActionXmas extends Sprite { // taitai studio http://www.taitaistudio.com/ // taitai studio フリーMp3素材集 http://www.taitaistudio.com/mp3/ [Embed(source="xmas.mp3")] private static const SoundXmas:Class; // 効果音g http://sfxg.bufsiz.jp/index.html [Embed(source="slap01.mp3")] private static const SoundSlap:Class; // dafont.com http://www.dafont.com/ [Embed(systemFont="KR Christmas Dings 2004 Four", fontName="StFont", mimeType="application/x-font" )] private static const StFont:Class; [Embed(systemFont="Kingthings Christmas ", fontName="XmasFont", mimeType="application/x-font" )] private static const XmasFont:Class; [Embed(systemFont="Faux Snow BRK", fontName="SnowFont", mimeType="application/x-font" )] private static const SnowFont:Class; private static const version:String = "0.01"; private var bgm:Sound = new SoundXmas() as Sound; private var ss:Sound = new SoundSlap() as Sound; private var channel:SoundChannel; private var txtTitle:TextField = new TextField(); private var txtSt:TextField = new TextField(); private var txtFadeSt:TextField = new TextField(); private var txtXmas:Array = new Array(); private var txtXmasList:Array; private var txtXmasLength:int; private var txtXmasWidth:Number; private var txtXmasMargin:Number; private var txtSnow:Array = new Array(); private var txtSnowList:Array; private var speedSnow:Array = new Array(); private var numTxtSnow:int = 30; private var txtSnowMargin:Number = 300; private var timer:Timer; private var delta_y_st:Number = 0.5; private var XmasStageWidth:int, XmasStageHeight:int; private function ClickHandler(event:Event):void { ss.play(); // fade flying angel FadeSt(); } private function OnTick(event:TimerEvent):void { var i:int; // flying angel if (txtSt.y > XmasStageHeight / 10 + 10) delta_y_st = -0.5; else if (txtSt.y < XmasStageHeight / 10 - 10) delta_y_st = 0.5; txtSt.y += delta_y_st; TickFadeSt(); // walking background characters for (i = 0; i < txtXmasLength; i++) { txtXmas[i].x += 1; if (txtXmas[i].x > txtXmasWidth - txtXmasMargin) txtXmas[i].x -= txtXmasWidth; } // fall snow for (i = 0; i < numTxtSnow; i++) { txtSnow[i].y += 1 * speedSnow[i]; txtSnow[i].x += (Math.random() - 0.5) * 2 + 1; if (txtSnow[i].x > XmasStageWidth + txtSnowMargin) txtSnow[i].x -= XmasStageWidth + txtSnowMargin * 2; if (txtSnow[i].y > XmasStageHeight) NewSnow(i); } } private function FadeSt():void { with (txtFadeSt) { defaultTextFormat = txtSt.defaultTextFormat; autoSize = TextFieldAutoSize.CENTER; selectable = false; text = txtSt.text; x = txtSt.x; y = txtSt.y; embedFonts = true; alpha = 1.0; } addChild(txtFadeSt); removeChild(txtSt); } private function TickFadeSt():void { txtFadeSt.alpha *= 0.95; txtFadeSt.x = txtSt.x; txtFadeSt.y = txtSt.y; } private function OnPlaybackComplete(event:Event):void { txtSt.removeEventListener(MouseEvent.CLICK, ClickHandler); timer.stop(); // ending var format_red:TextFormat = new TextFormat(); var format_small:TextFormat = new TextFormat(); format_red.color = 0xCC0000; format_small.size = 48; with (txtTitle) { text = "Fin\nHit Any Key!"; autoSize = TextFieldAutoSize.LEFT; x = XmasStageWidth / 10; y = XmasStageHeight / 5; for each (var n:int in [0, 4, 8, 12]) setTextFormat(format_red, n); setTextFormat(format_small, 4, 16); } addChild(txtTitle); stage.addEventListener(KeyboardEvent.KEY_UP, StartGame); } private function SetFlyingAngel():void { var format:TextFormat = new TextFormat(); var format_red:TextFormat = new TextFormat(); with (format) { color = 0x00CC00; size = 72; align = TextFormatAlign.CENTER; font = "StFont"; } format_red.color = 0xCC0000; with (txtSt) { defaultTextFormat = format; autoSize = TextFieldAutoSize.LEFT; selectable = false; x = XmasStageWidth * 3 / 5; y = XmasStageHeight / 10; embedFonts = true; text = "t"; } addChild(txtSt); } private function SetBackgroundCharacters():void { var format:TextFormat = new TextFormat(); var format_red:TextFormat = new TextFormat(); with (format) { color = 0x00CC00; size = 72; align = TextFormatAlign.CENTER; font = "StFont"; } format_red.color = 0xCC0000; with (txtXmas[0]) { embedFonts = true; defaultTextFormat = format; text = txtXmasList.toString(); } // length of background charactors txtXmasWidth = txtXmas[0].textWidth; txtXmasMargin = (txtXmasWidth - XmasStageWidth) / 2; for (var i:int = 0; i < txtXmasLength; i++) { with (txtXmas[i]) { autoSize = TextFieldAutoSize.CENTER; selectable = false; embedFonts = true; defaultTextFormat = format; text = txtXmasList[i]; x = i > 0 ? txtXmas[i-1].x + txtXmas[i-1].textWidth : -txtXmasMargin; y = XmasStageHeight * 2 / 3; } addChild(txtXmas[i]); } // red in background charactors txtXmas[0].setTextFormat(format_red); } private function SetSnow():void { var format:TextFormat = new TextFormat(); var format_size:TextFormat = new TextFormat(); with (format) { color = 0xFFFFFF; align = TextFormatAlign.LEFT; font = "SnowFont"; } for (var i:int = 0; i < numTxtSnow; i++) { format_size.size = 24 + Math.random() * 48; speedSnow[i] = 0.5 + Math.random() * 0.5; with (txtSnow[i]) { selectable = false; embedFonts = true; defaultTextFormat = format; text = txtSnowList[int(Math.random() * txtSnowList.length)]; x = Math.random() * (XmasStageWidth + txtSnowMargin * 2) - txtSnowMargin; y = -100; setTextFormat(format_size); alpha = 0.3 + Math.random() * 0.7; } addChild(txtSnow[i]); } } private function NewSnow(n:int):void { var format:TextFormat = new TextFormat(); var format_size:TextFormat = new TextFormat(); with (format) { color = 0xFFFFFF; align = TextFormatAlign.LEFT; font = "SnowFont"; } format_size.size = 24 + Math.random() * 48; speedSnow[n] = 0.5 + Math.random() * 0.5; with (txtSnow[n]) { selectable = false; embedFonts = true; defaultTextFormat = format; text = txtSnowList[int(Math.random() * txtSnowList.length)]; x = Math.random() * (XmasStageWidth + txtSnowMargin * 2) - txtSnowMargin; y = -100; setTextFormat(format_size); alpha = 0.3 + Math.random() * 0.7; } } private function StartGame(event:Event):void { removeChild(txtTitle); stage.removeEventListener(KeyboardEvent.KEY_UP, StartGame); txtSt.addEventListener(MouseEvent.CLICK, ClickHandler); channel = bgm.play(); channel.addEventListener(Event.SOUND_COMPLETE, OnPlaybackComplete); // show flying angel SetFlyingAngel(); // show background characters SetBackgroundCharacters(); // show snow SetSnow(); timer.start(); } private function InitGame():void { var format:TextFormat = new TextFormat(); var format_red:TextFormat = new TextFormat(); var format_small:TextFormat = new TextFormat(); var i:int; // opening (upper letters are red, others are green) with (format) { color = 0x00CC00; size = 72; align = TextFormatAlign.CENTER; font = "XmasFont"; } format_red.color = 0xCC0000; format_small.size = 48; with (txtTitle) { autoSize = TextFieldAutoSize.CENTER; selectable = false; x = XmasStageWidth / 2; y = XmasStageHeight / 3; embedFonts = true; defaultTextFormat = format; text = "ActionXmas\nHit Any Key!"; } for each (var n:int in [0, 6, 11, 15, 19]) txtTitle.setTextFormat(format_red, n); txtTitle.setTextFormat(format_small, 11, 23); addChild(txtTitle); txtXmasList = ["s", "k", "e", "h", "j", "r", "g", "x", "n", "i"]; txtXmasLength = txtXmasList.length; for (i = 0; i < txtXmasLength; i++) txtXmas[i] = new TextField(); txtSnowList = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; timer = new Timer(10); timer.addEventListener(TimerEvent.TIMER, OnTick); for (i = 0; i < numTxtSnow; i++) txtSnow[i] = new TextField(); } public function ActionXmas() { XmasStageWidth = stage.stageWidth; XmasStageHeight = stage.stageHeight; InitGame(); stage.addEventListener(KeyboardEvent.KEY_UP, StartGame); } } }

ActionXmas-config.xml

<flex-config> <output>ActionXmas.swf</output> <default-size> <width>350</width> <height>350</height> </default-size> <default-frame-rate>60</default-frame-rate> <default-background-color>0x000000</default-background-color> <use-network>false</use-network> <compiler> <fonts> <languages> <language-range> <lang>englishRange</lang> <range>U+0020-U+007E</range> </language-range> <language-range> <lang>otherRange</lang> <range>U+00??</range> </language-range> </languages> </fonts> </compiler> </flex-config>

2 コメント:

nox さんのコメント...

IE7だとトップページから「続きを読む」で開くとFlashの表示がずれるなぁ。個別ページだと大丈夫みたい。ChromeやFirefoxでは問題ないのになんでだろう?

nox さんのコメント...

IE7だとFlashがずれるのでトップページにFlashを表示させることにした。どうやらIE7は閉じた状態のFlashを先読みしているようなんだけど、そのときに矩形サイズの位置を誤認するようだ。