package { import flash.display.MorphShape; import flash.display.Shape; import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.geom.ColorTransform; import flash.geom.Point; import flash.text.TextField; import flash.text.TextFieldAutoSize; import flash.text.TextFormat; import flash.text.engine.CFFHinting; import flash.utils.getTimer; /** * ... * @author Alejandro Grilli J. */ public class Main extends Sprite { [Embed(source='../obj/georgia.ttf', fontFamily="GameFont", mimeType="application/x-font-truetype", cff="true")] private var fontGentiumR:Class; [Embed(source='../obj/georgiai.ttf', fontFamily="GameFont", fontStyle="italic", mimeType="application/x-font-truetype", cff="true")] private var fontGentiumI:Class; private var gameSceneCollection:GameSceneCollection; private var currentScene:GameScene; private var gotoGameScene:String; private var skipDelay:Number; private var lastTime:int; private var showTextDelay:Number; private var blinkDelay:Number; private var textArea:TextField; private var background:Sprite; private var options:Array = new Array; private var blinker:Shape; public function Main():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 // MochiBot.com -- Version 8 // Tested with Flash 9-10, ActionScript 3 MochiBot.track(this, "6bc17dd9"); addEventListener(Event.ENTER_FRAME, enterFrame); gameSceneCollection = new GameSceneCollection; background = new Sprite; addChild(background); var backgroundGraphic:Shape = new Shape; background.addChild(backgroundGraphic); with (backgroundGraphic.graphics) { beginFill(0xffffff); drawRect(0, 0, stage.stageWidth, stage.stageHeight); endFill(); } setBackground(gameSceneCollection.defaultColor); textArea = new TextField; addChild(textArea); textArea.selectable = false; textArea.embedFonts = true; textArea.defaultTextFormat = gameSceneCollection.defaultTextFormat; textArea.x = gameSceneCollection.defaultTextPosition.x; textArea.y = gameSceneCollection.defaultTextPosition.y; textArea.width = stage.stageWidth - gameSceneCollection.defaultTextPosition.x * 2; textArea.wordWrap = true; stage.addEventListener(MouseEvent.CLICK, clickHandler); blinker = new Shape; with (blinker.graphics) { beginFill(0xffffff); drawRect( -10, -10, 10, 10); endFill(); } blinker.x = stage.stageWidth; blinker.y = stage.stageHeight; blinker.visible = false; addChild(blinker); changeGameScene(gameSceneCollection.firstScene); } private function enterFrame(e:Event):void { if (gotoGameScene) { changeGameScene(gotoGameScene); gotoGameScene = null; } var timeDelta:Number = getTimer() - lastTime; lastTime += timeDelta; if (!isNaN(skipDelay)) { if (skipDelay > 0) { skipDelay -= timeDelta; } else { changeGameScene(currentScene.next); } } if (!isNaN(showTextDelay)) { if (showTextDelay > 0) { showTextDelay -= timeDelta; } else { showTextDelay = NaN; textArea.visible = true; if (options.length > 0) { for (var i:int = options.length - 1; i >= 0; i--) { options[i].visible = true; } } } } if (!isNaN(blinkDelay)) { if (blinkDelay > 0) { blinkDelay -= timeDelta; } else { blinker.visible = !blinker.visible; //blinker.visible = true; blinkDelay = 500; } } //trace(blinkDelay); } private function changeGameScene(newGameScene:String):void { skipDelay = NaN; if (options.length > 0) { for (var i:int = options.length - 1; i >= 0; i--) { options[i].removeEventListener(MouseEvent.CLICK, optionClickHandler); options[i].removeEventListener(MouseEvent.ROLL_OVER, optionRolloverHandler); options[i].removeEventListener(MouseEvent.ROLL_OUT, optionRolloutHandler); removeChild(options[i]); options.splice(i, 1); } } currentScene = gameSceneCollection[newGameScene]; textArea.text = currentScene.text; if (!isNaN(currentScene.color)) { setBackground(currentScene.color); } if (currentScene.textFormat) { setTextFormat(currentScene.textFormat); } var numOptions:int = currentScene.numOptions; if (numOptions > 0) { for (i = 0; i < numOptions; i++) { var newOption:Option = new Option(currentScene.getOptionText(i), currentScene.getOptionLink(i)); addChild(newOption); newOption.buttonMode = true; newOption.useHandCursor = true; newOption.mouseChildren = false; newOption.textField.selectable = false; newOption.textField.embedFonts = true; newOption.textField.autoSize = TextFieldAutoSize.LEFT; newOption.textField.defaultTextFormat = gameSceneCollection.defaultTextFormat; newOption.textField.defaultTextFormat = gameSceneCollection.defaultOptionFormat; if (currentScene.optionFormat) { newOption.textField.defaultTextFormat = currentScene.optionFormat; } newOption.textField.text = newOption.textField.text; //newOption.textField.setTextFormat(gameSceneCollection.defaultTextFormat); //newOption.textField.setTextFormat(gameSceneCollection.defaultOptionFormat); //if (currentScene.optionFormat) { newOption.textField.setTextFormat(currentScene.optionFormat); } newOption.y = gameSceneCollection.defaultOptionPosition.y; if (options.length > 0) { var lastOption:Option = options[options.length - 1]; newOption.x = lastOption.x + lastOption.width + 30; } else { newOption.x = gameSceneCollection.defaultOptionPosition.x; } newOption.addEventListener(MouseEvent.CLICK, optionClickHandler); newOption.visible = false; options.push(newOption); newOption.addEventListener(MouseEvent.ROLL_OVER, optionRolloverHandler); newOption.addEventListener(MouseEvent.ROLL_OUT, optionRolloutHandler); } } if (currentScene.duration) { skipDelay = currentScene.duration; } if (currentScene.music) { currentScene.music.play(); } textArea.visible = false; showTextDelay = 100; var colorTransform:ColorTransform = new ColorTransform; colorTransform.color = uint(gameSceneCollection.defaultTextFormat.color); if (currentScene.textFormat) { colorTransform.color = uint(currentScene.textFormat.color); } blinker.transform.colorTransform = colorTransform; blinker.visible = false; if ((options.length <= 0) && (!currentScene.duration)) { blinkDelay = 2500; } else { blinkDelay = NaN; } } private function setBackground(color:int):void { var colorTransform:ColorTransform = new ColorTransform; colorTransform.color = color; background.transform.colorTransform = colorTransform; } private function setTextFormat(textFormat:TextFormat):void { textArea.setTextFormat(textFormat); } private function clickHandler(e:MouseEvent):void { if (textArea.visible) { if (options.length <= 0) { gotoGameScene = currentScene.next; } } } private function optionClickHandler(e:MouseEvent):void { if (textArea.visible) { gotoGameScene = e.currentTarget.link; } } private function optionRolloverHandler(e:MouseEvent):void { e.currentTarget.textField.background = true; e.currentTarget.textField.backgroundColor = e.currentTarget.textField.textColor; e.currentTarget.textField.textColor = background.transform.colorTransform.color; var a:TextField = new TextField; } private function optionRolloutHandler(e:MouseEvent):void { e.currentTarget.textField.background = false; e.currentTarget.textField.textColor = e.currentTarget.textField.backgroundColor; } } }