5 画面がスクロールします
import net.hires.debug.Stats;
package {
import flash.display.*;
import flash.geom.*;
import flash.events.*;
import flash.text.*;
// import net.hires.debug.Stats;
[SWF(width = "465", height = "465", frameRate = "30", backgroundColor = "#808080")]
public class Test extends Sprite {
private const VMEM_W:int = 465, VMEM_H:int = 465;
private const BOX_SIZE:int = 10;
private const LINE_DIV:uint = 100;
private var vmem:BitmapData = new BitmapData(VMEM_W, VMEM_H, false, 0x808080);
private var input:Input = new Input();
private var player:Player = new Player(input);
private var cam:Vec2 = new Vec2(0, 0);
private function toRect(x:Number, y:Number, size:Number) : Rectangle {
var vw:Number = VMEM_W / 2;
var vh:Number = VMEM_H / 2;
var s:Number = size / 2;
return new Rectangle(
-s + x + vw - cam.x, - s + vh - y + cam.y,
size, size);
}
private function drawLine(x1:Number, y1:Number, x2:Number, y2:Number, color:Number) : void {
var dx:Number = x2 - x1;
var dy:Number = y2 - y1;
for (var i:uint = 0; i <= LINE_DIV; i++) {
drawRect(x1 + dx * i / LINE_DIV, y1 + dy * i / LINE_DIV, 4, color);
}
}
private function drawRect(x:Number, y:Number, size:Number, color:Number) : void {
var r:Rectangle = toRect(x, y, size);
vmem.fillRect(r, color);
}
private function getCursor() : Vec2 {
return new Vec2(-VMEM_W / 2 + stage.mouseX + cam.x, VMEM_H / 2 - stage.mouseY + cam.y);
}
public function update(event:Event) : void {
input.update(getCursor());
player.update();
cam.adds(player.v.sub(cam).mul(0.2));
vmem.lock();
vmem.fillRect(vmem.rect, 0x808080);
vmem.fillRect(toRect(0, 0, STAGE_LEN), 0x000000);
vmem.fillRect(toRect(0, 0, STAGE_LEN - 10), 0x404040);
vmem.fillRect(toRect(0, 0, STAGE_LEN - 20), 0x808080);
vmem.fillRect(toRect(player.v.x, player.v.y, player.len), player.col);
vmem.unlock();
}
function Test() {
var text:TextField = new TextField();
text.text = "test1 moving box";
addChild(text);
addChild(new Bitmap(vmem));
addEventListener(Event.ENTER_FRAME, update);
stage.addEventListener(MouseEvent.MOUSE_DOWN,
function(e:MouseEvent) : void { input.OnMouseDown(); } );
stage.addEventListener(MouseEvent.MOUSE_UP,
function(e:MouseEvent) : void { input.OnMouseUp(); } );
// addChild(new Stats());
}
}
}
const STAGE_LEN:Number = 400;
class Input {
private var mb:Boolean;
private var pmb:Boolean;
private var mv:Vec2;
function Input() {
mb = pmb = false;
}
function update(v:Vec2) {
pmb = mb;
mv = v;
}
function pos() : Vec2 {
return new Vec2(mv.x, mv.y);
}
function btnDown() : Boolean {
return mb && (!pmb);
}
function btnHold() : Boolean {
return mb;
}
function OnMouseDown() {
mb = true;
}
function OnMouseUp() {
mb = false;
}
}
class Player {
public var v:Vec2 = new Vec2(0, 0);
public var col:uint;
private var i:Input;
private var dash:uint;
private var dangle:Angle;
private const DASH_F:uint = 15;
private const DASH_V:Number = 8;
public const len:Number = 20;
public const vr:Number = 0.2;
public const vl:Number = 2;
public const COL_NORMAL:uint = 0xffffff;
public const COL_DASH:uint = 0xff0000;
function Player(input:Input) {
i = input;
dash = 0;
col = COL_NORMAL;
}
public function update() {
var dv:Vec2 = i.pos().sub(v);
if (dash == 0) {
dv.muls(vr);
dv.divs(Math.max(dv.len(), vl)); dv.muls(vl)
v.adds(dv);
} else {
v.adds(dangle.vec().mul(DASH_V));
}
v.x = Misc.Clamp(v.x, (len - STAGE_LEN) / 2, (STAGE_LEN - len) / 2);
v.y = Misc.Clamp(v.y, (len - STAGE_LEN) / 2, (STAGE_LEN - len) / 2);
if (dash == 0) {
if (i.btnHold()) {
dash = 1
dangle = i.pos().sub(v).angle();
col = COL_DASH;
}
} else {
dash++;
if (dash > DASH_F) {
dash = 0;
col = COL_NORMAL;
}
}
}
}
class Angle {
public var a:Number = 0;
function Angle(n:Number) {
a = n;
round();
}
function add(n:Angle) : Angle {
return Angle(a + n.a);
}
function adds(n:Angle) {
a += n.a;
round();
}
function sub(n:Angle) : Angle {
return Angle(a - n.a);
}
function subs(n:Angle) {
a - n.a;
round();
}
function mul(n:Number) : Angle {
return Angle(a * n);
}
function muls(n:Number) {
a *= n;
round();
}
function div(n:Number) : Angle {
return Angle(a / n);
}
function divs(n:Number) {
a /= n;
round();
}
function round() {
while (a >= Math.PI) a -= Math.PI * 2
while (a < -Math.PI) a += Math.PI * 2
}
function cos() : Number {
return Math.cos(a);
}
function sin() : Number {
return Math.sin(a);
}
function vec() : Vec2 {
return new Vec2(cos(), sin());
}
}
class Vec2 {
public var x:Number = 0, y:Number = 0;
function Vec2(ax:Number = 0, ay:Number = 0) {
x = ax; y = ay;
}
function set(ax:Number = 0, ay:Number = 0) {
x = ax; y = ay;
}
function add(v:Vec2) : Vec2 {
return new Vec2(x + v.x, y + v.y);
}
function adds(v:Vec2) {
x += v.x; y += v.y;
}
function sub(v:Vec2) : Vec2 {
return new Vec2(x - v.x, y - v.y);
}
function subs(v:Vec2) {
x -= v.x; y -= v.y;
}
function mul(n:Number) : Vec2 {
return new Vec2(x * n, y * n);
}
function muls(n:Number) {
x *= n; y *= n;
}
function div(n:Number) : Vec2 {
return new Vec2(x / n, y / n);
}
function divs(n:Number) {
x /= n; y /= n;
}
function len() : Number {
return Math.sqrt(x * x + y * y);
}
function angle() : Angle {
return new Angle(Math.atan2(y, x));
}
}
class Misc {
static function Clamp(x:Number, min:Number, max:Number) : Number {
return Math.min(Math.max(x, min), max);
}
}