jueves, 31 de marzo de 2011

Dibujar una flecha con el API (AS 3.0)

Es util dibujar una flecha con el API de dibujo sin necesidad de las herramientas de EDI,


package {
   import flash.display.Sprite;
    public class Arrow extends Sprite {
    public function Arrow() {
       init();
    }

    private function init() {
            graphics.lineStyle(1,0,1);
            graphics.beginFill(0xffff00);
            graphics.moveTo(-50,-25);
            graphics.lineTo(0,-25);
            graphics.lineTo(0,-50);
            graphics.lineTo(50,0);
            graphics.lineTo(0,50);
            graphics.lineTo(0,25);
            graphics.lineTo(-50,25);
            graphics.lineTo(-50,25);
            graphics.lineTo(-50,-25);
            graphics.endFill();
    }
  }
}
 
Si deseas bajar el código, pusla aqui.
Si deseas bajar el código de la clase, pulsa aqui.     

ActionScript: Simular gravedad, hacer rebotar una pelota

stage.addEventListener(MouseEvent.CLICK, onClick);
var dy = 20;
var dx = 5
var topey = bola.y;
var topex = bola.x
var gravedad = 1.5;

function onClick(e:Event):void {
   dy = 20;
   addEventListener(Event.ENTER_FRAME, onSalta);
}

function onSalta(e:Event):void {
   bola.x += dx;
   bola.y -= dy
   dy -= gravedad
   if (bola.y > topey) {
      bola.y = topey;
      removeEventListener(Event.ENTER_FRAME, onSalta);
   }
   if (bola.x > stage.stageWidth) {
      bola.x = topex;
   }
}   

 Si gustas el archivo fuente, pulsa aqui.