Sprite (x,y) defaulting to (0,0) in AS3 - actionscript-3

When executing the code:
var cont:Sprite = new Sprite();
var a:Vector.<int > = Vector.<int > ([1,2]);
var b:Vector.<Number > = Vector.<Number > ([0,0,40,40]);
cont.graphics.lineStyle(5, 0x442299);
cont.graphics.drawPath(a, b);
addChild( cont );
cont.x = 100;
cont.y = 100;
trace("X coordinate of purple line: ", cont.x);
I get the output "X coordinate of purple line: 100"
However, when I test this code and draw a line from (100, 100) to (140, 140) with the mouse:
var line:Sprite = new Sprite();
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler);
var startX:int = -1;
var startY:int = -1;
function mouseDownHandler(event:MouseEvent):void
{
startX = mouseX;
startY = mouseY;
}
function mouseUpHandler(event:MouseEvent):void
{
swype(Vector.<int> ([1,2]), Vector.<Number> ([startX,startY,mouseX,mouseY]));
}
function swype(commands:Vector.<int>, coords:Vector.<Number>):void
{
var container:Sprite = new Sprite();
container.graphics.lineStyle(5, 0x0066CC);
container.graphics.drawPath(commands, coords);
addChild( container );
container.x = 100;
container.y = 100;
trace("X coordinate of blue line: ", container.x);
}
I get the output: "X coordinate of blue line: 0"
Why is it that when I am getting the coordinates from the mouse's position on the screen and adding them to the vector, the Sprite container's x and y coordinates default to 0,0?

Not sure what you are wanting to do but you may need to use the localToGlobal methods if you keep moving that _container around ;)
This has to do with the context of where you get the property of the mouseX and mouseY.
You have to think more about the display stack and how that relates to what the properties of mouseX and Y will give you.
Have a try of this and shoot me any questions if you cannot understand why it works.
package
{
import flash.display.Sprite;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
public class Testing extends Sprite
{
private var startX:Number;
private var startY:Number;
private var _container:Sprite = new Sprite ();
public function Testing ()
{
stage.quality = StageQuality.BEST;
stage.align = "TL";
stage.scaleMode = StageScaleMode.NO_SCALE;
// if you comment this it will stuff up the first swype
_container.x = _container.y = 100;
var cont:Sprite = new Sprite ();
var a:Vector.<int> = Vector.<int> ( [1, 2] );
var b:Vector.<Number> = Vector.<Number> ( [0, 0, 40, 40] );
cont.graphics.lineStyle ( 5 , 0x442299 );
cont.graphics.drawPath ( a , b );
addChild ( cont );
cont.x = 100;
cont.y = 100;
trace ( "X coordinate of purple line: " , cont.x );
var line:Sprite = new Sprite ();
addChild ( _container );
stage.addEventListener ( MouseEvent.MOUSE_DOWN , mouseDownHandler );
stage.addEventListener ( MouseEvent.MOUSE_UP , mouseUpHandler );
var startX:int = - 1;
var startY:int = - 1;
}
function mouseDownHandler ( event:MouseEvent ):void
{
startX = _container.mouseX;
startY = _container.mouseY;
}
function mouseUpHandler ( event:MouseEvent ):void
{
swype ( Vector.<int> ( [1, 2] ) , Vector.<Number> ( [startX, startY, _container.mouseX, _container.mouseY] ) );
}
function swype ( commands:Vector.<int> , coords:Vector.<Number> ):void
{
_container.graphics.lineStyle ( 5 , 0x0066CC );
_container.graphics.drawPath ( commands , coords );
//this moves it after the property was collected for the initial swype which you don't want
_container.x = 100;
_container.y = 100;
trace ( "X coordinate of blue line: " , _container.x );
}
}
}

Related

How to scale GraphicsPathCommand data?

Context: For a legacy Flex/Actionscript drawing app, I need to add scaling of simple symbols. The app uses the Graffiti lib for drawing and the resulting shape data is stored as GraphicsPathCommands serialized to XML using the Degrafa lib for save and reload. I need to enable the user to scale these graphics and then get updated path data which can be serialized. The symbols are simple but more complicated than simple geometry. For example:
Question: I converted the SVG data for this symbol to Actionscript GraphicsPathCommands and am able to draw it, and of course translation is easy – but I don't know how I would scale it, given a bounding box defined by a user dragging out a marquee rectangle in the app.
Does anyone know of either an Actionscript way of transforming the command data, or a Javascript snippet for scaling SVG which I can port to Actionscript?
For reference, an example of the Actionscript GraphicsPathCommands for drawing a star is below.
public function DrawPathExample()
{
var star_commands:Vector.<int> = new Vector.<int>(5, true);
star_commands[0] = GraphicsPathCommand.MOVE_TO;
star_commands[1] = GraphicsPathCommand.LINE_TO;
star_commands[2] = GraphicsPathCommand.LINE_TO;
star_commands[3] = GraphicsPathCommand.LINE_TO;
star_commands[4] = GraphicsPathCommand.LINE_TO;
var star_coord:Vector.<Number> = new Vector.<Number>(10, true);
star_coord[0] = 66; //x
star_coord[1] = 10; //y
star_coord[2] = 23;
star_coord[3] = 127;
star_coord[4] = 122;
star_coord[5] = 50;
star_coord[6] = 10;
star_coord[7] = 49;
star_coord[8] = 109;
star_coord[9] = 127;
graphics.beginFill(0x003366);
graphics.drawPath(star_commands, star_coord);
}
Solution
A full solution for interactively scaling GraphicsPathCommand data is below. The path data was derived from an SVG put through this SVGParser. It generates path drawing commands in the form of graphics.lineTo(28.4,16.8);. A couple of utility functions separate the data from the commands and store them in Vectors so the data can be serialized. I don't need to use arbitrary SWGs so I just hardcoded the data.
package classes
{
import flash.display.GraphicsPathCommand;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.geom.Rectangle;
public class DrawSVG extends Sprite
{
private var startPt:Point = new Point();
private var selectRect:Rectangle = new Rectangle();
private var viewBox:Rectangle = new Rectangle();
protected var commands:Vector.<int> = new Vector.<int>();
protected var drawingData:Vector.<Number> = new Vector.<Number>();
protected var sourceDrawingData:Vector.<Number> = new Vector.<Number>();
public function DrawSVG()
{
super();
this.addEventListener(Event.ADDED_TO_STAGE, setup);
setupWomanData();
}
private function setup(event:Event):void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
}
private function onMouseDown(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
this.graphics.clear();
// offset so graphic draws centered on click point
startPt = new Point(event.stageX - (viewBox.width /2), event.stageY - (viewBox.height /2));
selectRect = new Rectangle(startPt.x, startPt.y, viewBox.width, viewBox.height);
var kx:Number = selectRect.width / (viewBox.width);
var ky:Number = selectRect.height / (viewBox.height);
var scaleFactor:Number = kx < ky ? kx : ky;
drawSymbol(scaleFactor);
this.graphics.lineStyle(1, 0x000000);
this.graphics.drawRect(selectRect.x, selectRect.y, selectRect.width, selectRect.height);
}
private function onMouseMove(event:MouseEvent):void
{
selectRect.width = Math.max(viewBox.width, Math.abs(event.stageX - startPt.x));
selectRect.height = Math.max(viewBox.height, Math.abs(event.stageY - startPt.y));
var kx:Number = selectRect.width / (viewBox.width);
var ky:Number = selectRect.height / (viewBox.height);
var scaleFactor:Number = kx < ky ? kx : ky;
this.graphics.clear();
drawSymbol(scaleFactor);
this.graphics.lineStyle(1, 0x000000);
this.graphics.drawRect(selectRect.x, selectRect.y, viewBox.width * scaleFactor, viewBox.height * scaleFactor);
}
private function onMouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
this.graphics.clear();
createSprite(commands, drawingData);
}
private function drawSymbol(toScale:Number):void
{
drawingData.length = 0;
for (var i:int = 0; i < sourceDrawingData.length; i++) {
drawingData[i] = Math.max(sourceDrawingData[i], sourceDrawingData[i] * toScale);
drawingData[i] += i % 2 == 0 ? startPt.x : startPt.y ;
}
this.graphics.clear();
this.graphics.lineStyle();
this.graphics.beginFill(0xff0000);
this.graphics.drawPath(commands, drawingData);
this.graphics.endFill();
}
private function createSprite(command:Vector.<int>, coord:Vector.<Number>):Shape{
var s:Shape = new Shape();
addChild(s);
s.graphics.beginFill(0xff);
s.graphics.drawPath(command, coord);
s.graphics.endFill();
return s;
}
private function setupWomanData():void
{
commands = new Vector.<int>();
drawingData = new Vector.<Number>();
viewBox= new Rectangle(0, 0, 24.629, 52.336);
addMoveToCmd(12.31,10.3);
addCurveToCmd(13.37,10.3,14.3,9.89);
addCurveToCmd(15.24,9.48,15.94,8.78);
addCurveToCmd(16.64,8.08,17.05,7.14);
addCurveToCmd(17.46,6.2,17.46,5.15);
addCurveToCmd(17.46,4.1,17.05,3.16);
addCurveToCmd(16.64,2.23,15.94,1.52);
addCurveToCmd(15.24,0.82,14.3,0.41);
addCurveToCmd(13.37,0,12.31,0);
addCurveToCmd(11.26,0,10.33,0.41);
addCurveToCmd(9.39,0.82,8.69,1.52);
addCurveToCmd(7.98,2.23,7.57,3.16);
addCurveToCmd(7.16,4.1,7.16,5.15);
addCurveToCmd(7.16,6.2,7.57,7.14);
addCurveToCmd(7.98,8.08,8.69,8.78);
addCurveToCmd(9.39,9.48,10.33,9.89);
addCurveToCmd(11.26,10.3,12.31,10.3);
addLineToCmd(12.314,10.304);
addMoveToCmd(24.6,26.36);
addLineToCmd(20.7,12.77);
addCurveToCmd(20.62,12.3,20.39,11.91);
addCurveToCmd(20.15,11.51,19.81,11.23);
addCurveToCmd(19.47,10.94,19.04,10.78);
addCurveToCmd(18.61,10.62,18.14,10.62);
addLineToCmd(6.49,10.62);
addCurveToCmd(6.02,10.62,5.59,10.78);
addCurveToCmd(5.16,10.94,4.82,11.23);
addCurveToCmd(4.48,11.51,4.24,11.91);
addCurveToCmd(4.01,12.3,3.93,12.77);
addLineToCmd(0.03,26.36);
addCurveToCmd(0.01,26.4,0.01,26.45);
addCurveToCmd(-0.01,26.5,-0.01,26.55);
addCurveToCmd(0.01,26.6,0.01,26.65);
addCurveToCmd(0.02,26.69,0.03,26.74);
addCurveToCmd(-0.15,27.95,0.55,28.69);
addCurveToCmd(1.25,29.44,2.2,29.6);
addCurveToCmd(3.15,29.77,4.05,29.3);
addCurveToCmd(4.95,28.84,5.17,27.63);
addLineToCmd(6.85,21.37);
addLineToCmd(4.07,34.88);
addCurveToCmd(3.81,35.51,3.91,36.15);
addCurveToCmd(4,36.78,4.35,37.3);
addCurveToCmd(4.7,37.81,5.26,38.13);
addCurveToCmd(5.81,38.45,6.49,38.45);
addLineToCmd(6.78,38.45);
addLineToCmd(6.78,49.72);
addCurveToCmd(6.78,50.99,7.59,51.62);
addCurveToCmd(8.41,52.25,9.39,52.25);
addCurveToCmd(10.37,52.25,11.19,51.62);
addCurveToCmd(12,50.99,12,49.72);
addLineToCmd(12,38.45);
addLineToCmd(12.63,38.45);
addLineToCmd(12.63,49.72);
addCurveToCmd(12.63,50.99,13.44,51.62);
addCurveToCmd(14.26,52.25,15.24,52.25);
addCurveToCmd(16.22,52.25,17.04,51.62);
addCurveToCmd(17.85,50.99,17.85,49.72);
addLineToCmd(17.85,38.45);
addLineToCmd(18.14,38.45);
addCurveToCmd(18.82,38.45,19.38,38.13);
addCurveToCmd(19.93,37.81,20.28,37.3);
addCurveToCmd(20.63,36.78,20.72,36.14);
addCurveToCmd(20.81,35.51,20.56,34.87);
addLineToCmd(17.78,21.37);
addLineToCmd(19.45,27.58);
addCurveToCmd(19.67,28.79,20.57,29.27);
addCurveToCmd(21.47,29.75,22.43,29.6);
addCurveToCmd(23.38,29.45,24.08,28.7);
addCurveToCmd(24.78,27.96,24.6,26.74);
addCurveToCmd(24.61,26.69,24.62,26.65);
addCurveToCmd(24.63,26.6,24.63,26.55);
addCurveToCmd(24.63,26.5,24.62,26.45);
addCurveToCmd(24.62,26.4,24.6,26.36);
addLineToCmd(24.601,26.356);
}
protected function addCurveToCmd(p1:Number, p2:Number, p3:Number, p4:Number):void
{
commands.push(GraphicsPathCommand.CURVE_TO);
sourceDrawingData.push(p1);
sourceDrawingData.push(p2);
sourceDrawingData.push(p3);
sourceDrawingData.push(p4);
}
protected function addMoveToCmd(p1:Number, p2:Number):void
{
commands.push(GraphicsPathCommand.MOVE_TO);
sourceDrawingData.push(p1);
sourceDrawingData.push(p2);
}
protected function addLineToCmd(p1:Number, p2:Number):void
{
commands.push(GraphicsPathCommand.LINE_TO);
sourceDrawingData.push(p1);
sourceDrawingData.push(p2);
}
}
}
Seems like there is a pretty straightforward way to do this. It looks like the only thing to scale are the coordinates themselves, so you may just apply a scale factor.
Based on your example:
public function ASEntryPoint() {
var star_commands:Vector.<int> = new Vector.<int>(5, true);
star_commands[0] = GraphicsPathCommand.MOVE_TO;
star_commands[1] = GraphicsPathCommand.LINE_TO;
star_commands[2] = GraphicsPathCommand.LINE_TO;
star_commands[3] = GraphicsPathCommand.LINE_TO;
star_commands[4] = GraphicsPathCommand.LINE_TO;
var star_coord:Vector.<Number> = new Vector.<Number>(10, true);
star_coord[0] = 66; //x
star_coord[1] = 10; //y
star_coord[2] = 23;
star_coord[3] = 127;
star_coord[4] = 122;
star_coord[5] = 50;
star_coord[6] = 10;
star_coord[7] = 49;
star_coord[8] = 109;
star_coord[9] = 127;
//reference shape to detect initial size
var s:Shape = shapeInRect(star_commands, star_coord);
var bounds:Rectangle = s.getBounds(s);
s.graphics.lineStyle(1);
s.graphics.drawRect(bounds.x, bounds.y, bounds.width, bounds.height);
addChild(s);
//fit to target
var targetSize:Rectangle = new Rectangle(150, 100, 75, 60);
//detect lesser factor - assuming you need to preserve proportions
var kx:Number = targetSize.width / (bounds.width);
var ky:Number = targetSize.height / (bounds.height);
var toUse:Number = kx < ky ? kx : ky;
//apply to coords
for (var i:int = 0; i < star_coord.length; i++) {
//size
star_coord[i] *= toUse;
//fix initial offset
star_coord[i] -= i % 2 == 0 ? bounds.x * toUse : bounds.y * toUse;
}
//draw
addChild(shapeInRect(star_commands, star_coord, targetSize));
}
private function shapeInRect(command:Vector.<int>, coord:Vector.<Number>, rect:Rectangle = null):Shape{
var s:Shape = new Shape();
addChild(s);
s.graphics.beginFill(0x003366);
s.graphics.drawPath(command, coord);
s.graphics.endFill();
if (rect){
s.graphics.lineStyle(1);
s.graphics.drawRect(0, 0, rect.width, rect.height);
s.x = rect.x;
s.y = rect.y;
}
return s;
}

Flow Field Actionscript: How to change triangles (boids) appearance to png image?

I was wondering if there was a way to change the appearance of the "particles" to an image that I've created. I found the lab at Lab: Simulating Flocking Behavior with Perlin Noise.
I've been trying to figure out how can I upload birds that I've created into the sketch.
Would anyone care to help me figure out if it's even possible?
/**
*
* FlowField v1.00
* 14/12/2008 17:53
*
* © JUSTIN WINDLE | soulwire ltd
* http://blog.soulwire.co.uk
*
**/
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.geom.ColorTransform;
import flash.geom.Point;
import flash.utils.getTimer;
public class FlowField extends Sprite
{
//___________________________________________________________ _____
//————————————————————————————————————————————— CLASS MEMBERS VALUE
private static const MARGIN: int = 10;
private static const UI_Y_OPEN: int = 200;
private static const UI_Y_CLOSED: int = 314;
private static const FADING: ColorTransform = new ColorTransform(1, 1, 1, 0.99);
//———————————————————————————————————————————————————————————
private var _offsets: Array = [];
private var _offsetSpeed1: Number = 2.0;
private var _offsetSpeed2: Number = -2.0;
private var _speedMultiplier: Number = 2.0;
private var _numParticles: int = 1000;
private var _noiseSeed: int = 1000;
private var _baseX: int = 180;
private var _baseY: int = 180;
private var _settingsOpen: Boolean = false;
private var _useBlitting: Boolean = false;
private var _renderBMD: BitmapData;
private var _noiseBMD: BitmapData;
private var _renderBMP: Bitmap;
private var _noiseBMP: Bitmap;
private var _particles: Sprite;
//___________________________________________________________
//————————————————————————————————————————— GETTERS + SETTERS
public function set numParticles( value:int ):void
{
_numParticles = value;
ui.particlesTF.text = value.toString();
if ( _numParticles > _particles.numChildren )
{
var particle:Particle;
while ( _particles.numChildren < _numParticles )
{
particle = new Particle();
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
_particles.addChild( particle );
}
}
else if ( _numParticles < _particles.numChildren )
{
while ( _particles.numChildren > _numParticles )
{
_particles.removeChildAt( _numParticles );
}
}
}
public function set noiseSeed( value:int ):void
{
_noiseSeed = value;
ui.seedTF.text = value.toString();
}
public function set baseX( value:int ):void
{
_baseX = value;
ui.baseXTF.text = value.toString();
}
public function set baseY( value:int ):void
{
_baseY = value;
ui.baseYTF.text = value.toString();
}
public function set offsetSpeed1( value:Number ):void
{
_offsetSpeed1 = value;
ui.offset1TF.text = value.toString();
}
public function set offsetSpeed2( value:Number ):void
{
_offsetSpeed2 = value;
ui.offset2TF.text = value.toString();
}
public function set speedMultiplier( value:Number ):void
{
_speedMultiplier = value;
ui.pSpeedTF.text = value.toString();
}
public function set useBlitting( value:Boolean ):void
{
_useBlitting = value;
if ( _useBlitting )
{
Particle.COLOUR = 0xFFFFFF;
removeChild( _particles );
addChild( _renderBMP );
}
else
{
Particle.COLOUR = 0x111111;
removeChild( _renderBMP );
addChild( _particles );
}
for (var i:int = 0; i < _numParticles; i++)
{
Particle( _particles.getChildAt(i) ).draw();
}
addChild( _noiseBMP );
addChild( ui );
}
//___________________________________________________________
//——————————————————————————————————————————————— CONSTRUCTOR
public function FlowField()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.quality = StageQuality.MEDIUM;
stage.align = StageAlign.TOP_LEFT;
_offsets.push( new Point() );
_offsets.push( new Point() );
_noiseSeed = Math.random() * 2000 << 0;
_renderBMD = new BitmapData( stage.stageWidth, stage.stageHeight, false, 0 );
_renderBMP = new Bitmap( _renderBMD );
addChild( _renderBMP );
_noiseBMD = new BitmapData( stage.stageWidth, stage.stageHeight );
_noiseBMD = new BitmapData( stage.stageWidth + (MARGIN * 4), stage.stageHeight + (MARGIN * 4), false );
_noiseBMP = new Bitmap( _noiseBMD );
_noiseBMP.visible = false;
addChild( _noiseBMP );
createParticles();
configureListeners();
useBlitting = false;
}
//___________________________________________________________
//——————————————————————————————————————————————————— METHODS
//———————————————————————————————————————————————————————————
private function createParticles():void
{
_particles = new Sprite();
addChild( _particles );
addChild( ui );
for (var i:int = 0; i < _numParticles; i++)
{
var particle:Particle = new Particle();
particle.x = Math.random() * stage.stageWidth;
particle.y = Math.random() * stage.stageHeight;
_particles.addChild( particle );
}
}
private function configureListeners():void
{
ui.particlesSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.seedSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.baseXSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.baseYSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.offset1Slider.addEventListener( Event.CHANGE, onSliderChange );
ui.offset2Slider.addEventListener( Event.CHANGE, onSliderChange );
ui.pSpeedSlider.addEventListener( Event.CHANGE, onSliderChange );
ui.showNoiseCB.addEventListener( Event.CHANGE, onCBSelect );
ui.blitCB.addEventListener( Event.CHANGE, onCBSelect );
ui.particlesSlider.value = _numParticles;
ui.particlesTF.text = _numParticles.toString();
ui.seedSlider.value = _noiseSeed;
ui.seedTF.text = _noiseSeed.toString();
ui.baseXSlider.value = _baseX;
ui.baseXTF.text = _baseX.toString();
ui.baseYSlider.value = _baseY;
ui.baseYTF.text = _baseX;
ui.offset1Slider.value = _offsetSpeed1;
ui.offset1TF.text = _offsetSpeed1.toString();
ui.offset2Slider.value = _offsetSpeed2;
ui.offset2TF.text = _offsetSpeed2.toString();
ui.pSpeedSlider.value = _speedMultiplier;
ui.pSpeedTF.text = _speedMultiplier.toString();
addEventListener( Event.ENTER_FRAME, update );
}
//___________________________________________________________
//———————————————————————————————————————————— EVENT HANDLERS
private function update( event:Event ):void
{
_offsets[0].x += _offsetSpeed1;
_offsets[0].y += _offsetSpeed1;
_offsets[1].x += _offsetSpeed2;
_offsets[1].y += _offsetSpeed2;
_noiseBMD.perlinNoise( _baseX, _baseY, 2, _noiseSeed, false, true, 10, true, _offsets );
var particle:Particle;
var brightness:Number;
var radians:Number;
var angle:Number;
var speed:Number;
var pixel:int;
for (var i:int = 0; i < _particles.numChildren; i++)
{
particle = _particles.getChildAt(i) as Particle;
pixel = _noiseBMD.getPixel( particle.x + (MARGIN *2), particle.y + (MARGIN *2) );
brightness = pixel / 0xFFFFFF;
speed = 0.1 + brightness * particle.speed * _speedMultiplier;
angle = 360 * brightness * particle.wander;
radians = angle * Math.PI / 180;
particle.x += Math.cos( radians ) * speed;
particle.y += Math.sin( radians ) * speed;
particle.scaleX = particle.scaleY = 0.1 + brightness * (_useBlitting ? 4 : 6);
particle.rotation = angle;
if ( particle.x < -MARGIN ) particle.x = stage.stageWidth + MARGIN;
else if ( particle.x > stage.stageWidth + MARGIN ) particle.x = -MARGIN;
if ( particle.y < -MARGIN ) particle.y = stage.stageHeight + MARGIN;
else if ( particle.y > stage.stageHeight + MARGIN ) particle.y = -MARGIN;
}
if ( _useBlitting )
{
_renderBMD.colorTransform( _renderBMD.rect, FADING );
_renderBMD.draw( _particles );
}
if ( !_settingsOpen && mouseY > UI_Y_CLOSED )
{
_settingsOpen = true;
}
else if ( _settingsOpen && mouseY < UI_Y_OPEN )
{
_settingsOpen = false;
}
if ( _settingsOpen && ui.y > UI_Y_OPEN )
{
ui.y += (UI_Y_OPEN - ui.y) / 3;
}
else if ( !_settingsOpen && ui.y < UI_Y_CLOSED )
{
ui.y += (UI_Y_CLOSED -ui.y) / 3;
}
}
private function onSliderChange( event:Event ):void
{
switch( event.currentTarget )
{
case ui.particlesSlider : numParticles = event.currentTarget.value; break;
case ui.seedSlider : noiseSeed = event.currentTarget.value; break;
case ui.baseXSlider : baseX = event.currentTarget.value; break;
case ui.baseYSlider : baseY = event.currentTarget.value; break;
case ui.offset1Slider : offsetSpeed1 = event.currentTarget.value; break;
case ui.offset2Slider : offsetSpeed2 = event.currentTarget.value; break;
case ui.pSpeedSlider : speedMultiplier = event.currentTarget.value; break;
}
}
private function onCBSelect( event:Event ):void
{
switch( event.currentTarget )
{
case ui.showNoiseCB : _noiseBMP.visible = ui.showNoiseCB.selected; break;
case ui.blitCB : useBlitting = ui.blitCB.selected; break;
}
}
}
}
import flash.display.Shape;
internal class Particle extends Shape
{
public static var COLOUR:uint = 0x111111;
public static const MIN_SPEED:int = 1;
public static const MAX_SPEED:int = 4;
public static const MIN_WANDER:Number = 0.8;
public static const MAX_WANDER:Number = 1.2;
public var speed:Number;
public var wander:Number;
public function Particle()
{
speed = Math.random() * (MAX_SPEED - MIN_SPEED) + MIN_SPEED;
wander = Math.random() * (MAX_WANDER - MIN_WANDER) + MIN_WANDER;
draw();
}
public function draw()
{
graphics.clear();
graphics.beginFill( COLOUR );
graphics.moveTo( -1, -0.5);
graphics.lineTo( 1, 0);
graphics.lineTo( -1, 0.5);
graphics.lineTo( -1, -0.5);
graphics.endFill();
}
}

Get the upper, bottom, rightmost and leftmost point of a pixel-perfect BitmapData collision

How can I get the upper, bottom, rightmost and leftmost point of a pixel-perfect BitmapData collision? This is my collision-detection code:
public static function checkCollision(object1:*, object2:*, debug:Boolean = false):Boolean{
var object1Rect:Rectangle = object1.getRect(stage);
var object2Rect:Rectangle = object2.getRect(stage);
var object1Point:Point = new Point(object1Rect.x, object1Rect.y);
var object2Point:Point = new Point(object2Rect.x, object2Rect.y);
var bitmapData1:BitmapData = new BitmapData(
object1Rect.width,
object1Rect.height,
true,
0
);
var bitmapData2:BitmapData = new BitmapData(
object2Rect.width,
object2Rect.height,
true,
0
);
var clr:ColorTransform = new ColorTransform();
if(debug)
clr.color = 0x00ff00;
bitmapData1.draw(object1, new Matrix(1, 0, 0, 1, -object1Rect.x, -object1Rect.y), clr);
bitmapData2.draw(object2, null, clr);
if(debug){
if(bmp1.stage)
stage.removeChild(bmp1);
bmp1 = new Bitmap(bitmapData1);
bmp1.x = object1Point.x;
bmp1.y = object1Point.y;
stage.addChild(bmp1);
if(bmp2.stage)
stage.removeChild(bmp2);
bmp2 = new Bitmap(bitmapData2);
bmp2.x = object2Point.x;
bmp2.y = object2Point.y;
stage.addChild(bmp2);
}
var bCollide:Boolean = bitmapData1.hitTest(
object1Point,
255,
bitmapData2,
object2Point,
255
);
if(!debug){
bitmapData1.dispose();
bitmapData2.dispose();
}
return bCollide;
}
And it works perfeclty fine. However, the code I use to detect the top hitpoint doesn't work properly. This is the code:
public static function getHitPoint(object1:*, object2:*):Point{
var point:Point = new Point();
var object1Rect:Rectangle = object1.getRect(stage);
var object2Rect:Rectangle = object2.getRect(stage);
var object1Point:Point = new Point(object1Rect.x, object1Rect.y);
var object2Point:Point = new Point(object2Rect.x, object2Rect.y);
var bitmapData1:BitmapData = new BitmapData(
object1.width,
object1.height,
true,
0
);
var bitmapData2:BitmapData = new BitmapData(
object2.width,
object2.height,
true,
0
);
bitmapData1.draw(object1, new Matrix(1, 0, 0, 1, -object1Rect.x, -object1Rect.y));
bitmapData2.draw(object2);
var bitmap1:Bitmap = new Bitmap(bitmapData1);
var bitmap2:Bitmap = new Bitmap(bitmapData2);
bitmap1.x = object1Point.x;
bitmap1.y = object1Point.y;
bitmap2.x = object2Point.x;
bitmap2.y = object2Point.y;
var bitmapOrigin:Point = new Point(object1Point.x, object1Point.y);
var bitmap2OriginLocal:Point = bitmap2.globalToLocal(bitmapOrigin);
var overlappingPixels:Vector.<uint> = bitmap2.bitmapData.getVector(
new Rectangle(bitmap2OriginLocal.x, bitmap2OriginLocal.y, object1Rect.width, object1Rect.height)
);
for(var i:String in overlappingPixels){
var index:uint = uint(i);
if(overlappingPixels[i] != 0){
point.x = (index % object1.width) + (bitmap2.x > bitmap1.x ? bitmap2.x : bitmap1.x);
point.y = (uint(index / bitmap1.height)) + (bitmap2.y > bitmap1.y ? bitmap2.y : bitmap1.y);
break;
}
}
return point;
}
I've got no idea why, but the getHitPoint() function sometimes returns the wrong coordinates. Can anyone please explain why that is? And how can I detect the bottommost, the leftmost and the rightmost hitpoint?
Edit
I now know why getHitPoint() sometimes returned a wrong value: point.y = (uint(index / bitmap1.height)) + (bitmap2.y > bitmap1.y ? bitmap2.y : bitmap1.y); should be point.y = (uint(index/bitmap1.width)) + (bitmap2.y > bitmap1.y ? bitmap2.y : bitmap1.y);
Edit 2
I found out how to get the bottom hitpoint:
public static function getHitPoint(object1:*, object2:*, direction:int = 0):*{
var point:Point = new Point();
var object1Rect:Rectangle = object1.getRect(stage);
var object2Rect:Rectangle = object2.getRect(stage);
var object1Point:Point = new Point(object1Rect.x, object1Rect.y);
var object2Point:Point = new Point(object2Rect.x, object2Rect.y);
var bitmapData1:BitmapData = new BitmapData(
Math.round(object1Rect.width),
Math.round(object1Rect.height),
true,
0
);
var bitmapData2:BitmapData = new BitmapData(
Math.round(object2Rect.width),
Math.round(object2Rect.height),
true,
0
);
bitmapData1.draw(object1, new Matrix(1, 0, 0, 1, -object1Rect.x, -object1Rect.y));
bitmapData2.draw(object2);
var bitmap1:Bitmap = new Bitmap(bitmapData1);
var bitmap2:Bitmap = new Bitmap(bitmapData2);
bitmap1.x = object1Point.x;
bitmap1.y = object1Point.y;
bitmap2.x = object2Point.x;
bitmap2.y = object2Point.y;
var bitmapOrigin:Point = new Point(object1Point.x, object1Point.y);
var bitmap2OriginLocal:Point = bitmap2.globalToLocal(bitmapOrigin);
var overlappingPixels:Vector.<uint> = bitmap2.bitmapData.getVector(
new Rectangle(bitmap2OriginLocal.x, bitmap2OriginLocal.y, object1Rect.width, object1Rect.height)
);
switch(direction){
case 0: //top
for(var i:String in overlappingPixels){
var index:uint = uint(i);
if(overlappingPixels[i] != 0){
point.x = (index % bitmap1.width) + (bitmap2.x > bitmap1.x ? bitmap2.x : bitmap1.x);
point.y = (uint((index)/bitmap1.width)) + (bitmap2.y > bitmap1.y ? bitmap2.y : bitmap1.y);
return point;
}
}
case 1: //right
// I still need this
case 2: //bottom
overlappingPixels.reverse();
for(var i:String in overlappingPixels){
var index:uint = uint(i);
if(overlappingPixels[i] != 0){
point.x = bitmap1.width - (index % bitmap1.width) + (bitmap2.x > bitmap1.x ? bitmap2.x : bitmap1.x);
point.y = (bitmap2.y + bitmap2.height > bitmap1.y + bitmap1.height ? bitmap1.y + bitmap1.height : bitmap2.y + bitmap2.height) - (uint(index/bitmap1.width));
return point;
}
}
case 3: //left
// I still need this too
}
return false;
}
I still need a way to get the left and rightmost hitpoints though
You don't need to do it like you're doing there. You can do it all within a single function, which returns everything back correctly. I've added comments to the below. Please take note of what I've changed, as when you're trying to do it as you're doing now, with the code you changed, it is impossible.
This works for any shape, any direction. It'll give you the exact X and Y of the collision.
Please do not make this into a static function. Put it into a global class and use a Singleton to manage it instead. Things start to go very badly wrong when you being using static functions and reference the stage.
Also, if you're going to be working with pixel values of less than 1 (ie 99.75), the below will need a bit of adapting to cater for that. I've assumed you're using whole pixels, given your Math.round usage.
/**
*
* #param object1
* #param object2
* #return
*/
private function getHitPoint(object1:*, object2:*):*{
var point:Point;
// X and Y where we hit
// do NOT change this to a stage location or it does NOT work
var object1Point:Point = new Point(object1.x, object1.y);
var object2Point:Point = new Point(object2.x, object2.y);
var bitmapData1:BitmapData = new BitmapData(
Math.round(object1.width),
Math.round(object1.height),
true,
0
);
var bitmapData2:BitmapData = new BitmapData(
Math.round(object2.width),
Math.round(object2.height),
true,
0
);
// Draw
bitmapData1.draw(object1, new Matrix(1, 0, 0, 1, -object1.x, -object1.y));
bitmapData2.draw(object2);
// Create BMP's
var bitmap1:Bitmap = new Bitmap(bitmapData1);
var bitmap2:Bitmap = new Bitmap(bitmapData2);
// Set X and Y and BMP
bitmap1.x = object1Point.x;
bitmap1.y = object1Point.y;
bitmap2.x = object2Point.x;
bitmap2.y = object2Point.y;
// BMP origin is the object1 X and Y
var bitmapOrigin:Point = new Point(object1Point.x, object1Point.y);
// Create a local version of the bitmap2 so we can see what is overlapping
var bitmap2OriginLocal:Point = bitmap2.globalToLocal(bitmapOrigin);
// Create a rectangle from what we now know
var rect:Rectangle = new Rectangle(bitmap2OriginLocal.x, bitmap2OriginLocal.y, object1.width, object1.height);
// The overlapping pixels are within the rectangle, so get them all
var overlappingPixels:Vector.<uint> = bitmap2.bitmapData.getVector(rect);
// Run through all the overlapping pixels until we find a colourful one
for (var i:uint = 0; i < overlappingPixels.length; i++ ) {
var index:uint = overlappingPixels[i];
// If the colour is not 0, we have found it
if(index != 0){
point = new Point();
// Basically, instead of using width and getting 100, we're working out how
// many pixles across the overlap is. The Vector doesn't tell us this, so we need to work it out
var overlappingWidth:uint = object1.width - Math.abs(bitmap2OriginLocal.x);
// The Y is object1.y, minus the local y, plus object1's width minus the X from the local
point.y = object1.y - bitmap2OriginLocal.y + uint(i / overlappingWidth);
// The X is the same as above, but % of the width
point.x = object1.x - bitmap2OriginLocal.x + (i % overlappingWidth);
// Found it, we're done
break;
}
}
// Only fires when you've got a collision that is less than 1 pixel from the width or height
// Just a fail safe
if (!point) {
point = new Point(object1.width, object1.height);
}
return point;
}
For context, my entire class is below which shows how I was using this function. You can copy/paste this class and it will work. It shows how you move sprites around the screen, once it finds a collision, then it works out where the collision took place.
This class is for absolute pixel perfect collision detection, including an example.
package kazo
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.geom.Matrix;
/**
* ...
* #author KM
*/
public class TestCases2 extends Sprite
{
private var rect :Sprite;
private var circle :Sprite;
/**
*
*/
public function TestCases2()
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
public function init(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
trace('Starting test case');
// Rectangle
rect = new Sprite();
// Circle
circle = new Sprite();
// Draw the rectangle. Center point must be TOP LEFT
// If you're using Flash Professional, place everything at 0,0 inside the MC
rect.graphics.beginFill(0xff0000);
rect.graphics.drawRect(0, 0, 100, 100);
rect.graphics.endFill();
// Draw the circle. Center point is TOP LEFT, so the X and Y of the circle need to be equal to the radius
circle.graphics.beginFill(0xffff00);
circle.graphics.drawCircle(50, 50, 50);
circle.graphics.endFill();
// Add them
addChild(rect);
addChild(circle);
// Position
rect.x = 225;
rect.y = 75;
// Position
circle.x = 225;
circle.y = 225;
// Frame loop
addEventListener(Event.ENTER_FRAME, frameFunc);
}
/**
*
* #param e
*/
private function frameFunc(e:Event):void {
// move them around
circle.y -= 2;
circle.x += 0;
rect.y += 1;
rect.x += 0;
// Check for collisions. If found, stop. Pass 'true' as the final param if you want it to draw to the screen
if (checkCollision(rect, circle)) {
var ref:Point = getHitPoint(rect, circle);
// Draws where the collision hit
var loc:Sprite = new Sprite();
loc.graphics.beginFill(0x000000);
loc.graphics.drawRect(0, 0, 10, 10);
loc.graphics.endFill();
addChild(loc);
loc.x = ref.x;
loc.y = ref.y;
trace(ref);
removeEventListener(Event.ENTER_FRAME, frameFunc);
}
}
/**
*
* #param _obj1
* #param _obj2
* #param _debug
* #return
*/
private function checkCollision(_obj1:Sprite, _obj2:Sprite, _debug:Boolean = false):Boolean {
// Draw the first item to bitmapdata
var bmd1:BitmapData = new BitmapData(_obj1.width, _obj1.height, true, 0);
// ..and the second
var bmd2:BitmapData = new BitmapData(_obj2.width, _obj2.height, true, 0);
// Now draw them
bmd1.draw(_obj1);
bmd2.draw(_obj2);
// If we're in debug, also add the bitmap to the stage so we can see where we are
if (_debug) {
var bmp:Bitmap = new Bitmap(bmd1);
bmp.x = _obj1.x;
bmp.y = _obj1.y;
addChild(bmp);
var bmp2:Bitmap = new Bitmap(bmd2);
bmp2.x = _obj2.x;
bmp2.y = _obj2.y;
addChild(bmp2);
}
// Hit test including alpha channel. Obj1 X/Y, Obj2 X/Y, alpha channel
var rtn:Boolean = bmd1.hitTest(new Point(_obj1.x, _obj1.y), 255, bmd2, new Point(_obj2.x, _obj2.y), 255);
// Dispose the bitmap data, we dont need it anymore
if (!_debug) {
bmd1.dispose();
bmd2.dispose();
}
// Return the boolean
return rtn;
}
/**
*
* #param object1
* #param object2
* #return
*/
private function getHitPoint(object1:*, object2:*):*{
var point:Point;
// X and Y where we hit
// do NOT change this to a stage location or it does NOT work
var object1Point:Point = new Point(object1.x, object1.y);
var object2Point:Point = new Point(object2.x, object2.y);
var bitmapData1:BitmapData = new BitmapData(
Math.round(object1.width),
Math.round(object1.height),
true,
0
);
var bitmapData2:BitmapData = new BitmapData(
Math.round(object2.width),
Math.round(object2.height),
true,
0
);
// Draw
bitmapData1.draw(object1, new Matrix(1, 0, 0, 1, -object1.x, -object1.y));
bitmapData2.draw(object2);
// Create BMP's
var bitmap1:Bitmap = new Bitmap(bitmapData1);
var bitmap2:Bitmap = new Bitmap(bitmapData2);
// Set X and Y and BMP
bitmap1.x = object1Point.x;
bitmap1.y = object1Point.y;
bitmap2.x = object2Point.x;
bitmap2.y = object2Point.y;
// BMP origin is the object1 X and Y
var bitmapOrigin:Point = new Point(object1Point.x, object1Point.y);
// Create a local version of the bitmap2 so we can see what is overlapping
var bitmap2OriginLocal:Point = bitmap2.globalToLocal(bitmapOrigin);
// Create a rectangle from what we now know
var rect:Rectangle = new Rectangle(bitmap2OriginLocal.x, bitmap2OriginLocal.y, object1.width, object1.height);
// The overlapping pixels are within the rectangle, so get them all
var overlappingPixels:Vector.<uint> = bitmap2.bitmapData.getVector(rect);
// Run through all the overlapping pixels until we find a colourful one
for (var i:uint = 0; i < overlappingPixels.length; i++ ) {
var index:uint = overlappingPixels[i];
// If the colour is not 0, we have found it
if(index != 0){
point = new Point();
// Basically, instead of using width and getting 100, we're working out how
// many pixles across the overlap is. The Vector doesn't tell us this, so we need to work it out
var overlappingWidth:uint = object1.width - Math.abs(bitmap2OriginLocal.x);
// The Y is object1.y, minus the local y, plus object1's width minus the X from the local
point.y = object1.y - bitmap2OriginLocal.y + uint(i / overlappingWidth);
// The X is the same as above, but % of the width
point.x = object1.x - bitmap2OriginLocal.x + (i % overlappingWidth);
// Found it, we're done
break;
}
}
// Only fires when you've got a collision that is less than 1 pixel from the width or height
// Just a fail safe
if (!point) {
point = new Point(object1.width, object1.height);
}
return point;
}
}
}

User interaction with Leapmotion AS3 library

I can connect the device and attach a custom cursor to one finger, but I can´t use any of the gestures to over/click a button or drag a sprite around, etc.
I´m using Starling in the project. To run this sample just create a Main.as, setup it with Starling and call this class.
My basic code:
package
{
import com.leapmotion.leap.Controller;
import com.leapmotion.leap.events.LeapEvent;
import com.leapmotion.leap.Finger;
import com.leapmotion.leap.Frame;
import com.leapmotion.leap.Gesture;
import com.leapmotion.leap.Hand;
import com.leapmotion.leap.InteractionBox;
import com.leapmotion.leap.Pointable;
import com.leapmotion.leap.ScreenTapGesture;
import com.leapmotion.leap.Vector3;
import starling.display.Shape;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.TouchEvent;
/**
* ...
* #author miau
*/
public class LeapController extends Sprite
{
private var _controller:Controller;
private var _cursor:Shape;
private var _screenTap:ScreenTapGesture;
private var _displayWidth:uint = 800;
private var _displayHeight:uint = 600;
public function LeapController()
{
addEventListener(Event.ADDED_TO_STAGE, _startController);
}
private function _startController(e:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, _startController);
//adding controller
_controller = new Controller();
_controller.addEventListener( LeapEvent.LEAPMOTION_INIT, onInit );
_controller.addEventListener( LeapEvent.LEAPMOTION_CONNECTED, onConnect );
_controller.addEventListener( LeapEvent.LEAPMOTION_DISCONNECTED, onDisconnect );
_controller.addEventListener( LeapEvent.LEAPMOTION_EXIT, onExit );
_controller.addEventListener( LeapEvent.LEAPMOTION_FRAME, onFrame );
//add test button
_testButton.x = stage.stageWidth / 2 - _testButton.width / 2;
_testButton.y = stage.stageHeight / 2 - _testButton.height / 2;
addChild(_testButton);
_testButton.touchable = true;
_testButton.addEventListener(TouchEvent.TOUCH, doSomething);
//draw ellipse as a cursor
_cursor = new Shape();
_cursor.graphics.lineStyle(6, 0xFFE24F);
_cursor.graphics.drawEllipse(0, 0, 80, 80);
addChild(_cursor);
}
private function onFrame(e:LeapEvent):void
{
trace("ON FRAME STARTED");
var frame:Frame = e.frame;
var interactionBox:InteractionBox = frame.interactionBox;
// Get the first hand
if(frame.hands.length > 0){
var hand:Hand = frame.hands[0];
var numpointables:int = e.frame.pointables.length;
var pointablesArray:Array = new Array();
if(frame.pointables.length > 0 && frame.pointables.length < 2){
//trace("number of pointables: "+frame.pointables[0]);
for(var j:int = 0; j < frame.pointables.length; j++){
//var pointer:DisplayObject = pointablesArray[j];
if(j < numpointables){
var pointable:Pointable = frame.pointables[j];
var normal:Vector3 = pointable.tipPosition;
var normalized:Vector3 = interactionBox.normalizePoint(normal);
//pointable.isFinger = true;
_cursor.x = normalized.x * _displayWidth;
_cursor.y = _displayHeight - (normalized.y * _displayHeight);
_cursor.visible = true;
}else if (j == 0) {
_cursor.visible = false;
}
}
}
}
}
private function onExit(e:LeapEvent):void
{
trace("ON EXIT STARTED");
}
private function onDisconnect(e:LeapEvent):void
{
trace("ON DISCONNECT STARTED");
}
private function onConnect(e:LeapEvent):void
{
trace("ON CONNECT STARTED");
_controller.enableGesture( Gesture.TYPE_SWIPE );
_controller.enableGesture( Gesture.TYPE_CIRCLE );
_controller.enableGesture( Gesture.TYPE_SCREEN_TAP );
_controller.enableGesture( Gesture.TYPE_KEY_TAP );
}
private function onInit(e:LeapEvent):void
{
trace("ON INIT STARTED");
}
private function doSomething(e:TouchEvent):void
{
trace("I WAS TOUCHED!!!");
}
}
}
If a good code Samaritan can update this code to perform a screen tap gesture (or any interacion with any object), I will really appreciate this a lot.
Regards!
controller.enableGesture(Gesture.TYPE_SWIPE);
controller.enableGesture(Gesture.TYPE_SCREEN_TAP);
if(controller.config().setFloat("Gesture.Swipe.MinLength", 200.0) && controller.config().setFloat("Gesture.Swipe.MinVelocity", 500)) controller.config().save();
if(controller.config().setFloat("Gesture.ScreenTap.MinForwardVelocity", 30.0) && controller.config().setFloat("Gesture.ScreenTap.HistorySeconds", .5) && controller.config().setFloat("Gesture.ScreenTap.MinDistance", 1.0)) controller.config().save();
//etc...
Then catch it in the frame event listener:
private function onFrame( event:LeapEvent ):void
{
var frame:Frame = event.frame;
var gestures:Vector.<Gesture> = frame.gestures();
for ( var i:int = 0; i < gestures.length; i++ )
{
var gesture:Gesture = gestures[ i ];
switch ( gesture.type )
{
case Gesture.TYPE_SCREEN_TAP:
var screentap:ScreenTapGesture = ScreenTapGesture ( gesture);
trace ("ScreenTapGesture-> x: " + Math.round(screentap.position.x ) + ", y: "+ Math.round( screentap.position.y));
break;
case Gesture.TYPE_SWIPE:
var screenSwipe:SwipeGesture = SwipeGesture(gesture);
if(gesture.state == Gesture.STATE_START) {
//
}
else if(gesture.state == Gesture.STATE_STOP) {
//
trace("SwipeGesture-> direction: "+screenSwipe.direction + ", duration: " + screenSwipe.duration);
}
break;
default:
trace( "Unknown gesture type." )
}
}
}
When the event occurs, check the coordinates translated to the stage/screen and whether a hit test returns true.
EDIT: Considering I have no idea how to reliable get the touch point x/y (or better: how to translate them to the correct screen coordinates), I would probably do something like this in my onFrame event:
private function onFrame(event:LeapEvent):void {
var frame:Frame = event.frame;
var gestures:Vector.<Gesture> = frame.gestures();
var posX:Number;
var posY:Number;
var s:Shape;
if(frame.pointables.length > 0) {
var currentVector:Vector3 = screen.intersectPointable(frame.pointables[0], true); //get normalized vector
posX = 1920 * currentVector.x - stage.x; //NOTE: I hardcoded the screen res value, you can get it like var w:int = leap.locatedScreens()[0].widthPixels();
posY = 1080 * ( 1 - currentVector.y ) - stage.y; //NOTE: I hardcoded the screen res value, you can get it like var h:int = leap.locatedScreens()[0].heightPixels();
}
for(var i:int = 0; i < gestures.length; i++) {
var gesture:Gesture = gestures[i];
if(gesture.type == Gesture.TYPE_SCREEN_TAP) {
if(posX >= _button1.x &&
posX <= _button1.x + _button1.width &&
posY >= _button1.y &&
posY <= _button1.y + _button1.height) {
s = new Shape();
s.graphics.beginFill(0x00FF00);
s.graphics.drawCircle(0, 0, 10);
s.graphics.endFill();
s.x = posX;
s.y = posY;
stage.addChild(s);
trace("Lisa tocada!");
}
else {
s = new Shape();
s.graphics.beginFill(0xFF0000);
s.graphics.drawCircle(0, 0, 10);
s.graphics.endFill();
s.x = posX;
s.y = posY;
stage.addChild(s);
trace("Fallaste! Intentalo otra vez, tiempo: "+new Date().getTime());
}
}
}
}

AS2 code to AS3 code help converting!

Help!!! This as2 code has given me a mind numbing headache. Can someone help me convert it to AS3. Here is the code:
import flash.filters.GlowFilter;
//Settings
var maxDist:Number = 400;
var accuracy:Number = 1;
//other vars (do not edit)
var dx:Number;
var dy:Number;
var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;
var realDist:Number;
var rad:Number = Math.PI/180;
var rad2:Number = 180/Math.PI;
map_mc.createEmptyMovieClip("laser_mc", map_mc.getNextHighestDepth());
//Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = 0xFF0000;
glow.alpha = 1;
glow.blurX = 7;
glow.blurY = 7;
glow.quality = 2;
map_mc.laser_mc.filters = new Array(glow);
/**
*
* Mouse Controls
*
*/
//create an object that we'll listen to
mouseListener = new Object();
//on Click, fire the weapon
mouseListener.onMouseDown = function() {
fireWeapon();
}
//on release, stop weapon firing
mouseListener.onMouseUp = function() {
stopWeapon();
}
//on mouse move, rotate the player
mouseListener.onMouseMove = function() {
rotatePlayer();
}
//add listener
Mouse.addListener(mouseListener);
/**
*
* Laser Weapon
*
*/
//fire weapon function - creates an onEnterFrame function to repeat updateLaser() over and over
function fireWeapon():Void
{
_root.onEnterFrame = function ():Void {
updateLaser();
}
}
//stop weapon function - deletes onEnterFrame and clears laser movieclip
function stopWeapon():Void
{
delete _root.onEnterFrame;
map_mc.laser_mc/**/.clear();
}
//Update Laser Function
function updateLaser() :Void
{
//run a loop
for (realDist=0; realDist<maxDist; realDist += accuracy)
{
//get end X&Y
endX = map_mc.player_mc._x + Math.cos(map_mc.player_mc._rotation * rad) * realDist;
endY = map_mc.player_mc._y + Math.sin(map_mc.player_mc._rotation * rad) * realDist;
//calculate hit test
if (map_mc.walls_mc.hitTest(endX, endY, true)) {
break;
}
}
//calculate the tip of the barrel (start X & Y)
startX = map_mc.player_mc._x + 15 * Math.cos(map_mc.player_mc._rotation * rad);
startY = map_mc.player_mc._y + 15 * Math.sin(map_mc.player_mc._rotation * rad);
// Clear it before we draw, so the line doesnt stay there
map_mc.laser_mc.clear();
//draw laser
map_mc.laser_mc.lineStyle(2, 0xFF0000, 75);
map_mc.laser_mc.moveTo(startX, startY);
map_mc.laser_mc.lineTo(endX, endY);
}
//rorate player towards mouse function
function rotatePlayer():Void
{
//get distance between mouse and player
dx = map_mc._xmouse-map_mc.player_mc._x;
dy = map_mc._ymouse-map_mc.player_mc._y;
//calculate rotation of player
map_mc.player_mc._rotation = Math.atan2(dy, dx)*rad2;
}
THX
Here is the bulk of it
You shouldn't get compile errors now
Plus I cleaned it up
You will also have to fix your rotation point of player_mc
import flash.filters.GlowFilter;
//Settings
var maxDist:Number = 400;
var accuracy:Number = 1;
//other vars (do not edit)
var dx:Number;
var dy:Number;
var startX:Number;
var startY:Number;
var endX:Number;
var endY:Number;
var realDist:Number;
var rad:Number = Math.PI/180;
var rad2:Number = 180/Math.PI;
var laser_mc:MovieClip = new MovieClip()
map_mc.addChild(laser_mc)
//Glow Filter
var glow:GlowFilter = new GlowFilter();
glow.color = 0xFF0000;
glow.alpha = 1;
glow.blurX = 7;
glow.blurY = 7;
glow.quality = 2;
laser_mc.filters = new Array(glow);
/**
*
* Mouse Controls
*
*/
stage.addEventListener( MouseEvent.MOUSE_DOWN, fire);
//on Click, fire the weapon
function fire( e:Event ):void {
stage.addEventListener( Event.ENTER_FRAME , updateLaser)
}
stage.addEventListener( MouseEvent.MOUSE_UP, stopFire);
//on release, stop weapon firing
function stopFire( e:Event ):void {
laser_mc.graphics.clear();
stage.removeEventListener( Event.ENTER_FRAME , updateLaser)
}
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved)
//on mouse move, rotate the player
function mouseMoved( e:Event ):void {
//get distance between mouse and player
dx = map_mc.mouseX-map_mc.player_mc.x;
dy = map_mc.mouseY-map_mc.player_mc.y;
//calculate rotation of player
map_mc.player_mc.rotation = Math.atan2(dy, dx)*rad2;
}
//Update Laser Function
function updateLaser( e:Event ):void {
//run a loop
for (realDist=0; realDist<maxDist; realDist += accuracy){
//get end X&Y
endX = map_mc.player_mc.x + Math.cos(map_mc.player_mc.rotation * rad) * realDist;
endY = map_mc.player_mc.y + Math.sin(map_mc.player_mc.rotation * rad) * realDist;
//calculate hit test
if (map_mc.walls_mc.hitTestPoint(endX,endY)) {
break;
}
}
//calculate the tip of the barrel (start X & Y)
startX = map_mc.player_mc.x + 15 * Math.cos(map_mc.player_mc.rotation * rad);
startY = map_mc.player_mc.y + 15 * Math.sin(map_mc.player_mc.rotation * rad);
//draw laser
laser_mc.graphics.clear();
laser_mc.graphics.lineStyle(2, 0xFF0000);
laser_mc.graphics.moveTo(startX, startY);
laser_mc.graphics.lineTo(endX, endY);
}