Advertisement
nutter666

Laser Beam

Feb 27th, 2013
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import flash.display.Sprite;
  2. import flash.geom.Point;
  3. import flash.events.MouseEvent;
  4. import flash.events.Event;
  5.  
  6. var lasersArray:Array = [];
  7.  
  8. var fadeSpeed:Number = 0.1;
  9. var laserColour:uint = 0xFF0000;
  10. var laserThickness:Number = 2;
  11.  
  12. var startPoint = new Point(275,400)
  13. // the point where the laser will start
  14.  
  15. stage.addEventListener(MouseEvent.CLICK,mClick)
  16. stage.addEventListener(Event.ENTER_FRAME,eFrame)
  17.  
  18. function mClick(m:MouseEvent){
  19. // creates a point with the mouse position
  20. var clickPoint:Point = new Point(stage.mouseX,stage.mouseY)
  21. drawBeam(clickPoint)
  22. }
  23.  
  24. function drawBeam(cPoint:Point){
  25. // the main guts of the laser drawing, creates a new sprite to hold the beam
  26. // draws a line from the start point to the point you pass into it (where you've clicked)
  27. var s:Sprite = new Sprite();
  28. addChild(s)
  29. lasersArray.push(s)
  30.  
  31. s.graphics.lineStyle(laserThickness,laserColour)
  32. s.graphics.moveTo(startPoint.x,startPoint.y)
  33. s.graphics.lineTo(cPoint.x,cPoint.y)
  34. }
  35.  
  36. // this bit just fades out and removes old lasers.. can stick it in any enter frame event
  37. // doesn't need its own special one or anything
  38. function eFrame(e:Event){
  39. for(var i:int = 0;i<=lasersArray.length-1;i++){
  40. lasersArray[i].alpha -= fadeSpeed
  41.  
  42. if(lasersArray[i].alpha <= 0){
  43. removeChild(lasersArray[i])
  44. lasersArray.splice(i,1)
  45. }
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement