1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package
{
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
/**
* @author Lorenzo
*
* Test des nouvelles possibilités de l'API de dessin de Flash 10 -> copyFrom
*
* -on dessine ce que l'on veut
* -on appuye sur ENTER pour declencher la copie + rotation
* -on appuye sur SPACE pour tout remettre a zero
*/
public class Main3 extends Sprite
{
private static const THICKNESS:uint = 1;
private static const COLOR:uint = 0xFF0000;
private static const ROTATION:uint = 360/10;
private var _shapeSource:Shape;
private var _shapeCourante:Shape;
private var _spriteConteneur:Sprite;
private var _dessiner:Boolean = false;
public function Main3():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// conteneur
this._spriteConteneur = new Sprite();
this._spriteConteneur.x = stage.stageWidth / 2;
this._spriteConteneur.y = stage.stageHeight / 2;
this.addChild(this._spriteConteneur);
// shape dont on veut dupliquer la contenu
this._shapeSource = new Shape();
this._shapeSource.graphics.lineStyle(THICKNESS, COLOR);
this._spriteConteneur.addChild(this._shapeSource);
// mire
this.graphics.clear();
this.graphics.lineStyle(1, 0xFFFFFF, 0.5);
var ecart:uint = 300;
this.graphics.moveTo(stage.stageWidth / 2, stage.stageHeight / 2 - ecart);
this.graphics.lineTo(stage.stageWidth / 2, stage.stageHeight / 2 + ecart);
this.graphics.moveTo(stage.stageWidth / 2 - ecart, stage.stageHeight / 2);
this.graphics.lineTo(stage.stageWidth / 2 + ecart, stage.stageHeight / 2);
this.graphics.drawCircle(stage.stageWidth / 2, stage.stageHeight / 2, ecart);
stage.addEventListener(MouseEvent.MOUSE_DOWN, evtStageMouseDown);
stage.addEventListener(MouseEvent.MOUSE_UP, evtStageMouseUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, evtStageKeyDown);
}
private function evtStageMouseDown(event:MouseEvent):void
{
this._shapeSource.graphics.moveTo(this._shapeSource.mouseX, this._shapeSource.mouseY);
stage.addEventListener(MouseEvent.MOUSE_MOVE, evtStageMouseMove);
}
private function evtStageMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, evtStageMouseMove);
}
private function evtStageMouseMove(event:MouseEvent):void
{
this._shapeSource.graphics.lineTo(this._shapeSource.mouseX, this._shapeSource.mouseY);
}
private function evtStageKeyDown(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.ENTER :
// dupliquer les lignes de shapeSource dans une nouvelle Shape et appliquer la rotation
for (var i:Number = 0; i < 360; i+=ROTATION)
{
this._shapeCourante = new Shape();
this._spriteConteneur.addChild(this._shapeCourante);
this._shapeCourante.rotation = i;
this._shapeCourante.graphics.copyFrom(this._shapeSource.graphics);
}
break;
case Keyboard.SPACE :
// RAZ
this.removeChild(this._spriteConteneur);
init(new Event(Event.ADDED_TO_STAGE));
break;
}
}
}
} |
Partager