flash on 2012-9-13
♥0 |
Line 69 |
Modified 2012-09-13 23:51:50 |
MIT License
archived:2017-03-30 22:59:57
ActionScript3 source code
/**
* Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/tZu4t
*/
package {
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.Graphics;
import flash.display.Sprite;
public class FlashTest extends Sprite {
public var car:Sprite;
public function FlashTest() {
// write as3 code here..
var g:Graphics;
car = new Sprite();
g = car.graphics;
g.clear();
g.lineStyle(2,0);
g.beginFill(0x0000FF,1);
g.drawRect(-16,-8,32,16);
g.endFill();
addChild(car);
car.x = 200;
car.y = 200;
graphics.clear();
graphics.lineStyle(2,0);
graphics.drawRect(50,50,200,200);
stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
stage.addEventListener(KeyboardEvent.KEY_UP, kup);
stage.addEventListener(Event.ENTER_FRAME, onEnter);
}//ctor
public var vecKey:Vector.<Boolean> = new Vector.<Boolean>(1024,false);
public function kup(e:KeyboardEvent):void
{
vecKey[e.keyCode] = false;
}
public function kdown(e:KeyboardEvent):void
{
vecKey[e.keyCode] = true;
}
public var vx:Number = 0;
public var vy:Number = 0;
public var a:Number = 0;
public function onEnter(e:Event):void
{
if (vecKey[Keyboard.LEFT]) { a-= 0.04;}
if (vecKey[Keyboard.RIGHT]) { a+=0.04;}
if (vecKey[ Keyboard.UP])
{
vx += Math.cos(a) * 0.17;
vy += Math.sin(a) * 0.17;
}
if (vecKey[Keyboard.DOWN])
{
vx -= Math.cos(a) * 0.17;
vy -= Math.sin(a) * 0.17;
}
vx *= 0.983;
vy *= 0.983;
car.x += vx;
car.y += vy;
car.rotation = a * 180/3.1415;
if (car.x < 50)
{ car.x = 50; if (vx < 0) { vx *=-0.5;}}
if (car.x >= 250)
{ car.x = 250; if (vx>0) { vx *= -0.5;}}
if (car.y < 50)
{ car.y = 50; if (vy<0) { vy *= -0.5;}}
if (car.y >= 250)
{ car.y = 250; if (vy > 0) { vy *= -0.5;}}
}//onenter
}//classend
}