Shield effect Class for FP


(Marko Cetinic) #1

Hi Guys,

I had years of experience with business applications (Flex and JS) but never created a game. FlashPunk seems so fluid and easy to use, that is a real pleasure creating something with it. I was inspired by Lighting Effect Class I found here, and decided to share a small class of my own. It is a simple shield effect. Hope somebody finds it useful…

package
{
	import net.flashpunk.Entity;
	import net.flashpunk.FP;
	import net.flashpunk.utils.Draw;

	public class Shield extends Entity
	{
		private var _target:Entity;
		private var _color:uint;
		
		private var _frames:int;
		private var _vib:Number;
		private var _step:Number;
		
		private var _xEllipse:Number;
		private var _yEllipse:Number;
		
		private var _xOffset:Number;
		private var _yOffset:Number;
		
		private var _pX1:Number;
		private var _pY1:Number;
		
		private var _pX2:Number;
		private var _pY2:Number;
		
		private var _resetFrames:int;
		
		public function Shield(target:Entity, color:uint = 0xFFFFFF, frames:int = 29, xOffset:int=0, yOffset:int=0, xEllipse:Number = 1, yEclipse:Number = 1, vib:int = 2, step:int = 30)
		{
			this._target = target;
			this._color = color;
			
			this._frames = frames;
			this._resetFrames = _frames;
			
			this._xOffset = xOffset;
			this._yOffset = yOffset;
			
			this._xEllipse = xEllipse;
			this._yEllipse = yEclipse;
			
			this._vib = vib;
			this._step = step;
		}
		
		public function get color():uint 
		{
			return _color;
		}
		
		public function set color(value:uint):void 
		{
			_color = value;
		}
		
		public function get frames():int 
		{
			return _frames;
		}
		
		public function set frames(value:int):void 
		{
			_frames = value;
		}
		
		public function get vib():Number 
		{
			return _vib;
		}
		
		public function set vib(value:Number):void 
		{
			_vib = value;
		}
		
		public function get step():Number 
		{
			return _step;
		}
		
		public function set step(value:Number):void 
		{
			_step = value;
		}
		
		public function get xEllipse():Number 
		{
			return _xEllipse;
		}
		
		public function set xEllipse(value:Number):void 
		{
			_xEllipse = value;
		}
		
		public function get yEllipse():Number 
		{
			return _yEllipse;
		}
		
		public function set yEllipse(value:Number):void 
		{
			_yEllipse = value;
		}
		
		public function get xOffset():Number 
		{
			return _xOffset;
		}
		
		public function set xOffset(value:Number):void 
		{
			_xOffset = value;
		}
		
		public function get yOffset():Number 
		{
			return _yOffset;
		}
		
		public function set yOffset(value:Number):void 
		{
			_yOffset = value;
		}
		
		protected function returnVibOffset():Number
		{
			return this._vib * -0.5 + Math.random() * this._vib * 2;
		}
		
		override public function render():void
		{
			if (this._target.world == null)
				return;
			
			var h:Number = this._target.x + _target.halfWidth + this._xOffset;
			var k:Number = this._target.y + _target.halfHeight + this._yOffset;
			
			var r:Number = this._target.halfHeight + 10;
			
			var step:Number = 2 * Math.PI / this._step;
						
			for (var theta:Number = 0; theta <= 360; theta += step)
			{
				if (_frames >= 0)
					this._frames--;
				else
				{
					this._frames = this._resetFrames;
					
					_pX1 = h + this._xEllipse * r * Math.cos(theta) + returnVibOffset();
					_pY1 = k - this._yEllipse * r * Math.sin(theta) + returnVibOffset();
					
					_pX2 = h + this._xEllipse * r * Math.cos(theta + step) + returnVibOffset();
					_pY2 = k - this._yEllipse * r * Math.sin(theta + step) + returnVibOffset();
				}
				
				Draw.line(_pX1, _pY1, _pX2, _pY2, _color);
			}
			
			super.render();
		}
	}
}

You can see example here.

Thanks


(Ultima2876) #2

Hm, doesn’t seem to load for me :frowning: I’m using Chrome (latest version I think) on Windows 8.1. It’s always awesome to see new faces around here, especially the ones who give back to the community!


(Bora Kasap) #3

I’m using chrome too, also win7starter, it’s taking time to load but it’s playing with no problems.


(Ultima2876) #4

Ah yeah, it does indeed work. Wonder why it takes so long to load?


(Bora Kasap) #5

Maybe he’s compiled with full game source


(Marko Cetinic) #6

The problem was actually that I left bunch of assets like sounds and images embedded, even though none of them were used in this example. The additional data has increased the .swf size thus the longer load time. I cleaned the code and now swf is around 80kb :smiley: