簡易2Dアクションゲームテンプレート
簡易2Dアクションゲームテンプレート
・←→:移動
・↑:ジャンプ
・マウスで線をひく:地形
移動アルゴリズム
・少し浮いて横に移動した後、浮いた分を戻す
・つまり「↑→↓」という風に動く
/**
* Copyright o_healer ( http://wonderfl.net/user/o_healer )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/p5tO
*/
// forked from o_healer's お絵かきサンプル
/*
簡易2Dアクションゲームテンプレート
・←→:移動
・↑:ジャンプ
・マウスで線をひく:地形
移動アルゴリズム
・少し浮いて横に移動した後、浮いた分を戻す
・つまり「↑→↓」という風に動く
*/
package {
import flash.display.Sprite;
import flash.events.*;
public class FlashTest extends Sprite {
public function FlashTest() {
// write as3 code here..
addChild(new GameMain());
}
}
}
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.ui.Keyboard;
internal class GameMain extends Sprite{
//==Const==
static public const LINE_W:int = 2;
static public const LINE_COLOR:uint = 0x000000;
static public const LINE_ALPHA:Number = 1.0;
//==Var==
//#Graphic
public var m_Bitmap:Bitmap;
public var m_Shape:Shape;
//#Player
public var m_Player:Player;
//==Func==
//Static Global Access
static private var m_Instance:GameMain;
static public function Instance():GameMain{return m_Instance;}
//#Init
public function GameMain(){
//Init Later (for Using "stage" etc.)
addEventListener(Event.ADDED_TO_STAGE, Init );
m_Instance = this;
}
public function Init(e:Event = null):void{
//Init Once Only
{
removeEventListener(Event.ADDED_TO_STAGE, Init );
}
//Create Bitmap
{
var bmp_data:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight, true, 0x00000000);
m_Bitmap = new Bitmap(bmp_data);
addChild(m_Bitmap);
}
//Create Shape
{
m_Shape = new Shape();
addChild(m_Shape);
}
//Draw By Mouse
{
var draw_flag:Boolean = false;
var g:Graphics = m_Shape.graphics;
//Down
stage.addEventListener(
MouseEvent.MOUSE_DOWN,
function(e:MouseEvent):void{
//Flag On
{
draw_flag = true;
}
//Clear Setting
{
g.clear();
}
//Draw Dot
{
g.lineStyle(0, LINE_COLOR, LINE_ALPHA);
g.drawCircle(mouseX, mouseY, LINE_W/2);
}
//Start Line
{
g.lineStyle(LINE_W, LINE_COLOR, LINE_ALPHA);
g.moveTo(mouseX, mouseY);
}
}
);
//Move
stage.addEventListener(
MouseEvent.MOUSE_MOVE,
function(e:MouseEvent):void{
//Check
{
if(! draw_flag){
return;
}
}
//Draw Line
{
g.lineTo(mouseX, mouseY);
}
}
);
//Up
stage.addEventListener(
MouseEvent.MOUSE_UP,
function(e:MouseEvent):void{
//Flag Off
{
draw_flag = false;
}
//Refresh
{
m_Bitmap.bitmapData.draw(m_Shape);
g.clear();
}
}
);
}
//Register Player
{
m_Player = new Player();
addChild(m_Player);
}
}
//#Move
public function IsCollision(i_X:int, i_Y:int):Boolean{
//範囲外は壁とみなす
{
if(i_X < 0){return true;}
if(i_X >= stage.stageWidth){return true;}
if(i_Y < 0){return true;}
if(i_Y >= stage.stageHeight){return true;}
}
//一定以上黒ければ壁とみなす
var color:uint = m_Bitmap.bitmapData.getPixel32(i_X, i_Y);
var alpha:uint = (color >> 24) & 0xFF;
return alpha > 0xFF/2;
}
}
internal class Player extends Sprite{
//==Cost==
//#Input
static public var button_enum:int = 0;
static public const BUTTON_L:int = button_enum++;
static public const BUTTON_R:int = button_enum++;
static public const BUTTON_U:int = button_enum++;
static public const BUTTON_NUM:int = button_enum;
//#Move
static public const VEL_X:Number = 100.0;
static public const VEL_Y:Number = 300.0;
static public const ACC_Y:Number = 500.0;
//==Var==
//#Input
public var m_Input:Array = new Array(BUTTON_NUM);
//#Move
public var m_VY:Number = 0.0;
public var m_OnGround:Boolean = false;
//==Function==
//#Init
public function Player(){
//Init Later (for Using "stage" etc.)
addEventListener(Event.ADDED_TO_STAGE, Init );
}
public function Init(e:Event = null):void{
var i:int;
//Init Once Only
{
removeEventListener(Event.ADDED_TO_STAGE, Init );
}
//Pos
{
this.x = stage.stageWidth/2;
this.y = stage.stageHeight/4;
}
//Create Graphic
{
const Rad:int = 5;
var shape:Shape = new Shape();
var g:Graphics = shape.graphics;
g.lineStyle(0, 0x000000, 0.0);
g.beginFill(0xFF0000, 1.0);
g.drawCircle(0, -Rad, Rad);
g.endFill();
addChild(shape);
}
//Input
{
//Init
for(i = 0; i < BUTTON_NUM; i++){
m_Input[i] = false;
}
//Listener
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
}
//Call Update
{
addEventListener(Event.ENTER_FRAME, Update);
}
}
//#Input
public function KeyDown(e:KeyboardEvent):void{
switch(e.keyCode){
case Keyboard.LEFT: m_Input[BUTTON_L] = true; break;
case Keyboard.RIGHT: m_Input[BUTTON_R] = true; break;
case Keyboard.UP: m_Input[BUTTON_U] = true; break;
}
}
public function KeyUp(e:KeyboardEvent):void{
switch(e.keyCode){
case Keyboard.LEFT: m_Input[BUTTON_L] = false; break;
case Keyboard.RIGHT: m_Input[BUTTON_R] = false; break;
case Keyboard.UP: m_Input[BUTTON_U] = false; break;
}
}
//#Update
public function Update(e:Event):void{
var DeltaTime:Number = 1.0 / 24.0;
//Move
{
//Calc MoveVal
var MoveX:int;
var MoveY:int;
{
//X
{
MoveX = 0;
if(m_Input[BUTTON_L]){MoveX -= VEL_X * DeltaTime;}
if(m_Input[BUTTON_R]){MoveX += VEL_X * DeltaTime;}
}
//Y
{
//ジャンプ
if(m_OnGround && m_Input[BUTTON_U]){
m_VY = -VEL_Y;
m_OnGround = false;
}
//重力
m_VY += ACC_Y * DeltaTime;
MoveY = m_VY * DeltaTime;
}
}
//移動アルゴリズム実行
const BaseOffset:int = 1;//浮く量
var Offset:int;
var TrgX:int;
var TrgY:int;
var i:int;
//Move : Up
{
TrgX = this.x;
Offset = (MoveY < 0)? BaseOffset-MoveY: BaseOffset;
for(i = 0; i < Offset; i++){
TrgY = this.y - 1;
if(GameMain.Instance().IsCollision(TrgX, TrgY)){
//天井にぶつかったので速度は0にする
m_VY = 0;
break;
}
this.y = TrgY;
}
}
//Move : Horz
{
TrgX = this.x;
TrgY = this.y;
for(i = 0; i != MoveX; ){
if(i < MoveX){
i++;
TrgX++;
}else{
i--;
TrgX--;
}
if(GameMain.Instance().IsCollision(TrgX, TrgY)){break;}
this.x = TrgX;
}
}
//Move : Down
{
TrgX = this.x;
Offset = (MoveY > 0)? BaseOffset+MoveY: BaseOffset;
for(i = 0; i < Offset; i++){
TrgY = this.y + 1;
if(GameMain.Instance().IsCollision(TrgX, TrgY)){
//接地したとみなす
m_OnGround = true;//ジャンプできるようにフラグオン
m_VY = 0;//速度リセット
break;
}
this.y = TrgY;
}
}
}
}
}