RunnningInTrees
Press UpKey and DownKey
/**
* Copyright fakestar0826 ( http://wonderfl.net/user/fakestar0826 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/493G
*/
package {
import flash.ui.Keyboard;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Sprite;
public class FlashTest extends Sprite {
private var trees:Array;
private var numTrees:uint = 100;
private var fl:Number = 250;
private var vpX:Number = stage.stageWidth / 2;
private var vpY:Number = stage.stageHeight / 2;
private var floor:Number = 50;
private var vz:Number = 0;
private var friction:Number = 0.98;
public function FlashTest() {
// write as3 code here..
trees = [];
for(var i:int = 0;i < numTrees;i++)
{
var t:Tree = new Tree();
trees[i] = t;
t.xpos = Math.random() * 2000 - 1000;
t.ypos = floor;
t.zpos = Math.random() * 10000;
addChild(t);
}
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
private function onEnterFrame(e:Event):void
{
for(var i:int = 0;i < numTrees;i++)
{
var t:Tree = trees[i];
move(t);
}
vz *= friction;
sortZ();
}
private function onKeyDown(e:KeyboardEvent):void
{
if(e.keyCode == Keyboard.UP)
{
vz -= 1;
}
else if(e.keyCode == Keyboard.DOWN)
{
vz += 1;
}
}
private function move(t:Tree):void
{
t.zpos += vz;
if(t.zpos < -fl)
{
t.zpos += 1000;
}
if(t.zpos > 10000 - fl)
{
t.zpos -= 10000;
}
var scale:Number = fl / (fl + t.zpos);
t.scaleX = t.scaleY = scale;
t.x = vpX + t.xpos * scale;
t.y = vpY + t.ypos * scale;
t.alpha = scale;
}
private function sortZ():void
{
trees.sortOn("zpos", Array.NUMERIC | Array.DESCENDING);
for(var i:int = 0;i < numTrees;i++)
{
var t:Tree = trees[i];
setChildIndex(t, i);
}
}
}
}
import flash.display.Sprite;
class Tree extends Sprite
{
public var xpos:Number = 0;
public var ypos:Number = 0;
public var zpos:Number = 0;
public function Tree()
{
graphics.lineStyle(0, 0);
graphics.lineTo(0, -140 - Math.random() * 20);
graphics.moveTo(0, -30 - Math.random() * 30);
graphics.lineTo(Math.random() * 80 - 40, -100 - Math.random() * 40);
graphics.moveTo(0, -60 - Math.random() * 40);
graphics.lineTo(Math.random() * 60 - 30, -110 - Math.random() * 20);
}
}