Actionscript3.0で状態遷移 で学んだことを取り入れて、まずはSTGの状態遷移だけ作ってみる。
前の記事:ActionScript3.0でSTGを作成(1)の続き
https://sites.google.com/site/itouhiro/2013/20130715stgstat.swf
- タイトル画面 ―― PRESS‥の文字をクリックすると次のシーンに進む (画面全体をクリックするようにしたらゲームオーバーでクリックしたときタイトル画面のクリックにもなってしまうという問題があったので、文字をクリックする仕様に変えた)
- ゲームプレイ画面 ―― まだ作成してない。自機をクリックすると次のシーンに進む
- ゲームオーバー画面 ―― OVER‥の文字をクリックするか、10秒まてば次のシーンに進む
ビルドにはFlashCS6を使用した。
Main.as
package {
import flash.display.Scene;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
/**
* ...
* @author itouhiro
*/
[SWF (backgroundColor="0x000000",frameRate="30",width="465",height="465")]
public class Main extends Sprite {
private var scene:SceneBase;
static public var bg:Background;
static public var stg:Stage;
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
stg = stage;
//背景 表示
bg = new Background();
addChild(bg);
//タイトル画面 表示
scene = new SceneTop();
addChild(scene);
scene.addEventListener(Event.COMPLETE, onComplete);
}
private function onComplete(e:Event):void {
scene.removeEventListener(Event.COMPLETE, onComplete);
var newScene:SceneBase = scene.getNext();
removeChild(scene);
scene = null;
scene = newScene;
scene.init();
addChild(scene);
scene.addEventListener(Event.COMPLETE, onComplete);
}
}
}
Background.as
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
/**
* ...
* @author itouhiro
*/
public class Background extends Sprite {
private var backgroundImg:Bitmap;
private var count:int = 0;
private var scoreTxt:TextField;
private var score:int = 0;
public var statusTxt:TextField; //クリック可能にするため、publicに
public function Background() {
//背景
backgroundImg = new Bitmap(new BackgroundImg());
addChild(backgroundImg);
setChildIndex(backgroundImg, 0);
//score表示欄
var theFont:NamcoFont = new NamcoFont();
/* Font: (c) 2011 Cody "CodeMan38" Boisclair. Released under the SIL Open Font License. */
scoreTxt = new TextField();
scoreTxt.defaultTextFormat = new TextFormat(theFont.fontName, 16, 0xEEEEEE);
scoreTxt.embedFonts = true;
scoreTxt.x = 0;
scoreTxt.width = Main.stg.stageWidth;
scoreTxt.autoSize = TextFieldAutoSize.CENTER;
scoreTxt.y = 10;
addChild(scoreTxt);
//GameOver表示欄
statusTxt = new TextField();
statusTxt.defaultTextFormat = new TextFormat(theFont.fontName, 16, 0xEEEEEE);
statusTxt.embedFonts = true;
statusTxt.x = 0;
statusTxt.width = Main.stg.stageWidth;
statusTxt.autoSize = TextFieldAutoSize.CENTER;
statusTxt.y = Main.stg.stageHeight / 2 - 16; //16はフォントの縦幅
statusTxt.text = '';
addChild(statusTxt);
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
private function enterFrameHandler(e:Event):void {
count++;
backgroundImg.y = Math.sin((count % 360) * Math.PI / 180) * 20 + 20; // 0~40
}
public function setScore(plusScore:int):void {
score += plusScore;
scoreTxt.text = 'SCORE ' + ('0000000' + score).slice( -7);
}
public function setMessage(message:String):void {
statusTxt.text = message;
}
}
}
SceneBase.as
package {
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.Event;
/**
* ...
* @author itouhiro
*/
public class SceneBase extends Sprite {
protected var nextScene:SceneBase;
public function getNext():SceneBase {
return nextScene;
}
public function init():void {
}
public function close():void {
dispatchEvent(new Event(Event.COMPLETE));
}
}
}
SceneTop.as
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.MouseEvent;
/**
* ...
* @author itouhiro
*/
public class SceneTop extends SceneBase {
public function SceneTop() {
init();
}
override public function init():void {
var titleLogo:Bitmap = new Bitmap(new TitleLogo());
titleLogo.scaleX = titleLogo.scaleY = 1.5;
titleLogo.x = (Main.stg.stageWidth - titleLogo.width) / 2;
titleLogo.y = Main.stg.stageHeight / 5;
addChild(titleLogo);
Main.bg.setMessage('PRESS MOUSE CLICK\n\n\n\n\n\n\n\n(C) 2013 ITOUHIRO');
Main.bg.statusTxt.addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void {
trace('SceneTop onClick');
Main.bg.statusTxt.removeEventListener(MouseEvent.CLICK, onClick);
Main.bg.setMessage('');
nextScene = new ScenePlay();
close();
}
}
}
ScenePlay.as
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.MouseEvent;
/**
* ...
* @author itouhiro
*/
public class ScenePlay extends SceneBase {
public function ScenePlay() {
init();
}
override public function init():void {
Main.bg.setScore(0);
//自機を配置
var myShip:Sprite = new Sprite();
myShip.addChild(new Bitmap(new MyShip1()));
myShip.scaleX = myShip.scaleY = 3;
myShip.x = Main.stg.stageWidth / 2;
myShip.y = Main.stg.stageHeight / 5 * 4;
addChild(myShip);
addEventListener(MouseEvent.CLICK, onClick);
}
private function onClick(e:MouseEvent):void {
trace('ScenePlay onClick');
removeEventListener(MouseEvent.CLICK, onClick);
nextScene = new SceneOver();
close();
}
}
}
SceneOver.as
package {
import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
/**
* ...
* @author itouhiro
*/
public class SceneOver extends SceneBase {
private var count:int = 0;
public function SceneOver() {
init();
}
override public function init():void {
Main.bg.setMessage('GAME OVER');
Main.bg.statusTxt.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
count++;
if (count < Main.stg.frameRate * 10) return; //10秒立つと自動的に次のシーンに移動
onClick(null);
}
private function onClick(e:MouseEvent):void {
trace('SceneOver onClick');
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
Main.bg.statusTxt.removeEventListener(MouseEvent.CLICK, onClick);
Main.bg.setMessage('');
nextScene = new SceneTop();
close();
}
}
}
それではScenePlayの中身を作る。
0 件のコメント:
コメントを投稿