アクションゲーム風
左右キーで移動
上でジャンプ
マウスで地形がかけます
小さすぎる地形は突き抜けマス
/**
左右キーで移動
上でジャンプ
マウスで地形がかけます
小さすぎる地形は突き抜けマス
*/
package {
import flash.display.Sprite;
import flash.events.*;
import flash.text.*;
import flash.geom.*;
public class Main extends Sprite {
private var myShip:MyShip = new MyShip();
private var ground:Sprite;
public static var leftKey:Boolean = false;
public static var upKey:Boolean = false;
public static var rightKey:Boolean = false;
public static var downKey:Boolean = false;
public static var ctrlKey:Boolean = false;
public function Main() {
// write as3 code here..
cacheAsBitmap = true;
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onClick);
stage.addEventListener(MouseEvent.MOUSE_UP, onRelease);
ground = new Sprite();
addChild(ground);
myShip.ground = ground;
addChild(myShip);
}
public function update(e:Event):void
{
myShip.update();
}
//マウスで地形を描く
private var pt:Point = new Point();
private function onClick(e:MouseEvent):void
{
pt.x = stage.mouseX;
pt.y = stage.mouseY;
}
private function onRelease(e:MouseEvent):void
{
ground.graphics.beginFill(0xEA594F);
ground.graphics.drawRect(pt.x, pt.y, stage.mouseX-pt.x, stage.mouseY-pt.y);
ground.graphics.endFill();
}
//キーダウン処理
private function onKeyPress(e:KeyboardEvent):void
{
switch(e.keyCode){
case 37:
leftKey = true;
break;
case 38:
upKey = true;
break;
case 39:
rightKey = true;
break;
case 40:
downKey = true;
break;
case 17:
ctrlKey = true;
break;
}
}
//キーアップ処理
private function onKeyUp(evt:KeyboardEvent):void
{
switch(evt.keyCode){
case 37:
leftKey = false;
break;
case 38:
upKey = false;
break;
case 39:
rightKey = false;
break;
case 40:
downKey = false;
break;
case 17:
ctrlKey = false;
break;
}
}
}
}
import flash.display.Sprite;
import flash.events.*;
import flash.text.*;
//キャラクター基底
class Tokun extends Sprite
{
public var vx:Number = 0;
public var vy:Number = 0;
public var lastX:Number = 0;
public var lastY:Number = 0;
public const moveSpd:Number = 7;
public const jumpSpd:Number = -10;
public const masatu:Number = 0.7;
public var tf:TextField = new TextField();
private const gravity:Number = 9.8/30;
public var ground:Sprite;
public function Tokun():void
{
cacheAsBitmap = true;
tf.textColor = 0x000000;
tf.selectable = false;
tf.text ="(・∀・)";
tf.wordWrap = false;
tf.autoSize = TextFieldAutoSize.LEFT;
addChild(tf);
}
public function update():void
{
vy += gravity;
lastY = y;
y += vy;
//地形とあたり判定
if (ground.hitTestPoint(x+width/2, y+height, true) )
{
vy = 0;
y = lastY;
}
lastX = x;
x += vx;
if (ground.hitTestPoint(x+width/2, y+height, true) )
{
vx = 0;
x = lastX;
}
vx *= masatu;
if (y >= stage.stageHeight- this.height) {
y = stage.stageHeight - this.height;
vy = 0;
}
}
}
//自機
class MyShip extends Tokun
{
public override function update():void
{
if (Main.leftKey) {
vx = -moveSpd;
}
if (Main.rightKey) {
vx = moveSpd;
}
if (vy == 0 && Main.upKey) {
vy = jumpSpd;
Main.upKey = false;
}
super.update();
}
}