Hi, it’s me again.
So, defying my ignorance, I tried to make a ray tracing experiment. And of course, it doesn’t work.
I think it’s a pretty basic thing with the drawing api, but I have tried many different options, and none work.
Basically, I want my raytracer to be instantiated when the mouse button is pressed, and when released, it should gently disappear. Even though the first part work, the second doesn’t, and I am not sure why (I have tried all .graphics.clear() possibilities, I think, but I am fairly new to AS3’s magic). I have commented so it helps (and there is stuff I will use later on for collision detection).
Anyway, here’s the code (sorry about the mess, it’s pretty much a what’s in my mind prototype, building on old scraps of code)- any hint as to why it doesn’t work would be awesome! Thanks a bunch!
package
{
import flash.display.*;
import flash.events.MouseEvent;
import flash.events.Event;
public class Testing extends Sprite
{
//variables related to the movement of the cone
public var oldX:Number;
public var oldY:Number;
public var vx:Number;
public var vy:Number;
public var dx:Number;
public var dy:Number;
public var speed:Number;
public var angle:Number;
//variables to create the raytracer
public var cone:Sprite; //the sprite that should contain the raytracer
public var lth:Number = 100; //length of the rays
public var dpth:Number = 100; //depth of the rays - will use for collision testing
public var coneAngle:Number = 60; //angle of the tracer
public var coneAngleStep:Number = 20; //times I check for collision
public var rayAngle:Number; //in use later
public var rayAngleRad:Number; //in use later
public function Testing()
{
init();
}
private function init():void
{
addEventListener(Event.ENTER_FRAME, enterF);
}
private function enterF(event:Event):void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, accion);
}
private function accion(event:MouseEvent):void
{
oldX = mouseX;
oldY = mouseY;
stage.addEventListener(MouseEvent.MOUSE_UP, reaccion);
addEventListener(Event.ENTER_FRAME, annoy);
}
private function reaccion(event:MouseEvent):void
{
removeChild(cone); //once the button is released, the cone should disappear
removeEventListener(Event.ENTER_FRAME, annoy);
}
private function annoy(event:Event):void
{
var vx:Number = oldX - mouseX;
var vy:Number = oldY - mouseY;
var speed = Math.sqrt(dx * dx + dy * dy);//will be used later, with collision detection
var dx:Number = mouseX - oldX;
var dy:Number = mouseY - oldY;
var angle:Number = Math.atan2(dy, dx); //calculate the angle between the mouse and where it moves
var cone:Sprite = new Sprite(); //creating the cone
cone.graphics.lineStyle(1, 0xff0000); //givin' it some style
for (var x:uint = 0; x <= coneAngle; x += coneAngle/coneAngleStep) //basic raytracer
{
cone.graphics.moveTo(oldX, oldY);
var rayAngle:Number = angle/(Math.PI/180) - 90*(coneAngle/2) + x;
var rayAngleRad:Number = rayAngle * Math.PI / 180;
trace(rayAngleRad);
cone.graphics.lineTo(oldX - (lth) * Math.cos(rayAngleRad), oldY - (lth) * Math.sin(rayAngleRad));
}
addChild(cone);
}
}
}