A more efficient method of creating a laser


(Adam Edney) #1

This is my code for creating a laser.

		    var sp:Sprite = new Sprite();
			sp.graphics.lineStyle(3, 0xffffff,1);
			sp.graphics.moveTo(x1, y1); //Draws line from (0,0)
			sp.graphics.lineTo(x2, y2); //To (100,100)
	
			var glow:GlowFilter = new GlowFilter();
			sp.filters = [glow];

			bmd = new BitmapData(Math.abs(x1 + x2), Math.abs(y1 + y2), true, 1); //Probably the bit
			bmd.draw(sp); //you're interested in
			
			img = new Image(bmd);
			
			graphic = img;

But the Bitmap Data is using a lot of memory(2mb per time and it would be firing once or twice a second), what would be a good method of doing the same thing.


(Mike Evmm) #2

I’m pretty sure there’s a post here saying how ineffective this is. Nonetheless, this might help.
Ultima’s method is also very efficient. I’ll see if I can find the post.

Aha, found it!


(Adam Edney) #3

yh i tried this post (below)

“I’d use an Image instead. Make a small slice of your ‘laser’; say, 4x8 pixels, then scale it as appropriate (lineLength / 8) and rotate it. This’ll require some maths but will be the most efficient way to draw lines as it’s just drawing sprites.”

But it wasn’t really working and your method was the only one which was. His method was difficult to a line so it looked like it was missing all the time .


(Mike Evmm) #4

How does this one work for you?


(Adam Edney) #5

Thanks for the reply :slight_smile: Ok i made a different laser with using images which uses less memory (below) without the filter

public function LASER(x1:Number , y1:Number,x2:Number , y2:Number,bmd:BitmapData,_angle:Number,world_map:World_Map)
	{
		img = new Image(ASSET_IMG.TEST_LASER);
		img.scaleY = world_map.distance_between_two_points(x1,y1,x2,y2)/8;
		if (x2 < x1) {var abs_x:Number = Math.abs(x1 - x2); img.x = x2 +
                (abs_x ) } else { img.x = x1 }
		if (y2 < y1) { var abs_y:Number = Math.abs(y1 - y2); img.y = y2 + 
                (abs_y ) } else { img.y = y1 }
		img.angle = _angle;
	
		graphic = img;
	} 

I don’t see how the link you suggested will work with the code above so i can apply the filters, or am I missing something ? )


(Mike Evmm) #6

If you use a premade image, you can’t (at least to my knowledge) apply a filter dynamically. You can do it, however, to the image you’re using (so, in this case, to ASSETS_IMG.TEST_LASER) using image edition software, like GIMP!


(azrafe7) #7

Out of curiosity I played a bit with this laser thing and got a semi-passable result ([swf] (https://dl.dropboxusercontent.com/u/32864004/dev/FPDemo/FPLaser.swf) - click to fire).

The code is a bit messy and the class is not exactly usable as is, but it can hopefully give you some ideas. Laser.as (3.2 KB)


(Adam Edney) #8

Sorry for the late reply i used it almost right away but forgot to tell you lot.

Thanks for the file, I made it more usable, so i can quickly make a laser. and I change a few of the effects so they more suitable for my art style (if you can call my sprites art :smile: ).

Thanks to everyone that replied.


(Zouhair Elamrani Abou Elassad) #9

Can provide the new class you’re using, that can be helpful as well :slight_smile: