Voiture Polution 2
lighter
// forked from Yukulele's Voiture Polution
//lighter
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.filters.GlowFilter;
import flash.geom.ColorTransform;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Keyboard;
import flash.utils.Timer;
public class Main extends Sprite
{
private var voiture:Voiture = new Voiture;
private var directions:Object = { haut:false, bas:false, gauche:false, droite:false };
private var dataDessin:BitmapData;
private var dessin:Dessin;
private var oldDatas:Object = null;
public function Main():void
{
stage.frameRate=60;
dessin = new Dessin;
addEventListener(CustomEvent.CHANGEPOSITION, dessiner);
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener(KeyboardEvent.KEY_DOWN, appuyer);
stage.addEventListener(KeyboardEvent.KEY_UP, relacher);
stage.addEventListener(Event.RESIZE, resize);
var bitmap:Bitmap = new Bitmap(dataDessin);
addChild(voiture);
resize();
}
private function appuyer(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case Keyboard.UP:
directions.haut = true;
break;
case Keyboard.DOWN:
directions.bas = true;
break;
case Keyboard.LEFT:
directions.gauche = true;
break;
case Keyboard.RIGHT:
directions.droite = true;
break;
}
majVoiture();
}
private function relacher(e:KeyboardEvent):void
{
switch(e.keyCode)
{
case Keyboard.UP:
directions.haut = false;
break;
case Keyboard.DOWN:
directions.bas = false;
break;
case Keyboard.LEFT:
directions.gauche = false;
break;
case Keyboard.RIGHT:
directions.droite = false;
break;
}
majVoiture();
}
private function majVoiture():void
{
if (directions.bas)
voiture.acceleration = -.2;
else if (directions.haut)
voiture.acceleration = 1;
else
voiture.acceleration = 0;
if (directions.droite == directions.gauche)
voiture.direction = 0;
else if (directions.droite)
voiture.direction = 1;
else if (directions.gauche)
voiture.direction = -1;
else
voiture.acceleration = 0;
}
private function dessiner(e:CustomEvent):void
{
//dessin.colorTransform(dessin.rect, new ColorTransform(1, 1, 1,.999));
if (e.datas.traverse)
oldDatas = null;
if (oldDatas != null)
dessin.line(e.datas.x, e.datas.y, oldDatas.x,oldDatas.y);
oldDatas = e.datas;
}
private function resize(e:Event=null):void
{
var copyData:BitmapData;
if (dataDessin != null)
copyData = dataDessin.clone();
dataDessin = new BitmapData(stage.stageWidth/Dessin.RESO, stage.stageHeight/Dessin.RESO);
if (copyData != null)
{
var w:int = Math.ceil(dataDessin.width / copyData.width);
var h:int = Math.ceil(dataDessin.height / copyData.height);
for (var _w:Number = 0; _w < w; _w++ )
for (var _h:Number = 0; _h < h; _h++ )
{
var matrix:Matrix = new Matrix();
matrix.translate(_w * copyData.width, _h * copyData.height);
dataDessin.draw(copyData,matrix);
}
}
dessin.bitmapData = dataDessin;
dessin.width=stage.stageWidth;
dessin.height=stage.stageHeight;
addChildAt(dessin, 0);
}
}
}
import flash.events.Event;
class CustomEvent extends Event
{
public static const CHANGEPOSITION:String = "changePosition";
private var _datas:Object;
public function get datas():Object
{
return _datas;
}
public function CustomEvent(type:String, datas:Object=null)
{
super(type, true);
_datas = datas;
}
public override function clone():Event
{
return new CustomEvent(type, _datas);
}
}
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.filters.GlowFilter;
import flash.utils.Timer;
class Voiture extends Sprite
{
private var vitesse:Number=0;
private var _acceleration:Number=0;
private var timer:Timer = new Timer(1);
private var _direction:Number = 0;
private var accAngle:Number = 0;
private var angle:Number = -Math.PI / 2;
private var _x:Number;
private var _y:Number;
private var lumiere:GlowFilter = new GlowFilter(0x0077ff, 1, 10, 10, 7, 3);
private var lum:Number=0;
public function Voiture()
{
filters = [lumiere];
addEventListener(Event.ADDED_TO_STAGE, poser);
timer.addEventListener(TimerEvent.TIMER, majPos);
addEventListener(Event.ENTER_FRAME, maj);
graphics.beginFill(0x330000);
graphics.moveTo( 0, -10);
graphics.lineTo( 10, 0);
graphics.lineTo(0, 10);
graphics.endFill();
width = 15;
height = 15;
addEventListener(Event.ADDED_TO_STAGE, centrer);
function centrer(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, centrer);
_x = stage.stageWidth / 2;
_y = stage.stageHeight / 2;
}
}
public function get acceleration():Number
{
return _acceleration;
}
public function set acceleration(val:Number):void
{
_acceleration = val;
}
public function get direction():Number
{
return _direction;
}
public function set direction(val:Number):void
{
_direction = val;
}
private function maj(e:Event):void
{
x = _x;
y = _y;
lum+=acceleration/20;
lum=Math.min(1.5,lum);
lum*=.96;
lumiere.alpha = lum;
filters = [lumiere];
rotation = (angle*180/Math.PI)%360;
}
private function majPos(e:TimerEvent):void
{
vitesse += acceleration/3;
vitesse *= .97;
accAngle += direction / 2000;
if (direction == 0)
accAngle *= .9;
else
accAngle *= .99;
angle += accAngle*vitesse;
var vitX:Number = Math.cos(angle);
var vitY:Number = Math.sin(angle);
_x += vitesse * vitX;
_y += vitesse * vitY;
var traverse:Boolean = false;
if (_x < 0)
{
traverse = true;
_x = stage.stageWidth;
}
if (_x > stage.stageWidth)
{
traverse = true;
_x = 0;
}
if (_y < 0)
{
traverse = true;
_y = stage.stageHeight;
}
if (_y > stage.stageHeight)
{
traverse = true;
_y = 0;
}
dispatchEvent(new CustomEvent(CustomEvent.CHANGEPOSITION, { x:_x, y:_y ,traverse:traverse} ));
}
private function poser(e:Event):void
{
timer.start();
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.BitmapDataChannel;
import flash.display.CapsStyle;
import flash.display.JointStyle;
import flash.display.LineScaleMode;
import flash.display.Shape;
import flash.events.TimerEvent;
import flash.filters.BlurFilter;
import flash.geom.ColorTransform;
import flash.geom.Point;
import flash.utils.Timer;
class Dessin extends Bitmap
{
private var timer:Timer = new Timer(40);
private var blur:BlurFilter = new BlurFilter(1.5, 1.5,3);
private var myColorTransform:ColorTransform = new ColorTransform(.99999, .99999, .99999, 1, 2, 2, 2,0);
private var pointNull:Point = new Point();
private var dessin:Shape = new Shape();
public static var RESO:Number=5;
public function Dessin()
{
timer.addEventListener(TimerEvent.TIMER, filtrer);
timer.start();
dessin.filters = [blur];
}
private function filtrer(e:TimerEvent):void
{
bitmapData.applyFilter(bitmapData, bitmapData.rect, pointNull, blur);
bitmapData.colorTransform(bitmapData.rect, myColorTransform);smoothing=true;
}
public function line(x1:int, y1:int, x2:int, y2:int):void
{
dessin.graphics.clear();
dessin.graphics.lineStyle(3, 0,1,false,LineScaleMode.NONE,CapsStyle.ROUND,JointStyle.BEVEL,0);
dessin.graphics.moveTo(x1/RESO, y1/RESO);
dessin.graphics.lineTo(x2/RESO, y2/RESO);
bitmapData.draw(dessin);
}
}