forked from: RayCasting, help with bitmapdata.draw
This code works on flash, but not on wonderfl.
If you look at the function "loaded," you'll see the line:
map.draw(loader);
This line draws the contents of the loaded background
(loader, a Loader which loads the background image)
to a bitmapdata, "map," which is used in the main engine
however, this line seems not to work, and any lines written
after it are ignored. I was forced to move the addListenerEvent
for "main" before this line because flash apparently stops here
Any help? Thanks!
// forked from AceDecade's RayCasting, help with bitmapdata.draw
//This code works on flash, but not on wonderfl.
//If you look at the function "loaded," you'll see the line:
//map.draw(loader);
//This line draws the contents of the loaded background
//(loader, a Loader which loads the background image)
//to a bitmapdata, "map," which is used in the main engine
//however, this line seems not to work, and any lines written
//after it are ignored. I was forced to move the addListenerEvent
//for "main" before this line because flash apparently stops here
//Any help? Thanks!
package{
import flash.system.LoaderContext;
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.Event;
public class RayCast extends Sprite{
public var range:Number = 20;
public var steps:Number = 20;
public var rate:Number = range/steps;
public var angle:Number = 2*Math.PI;
public var shots:Number = 50;
public var arate:Number = angle/shots;
public var url:URLRequest = new URLRequest("http://i47.tinypic.com/15ogcnk.jpg");
public var loader:Loader = new Loader();
public var Points:Vector.<Vector.<Number>> = new Vector.<Vector.<Number>>();
public var map:BitmapData;
public var light:Sprite = new Sprite();
public function RayCast(){
map = new BitmapData(465,465,true,0xffffffff);
loader.load(url);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaded);
}
public function main(e:Event):void{
Points= new Vector.<Vector.<Number>>();
for(var i:Number=0;i<=shots;i+=1){
var a:Number = i*arate;
var p:Vector.<Number>;
var A:Vector.<Number> = new Vector.<Number>();
var X:Number = mouseX;
var Y:Number = mouseY;
var DX:Number = Math.cos(a)*rate;
var DY:Number = Math.sin(a)*rate;
for(var j:Number=rate;j<=range;j+=rate){
X += DX;
Y += DY;
p = Channels(X,Y,0);
if(p[0] == 0){
j = range;
}
}
A.push(X);
A.push(Y);
Points.push(A);
}
with(light.graphics){
clear();
lineStyle(1,0xff0000,1);
beginFill(0x00ff00);
moveTo(mouseX,mouseY);
}
for(i=0;i<Points.length;i++){
light.graphics.lineTo(Points[i][0],Points[i][1]);
}
light.graphics.endFill();
}
public function loaded(e:Event):void{
addChild(loader);
addChild(light);
stage.addEventListener(Event.ENTER_FRAME,main);
var byteLoader:Loader = new Loader;
byteLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, function ():void {
map.draw(byteLoader.content);
});
byteLoader.loadBytes(loader.contentLoaderInfo.bytes);
//map.draw(loader.content);//lines beyond this one will not execute
//map = Bitmap(LoaderInfo(e.target).content).bitmapData;//this doesn't work either
}
public function Channels(X:int,Y:int,t:Number=-1):Vector.<Number>{
var A:Vector.<Number> = new Vector.<Number>();
var source:uint = map.getPixel32(X,Y);
for(var i:int=0;i<3;i++){
if(i == t || t == -1){
var j:int = 8*(2-i);
var u:uint = (source >> j) & 0xff;
if(u == 255){
A.push(2);
}else if(u == 0){
A.push(0);
}else{
A.push(1);
}
}
}
return A;
}
}
}