Lightning Effect Class [Code Fixed & Preview Video Added]


(Bora Kasap) #1

you can add it to the world with this line:

Lightning(FP.world.create(Lightning)).init(source_entity, target_entity, [0xFFFFFF, 0x00FF00, n+++]);
    package
    {
    	import net.flashpunk.Entity;
    	import net.flashpunk.utils.Draw;
    	import net.flashpunk.FP;
    	import flash.geom.Point;
    	
    	public class Lightning extends Entity
    	{
    		private var source:Entity;
    		private var target:Entity;
    		private var points:Array = new Array;
    		private var colors:Array;
    		private var vib:Number;
    		private var quantity:int;
    		private var size:Number;
    		
    		public function Lightning()
    		{
    		}
    		
    		public function init(source:Entity, target:Entity, colors:Array, vibration:Number = 6, quantity:int = 2, size:Number = 10):Lightning
    		{
    			this.source = source;
    			this.target = target;
    			this.colors = colors;
    			this.vib = vibration;
    			this.quantity = quantity;
    			this.size = size;
    			return this;
    		}
    		
    		override public function render():void
    		{
    			if (this.source.world != null && this.target.world != null)
    			{
    				var distance:Number = FP.distance(this.source.x, this.source.y, this.target.x, this.target.y);
    				var angle:Number = FP.angle(this.source.x, this.source.y, this.target.x, this.target.y) * FP.RAD;
    				var pointcount:int = int(distance / this.size);
    				this.points.length = 0;
    				
    				var p:int;
    				
    				this.points.push(new Point(this.source.x, this.source.y));
    				for (p = 1; p < pointcount; p++)
    				{
    					this.points.push(new Point(this.source.x + Math.cos(angle) * p*this.size, this.source.y + Math.sin(angle) * p*this.size));
    				}
    				this.points.push(new Point(this.target.x, this.target.y));
    				
    				for (var q:int = 0; q < this.quantity; q++)
    				{
    					for (p = 1; p < pointcount; p++)
    					{
    						this.points[p].x += this.vib * -0.5 + Math.random() * this.vib;
    						this.points[p].y += this.vib * -0.5 + Math.random() * this.vib;
    					}
    					
    					var color:uint = this.colors[FP.rand(this.colors.length)];
    					for (p = 1; p < pointcount + 1; p++)
    					{
    						Draw.line(this.points[p-1].x, this.points[p-1].y, this.points[p].x, this.points[p].y, color);
    					}
    				}
    				
    			} else this.world.recycle(this);
    			
    		}
    	}
    }

Shield effect Class for FP
[SOLVED: Problem wasn't about collideLine] What is the problem? CollideLine
(Ultima2876) #2

Sweet! Do you have an swf example so we can see how it looks in action? :slight_smile: