0 Replies - 2749 Views - Last Post: 04 March 2013 - 09:28 AM

#1 PianistChris  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 28-February 13

2D Dynamic Lighting Multiple Layers

Posted 04 March 2013 - 09:28 AM

Both my friend and I have been working on a simple 2D side-scroller. We want to add lighting effects that basically hides everything on screen except at a certain radius around the player, but we have various layers (boundaries for collision, a background, and wallboundaries).

Though the code works for one of the layers (See:
 _ground.mask=_light; 
), we can't find a way to add it to all the layers without breaking. (We want it to interact with BOTH the boundaries and background).

package
{
	import flash.display.MovieClip
	import flash.events.Event
	import flash.events.KeyboardEvent

	import flash.display.Sprite;
	
	public class DocumentMain extends MovieClip
	{
		public var _startMarker:StartMarker;
		public var _player:Player;
		public var _ground:Ground;
		public var _boundaries:Boundaries;
		public var _wallboundaries:WallBoundaries;
		
		public var _light:Sprite=new Sprite;
		
		public var key_pressed:int=0;
		
		public var radius:int=8;
		public var torch_power:int=100;
		public var torch_step:int=100;
		public var torch_angle:int=60;
		public var torch_angle_step:int=20;
		public var flicker=0;

		public var _vx:Number;
		public var _vy:Number;
		
		public function DocumentMain():void
		{
			addChild(_ground);
			addChild(_boundaries);
			addChild(_light);
			addChild(_player);
			
		 	_ground.mask=_light;
		
			//Assign default values
			_startMarker.visible = false;
			_vx = 0;
			_vy = 0;
					
			//Set focus for keyboard input
			stage.focus = stage;
			
			//Add event listeners
			this.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
		}
		
		public function enterFrameHandler(e:Event):void
		{
			var dist_x:Number=_player.x;
			var dist_y:Number=_player.y;
			var angle:Number=- Math.atan2(dist_x, dist_y);
		
			_light.graphics.clear();
			
			if(Math.random()*100>0)
			{
				_light.graphics.beginFill(0xffffff, 100);
				_light.graphics.moveTo(_player.x, _player.y);
				for(var i:int=0; i<=torch_angle; i+=(torch_angle/torch_angle_step))
				{
					var ray_angle=to_radians((to_degrees(angle)-90-(torch_angle/2)+i));
					for(var j:int=1; j<=torch_step; j++)
					{
						if(_boundaries.hitTestPoint(_player.x+(torch_power/torch_step*j)*Math.cos(ray_angle), _player.y+(torch_power/torch_step*j)*Math.sin(ray_angle), true))
						{
							break;
						}
					}
					_light.graphics.lineTo(_player.x+(torch_power/torch_step*j)*Math.cos(ray_angle), _player.y+(torch_power/torch_step*j)*Math.sin(ray_angle));
                }
				_light.graphics.lineTo(_player.x, _player.y);
				_light.graphics.endFill();
			}
			
			
			
			//Process collisions
			processCollisions();
			
			//Gravitate the player
			_vy += 2;
			
			//Move the player
			_player.x += _vx;
			_player.y += _vy;
		
			//Scroll the stage
			scrollStage();
		}
		
		public function keyDownHandler(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case 37: //Left arrow
				_vx = -7;
				break;
				
				case 38: //Up arrow
				_vy = -20;
				break;
				
				case 39: //Right arrow
				_vx = 7;
				break;
				
				default:
			}
		}
		
		public function keyUpHandler(e:KeyboardEvent):void
		{
			switch(e.keyCode)
			{
				case 37:
				case 39:
				_vx = 0;
				break;
				
				default:
			}
		}
		
		public function processCollisions():void
		{
			//When player is falling
			if(_vy > 0)
			{
				//Respawn if player is out of bounds
				if(_player.y > stage.stageHeight)
				{
					_player.x = _startMarker.x;
					_player.y = _startMarker.y;
					_boundaries.x = 0;
					_boundaries.y = 0;
					_wallboundaries.x = 0;
					_wallboundaries.y = 0;
					_vy = 0;
				}
				//Otherwise, process collisions with boundaries
				else
				{
					var collision:Boolean = false;
					var wallcollision:Boolean = false;
					
					if(_boundaries.hitTestPoint(_player.x, _player.y, true))
					{
						collision = true;
					}
					
					if(collision)
					{
						while(collision)
						{
							_player.y -= 0.1;
							collision = false;
							
							if(_boundaries.hitTestPoint(_player.x, _player.y, true))
							{
								collision = true;
							}
						}
						
						_vy = 0;
					}
					
					if(_wallboundaries.hitTestPoint(_player.x, _player.y, true))
					{
						wallcollision = true;
					}
					
					if(wallcollision)
					{
						while(wallcollision)
						{
							_player.x -= 0.01;
							wallcollision = false;
							
							if(_wallboundaries.hitTestPoint(_player.x, _player.y, true))
							{
								wallcollision = true;
							}
						}
						
						_vx = 0;
					}
				}
				
				
			}
		}
		
		public function to_radians(n:Number):Number
		{
			return(n*0.174532925);
		}
		
		public function to_degrees(n:Number):Number
		{
			return(n*57.2957795);
		}
		
		public function scrollStage():void
		{
			_boundaries.x += (stage.stageWidth * 0.5) - _player.x;
			_wallboundaries.x += (stage.stageWidth * 0.5) - _player.x;
			_player.x = stage.stageWidth * 0.5;
		}
		
	}
}



Any help is appreciated!

Is This A Good Question/Topic? 0
  • +

Page 1 of 1