Advertisement
Guest User

FlxExtendedSprite

a guest
Mar 10th, 2012
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * FlxExtendedSprite
  3.  * -- Part of the Flixel Power Tools set
  4.  *
  5.  * v1.4 Added MouseSpring, plugin checks and all the missing documentation
  6.  * v1.3 Added Gravity, Friction and Tolerance support
  7.  * v1.2 Now works fully with FlxMouseControl to be completely clickable and draggable!
  8.  * v1.1 Added "setMouseDrag" and "mouse over" states
  9.  * v1.0 Updated for the Flixel 2.5 Plugin system
  10.  *
  11.  * @version 1.4 - July 29th 2011
  12.  * @link http://www.photonstorm.com
  13.  * @author Richard Davey / Photon Storm
  14. */
  15.  
  16. package org.flixel.plugin.photonstorm
  17. {
  18.     import org.flixel.*;
  19.     import org.flixel.plugin.photonstorm.BaseTypes.MouseSpring;
  20.  
  21.     /**
  22.      * An enhanced FlxSprite that is capable of receiving mouse clicks, being dragged and thrown, mouse springs, gravity and other useful things
  23.      */
  24.     public class FlxExtendedSprite extends FlxSprite
  25.     {
  26.         /**
  27.          * Used by FlxMouseControl when multiple sprites overlap and register clicks, and you need to determine which sprite has priority
  28.          */
  29.         public var priorityID:uint;
  30.        
  31.         /**
  32.          * If the mouse currently pressed down on this sprite?
  33.          * @default false
  34.          */
  35.         public var isPressed:Boolean = false;
  36.        
  37.         /**
  38.          * Is this sprite allowed to be clicked?
  39.          * @default false
  40.          */
  41.         public var clickable:Boolean = false;
  42.         private var clickOnRelease:Boolean = false;
  43.         private var clickPixelPerfect:Boolean = false;
  44.         private var clickPixelPerfectAlpha:uint;
  45.         private var clickCounter:uint;
  46.        
  47.         /**
  48.          * Function called when the mouse is pressed down on this sprite. Function is passed these parameters: obj:FlxExtendedSprite, x:int, y:int
  49.          * @default null
  50.          */
  51.         public var mousePressedCallback:Function;
  52.        
  53.         /**
  54.          * Function called when the mouse is released from this sprite. Function is passed these parameters: obj:FlxExtendedSprite, x:int, y:int
  55.          * @default null
  56.          */
  57.         public var mouseReleasedCallback:Function;
  58.        
  59.         /**
  60.          * Is this sprite allowed to be thrown?
  61.          * @default false
  62.          */
  63.         public var throwable:Boolean = false;
  64.         private var throwXFactor:int;
  65.         private var throwYFactor:int;
  66.        
  67.         /**
  68.          * Does this sprite have gravity applied to it?
  69.          * @default false
  70.          */
  71.         public var hasGravity:Boolean = false;
  72.        
  73.         /**
  74.          * The x axis gravity influence
  75.          */
  76.         public var gravityX:int;
  77.        
  78.         /**
  79.          * The y axis gravity influence
  80.          */
  81.         public var gravityY:int;
  82.        
  83.         /**
  84.          * Determines how quickly the Sprite come to rest on the walls if the sprite has x gravity enabled
  85.          * @default 500
  86.          */
  87.         public var frictionX:Number;
  88.        
  89.         /**
  90.          * Determines how quickly the Sprite come to rest on the ground if the sprite has y gravity enabled
  91.          * @default 500
  92.          */
  93.         public var frictionY:Number;
  94.        
  95.         /**
  96.          * If the velocity.x of this sprite falls between zero and this amount, then the sprite will come to a halt (have velocity.x set to zero)
  97.          */
  98.         public var toleranceX:Number;
  99.        
  100.         /**
  101.          * If the velocity.y of this sprite falls between zero and this amount, then the sprite will come to a halt (have velocity.y set to zero)
  102.          */
  103.         public var toleranceY:Number;
  104.        
  105.         /**
  106.          * Is this sprite being dragged by the mouse or not?
  107.          * @default false
  108.          */
  109.         public var isDragged:Boolean = false;
  110.        
  111.         /**
  112.          * Is this sprite allowed to be dragged by the mouse? true = yes, false = no
  113.          * @default false
  114.          */
  115.         public var draggable:Boolean = false;
  116.         private var dragPixelPerfect:Boolean = false;
  117.         private var dragPixelPerfectAlpha:uint;
  118.         private var dragOffsetX:int;
  119.         private var dragOffsetY:int;
  120.         private var dragFromPoint:Boolean;
  121.         private var allowHorizontalDrag:Boolean = true;
  122.         private var allowVerticalDrag:Boolean = true;
  123.        
  124.         /**
  125.          * Function called when the mouse starts to drag this sprite. Function is passed these parameters: obj:FlxExtendedSprite, x:int, y:int
  126.          * @default null
  127.          */
  128.         public var mouseStartDragCallback:Function;
  129.        
  130.         /**
  131.          * Function called when the mouse stops dragging this sprite. Function is passed these parameters: obj:FlxExtendedSprite, x:int, y:int
  132.          * @default null
  133.          */
  134.         public var mouseStopDragCallback:Function;
  135.        
  136.         /**
  137.          * An FlxRect region of the game world within which the sprite is restricted during mouse drag
  138.          * @default null
  139.          */
  140.         public var boundsRect:FlxRect = null;
  141.        
  142.         /**
  143.          * An FlxSprite the bounds of which this sprite is restricted during mouse drag
  144.          * @default null
  145.          */
  146.         public var boundsSprite:FlxSprite = null;
  147.        
  148.         private var snapOnDrag:Boolean = false;
  149.         private var snapOnRelease:Boolean = false;
  150.         private var snapX:int;
  151.         private var snapY:int;
  152.        
  153.         /**
  154.          * Is this sprite using a mouse spring?
  155.          * @default false
  156.          */
  157.         public var hasMouseSpring:Boolean = false;
  158.        
  159.         /**
  160.          * Will the Mouse Spring be active always (false) or only when pressed (true)
  161.          * @default true
  162.          */
  163.         public var springOnPressed:Boolean = true;
  164.        
  165.         /**
  166.          * The MouseSpring object which is used to tie this sprite to the mouse
  167.          */
  168.         public var mouseSpring:MouseSpring;
  169.        
  170.         /**
  171.          * By default the spring attaches to the top left of the sprite. To change this location provide an x offset (in pixels)
  172.          */
  173.         public var springOffsetX:int;
  174.        
  175.         /**
  176.          * By default the spring attaches to the top left of the sprite. To change this location provide a y offset (in pixels)
  177.          */
  178.         public var springOffsetY:int;
  179.        
  180.         /**
  181.          * Creates a white 8x8 square <code>FlxExtendedSprite</code> at the specified position.
  182.          * Optionally can load a simple, one-frame graphic instead.
  183.          *
  184.          * @param   X               The initial X position of the sprite.
  185.          * @param   Y               The initial Y position of the sprite.
  186.          * @param   SimpleGraphic   The graphic you want to display (OPTIONAL - for simple stuff only, do NOT use for animated images!).
  187.          */
  188.         public function FlxExtendedSprite(X:Number = 0, Y:Number = 0, SimpleGraphic:Class = null)
  189.         {
  190.             super(X, Y, SimpleGraphic);
  191.         }
  192.        
  193.         /**
  194.          * Allow this Sprite to receive mouse clicks, the total number of times this sprite is clicked is stored in this.clicks<br>
  195.          * You can add callbacks via mousePressedCallback and mouseReleasedCallback
  196.          *
  197.          * @param   onRelease           Register the click when the mouse is pressed down (false) or when it's released (true). Note that callbacks still fire regardless of this setting.
  198.          * @param   pixelPerfect        If true it will use a pixel perfect test to see if you clicked the Sprite. False uses the bounding box.
  199.          * @param   alphaThreshold      If using pixel perfect collision this specifies the alpha level from 0 to 255 above which a collision is processed (default 255)
  200.          */
  201.         public function enableMouseClicks(onRelease:Boolean, pixelPerfect:Boolean = false, alphaThreshold:uint = 255):void
  202.         {
  203.             if (FlxG.getPlugin(FlxMouseControl) == null)
  204.             {
  205.                 throw Error("FlxExtendedSprite.enableMouseClicks called but FlxMouseControl plugin not activated");
  206.             }
  207.            
  208.             clickable = true;
  209.            
  210.             clickOnRelease = onRelease;
  211.             clickPixelPerfect = pixelPerfect;
  212.             clickPixelPerfectAlpha = alphaThreshold;
  213.             clickCounter = 0;
  214.         }
  215.        
  216.         /**
  217.          * Stops this sprite from checking for mouse clicks and clears any set callbacks
  218.          */
  219.         public function disableMouseClicks():void
  220.         {
  221.             clickable = false;
  222.             mousePressedCallback = null;
  223.             mouseReleasedCallback = null;
  224.         }
  225.        
  226.         /**
  227.          * Returns the number of times this sprite has been clicked (can be reset by setting clicks to zero)
  228.          */
  229.         public function get clicks():uint
  230.         {
  231.             return clickCounter;
  232.         }
  233.        
  234.         /**
  235.          * Sets the number of clicks this item has received. Usually you'd only set it to zero.
  236.          */
  237.         public function set clicks(i:uint):void
  238.         {
  239.             clickCounter = i;
  240.         }
  241.        
  242.         /**
  243.          * Make this Sprite draggable by the mouse. You can also optionally set mouseStartDragCallback and mouseStopDragCallback
  244.          *
  245.          * @param   lockCenter          If false the Sprite will drag from where you click it. If true it will center itself to the tip of the mouse pointer.
  246.          * @param   pixelPerfect        If true it will use a pixel perfect test to see if you clicked the Sprite. False uses the bounding box.
  247.          * @param   alphaThreshold      If using pixel perfect collision this specifies the alpha level from 0 to 255 above which a collision is processed (default 255)
  248.          * @param   boundsRect          If you want to restrict the drag of this sprite to a specific FlxRect, pass the FlxRect here, otherwise it's free to drag anywhere
  249.          * @param   boundsSprite        If you want to restrict the drag of this sprite to within the bounding box of another sprite, pass it here
  250.          */
  251.         public function enableMouseDrag(lockCenter:Boolean = false, pixelPerfect:Boolean = false, alphaThreshold:uint = 255, boundsRect:FlxRect = null, boundsSprite:FlxSprite = null):void
  252.         {
  253.             if (FlxG.getPlugin(FlxMouseControl) == null)
  254.             {
  255.                 throw Error("FlxExtendedSprite.enableMouseDrag called but FlxMouseControl plugin not activated");
  256.             }
  257.            
  258.             draggable = true;
  259.            
  260.             dragFromPoint = lockCenter;
  261.             dragPixelPerfect = pixelPerfect;
  262.             dragPixelPerfectAlpha = alphaThreshold;
  263.            
  264.             if (boundsRect)
  265.             {
  266.                 this.boundsRect = boundsRect;
  267.             }
  268.            
  269.             if (boundsSprite)
  270.             {
  271.                 this.boundsSprite = boundsSprite;
  272.             }
  273.         }
  274.        
  275.         /**
  276.          * Stops this sprite from being able to be dragged. If it is currently the target of an active drag it will be stopped immediately. Also disables any set callbacks.
  277.          */
  278.         public function disableMouseDrag():void
  279.         {
  280.             if (isDragged)
  281.             {
  282.                 FlxMouseControl.dragTarget = null;
  283.                 FlxMouseControl.isDragging = false;
  284.             }
  285.            
  286.             isDragged = false;
  287.             draggable = false;
  288.            
  289.             mouseStartDragCallback = null;
  290.             mouseStopDragCallback = null;
  291.         }
  292.          
  293.         /**
  294.         * Restricts this sprite to drag movement only on the given axis. Note: If both are set to false the sprite will never move!
  295.          *
  296.          * @param   allowHorizontal     To enable the sprite to be dragged horizontally set to true, otherwise false
  297.          * @param   allowVertical       To enable the sprite to be dragged vertically set to true, otherwise false
  298.          */
  299.         public function setDragLock(allowHorizontal:Boolean = true, allowVertical:Boolean = true):void
  300.         {
  301.             allowHorizontalDrag = allowHorizontal;
  302.             allowVerticalDrag = allowVertical;
  303.         }
  304.        
  305.         /**
  306.          * Make this Sprite throwable by the mouse. The sprite is thrown only when the mouse button is released.
  307.          *
  308.          * @param   xFactor     The sprites velocity is set to FlxMouseControl.speedX * xFactor. Try a value around 50+
  309.          * @param   yFactor     The sprites velocity is set to FlxMouseControl.speedY * yFactor. Try a value around 50+
  310.          */
  311.         public function enableMouseThrow(xFactor:int, yFactor:int):void
  312.         {
  313.             if (FlxG.getPlugin(FlxMouseControl) == null)
  314.             {
  315.                 throw Error("FlxExtendedSprite.enableMouseThrow called but FlxMouseControl plugin not activated");
  316.             }
  317.            
  318.             throwable = true;
  319.             throwXFactor = xFactor;
  320.             throwYFactor = yFactor;
  321.            
  322.             if (clickable == false && draggable == false)
  323.             {
  324.                 clickable = true;
  325.             }
  326.         }
  327.        
  328.         /**
  329.          * Stops this sprite from being able to be thrown. If it currently has velocity this is NOT removed from it.
  330.          */
  331.         public function disableMouseThrow():void
  332.         {
  333.             throwable = false;
  334.         }
  335.        
  336.         /**
  337.          * Make this Sprite snap to the given grid either during drag or when it's released.
  338.          * For example 16x16 as the snapX and snapY would make the sprite snap to every 16 pixels.
  339.          *
  340.          * @param   snapX       The width of the grid cell in pixels
  341.          * @param   snapY       The height of the grid cell in pixels
  342.          * @param   onDrag      If true the sprite will snap to the grid while being dragged
  343.          * @param   onRelease   If true the sprite will snap to the grid when released
  344.          */
  345.         public function enableMouseSnap(snapX:int, snapY:int, onDrag:Boolean = true, onRelease:Boolean = false):void
  346.         {
  347.             snapOnDrag = onDrag;
  348.             snapOnRelease = onRelease;
  349.             this.snapX = snapX;
  350.             this.snapY = snapY;
  351.         }
  352.        
  353.         /**
  354.          * Stops the sprite from snapping to a grid during drag or release.
  355.          */
  356.         public function disableMouseSnap():void
  357.         {
  358.             snapOnDrag = false;
  359.             snapOnRelease = false;
  360.         }
  361.        
  362.         /**
  363.          * Adds a simple spring between the mouse and this Sprite. The spring can be activated either when the mouse is pressed (default), or enabled all the time.
  364.          * Note that nearly always the Spring will over-ride any other motion setting the sprite has (like velocity or gravity)
  365.          *
  366.          * @param   onPressed           true if the spring should only be active when the mouse is pressed down on this sprite
  367.          * @param   retainVelocity      true to retain the velocity of the spring when the mouse is released, or false to clear it
  368.          * @param   tension             The tension of the spring, smaller numbers create springs closer to the mouse pointer
  369.          * @param   friction            The friction applied to the spring as it moves
  370.          * @param   gravity             The gravity controls how far "down" the spring hangs (use a negative value for it to hang up!)
  371.          *
  372.          * @return  The MouseSpring object if you wish to perform further chaining on it. Also available via FlxExtendedSprite.mouseSpring
  373.          */
  374.         public function enableMouseSpring(onPressed:Boolean = true, retainVelocity:Boolean = false, tension:Number = 0.1, friction:Number = 0.95, gravity:Number = 0):MouseSpring
  375.         {
  376.             if (FlxG.getPlugin(FlxMouseControl) == null)
  377.             {
  378.                 throw Error("FlxExtendedSprite.enableMouseSpring called but FlxMouseControl plugin not activated");
  379.             }
  380.            
  381.             hasMouseSpring = true;
  382.             springOnPressed = onPressed;
  383.            
  384.             if (mouseSpring == null)
  385.             {
  386.                 mouseSpring = new MouseSpring(this, retainVelocity, tension, friction, gravity);
  387.             }
  388.             else
  389.             {
  390.                 mouseSpring.tension = tension;
  391.                 mouseSpring.friction = friction;
  392.                 mouseSpring.gravity = gravity;
  393.             }
  394.            
  395.             if (clickable == false && draggable == false)
  396.             {
  397.                 clickable = true;
  398.             }
  399.            
  400.             return mouseSpring;
  401.         }
  402.        
  403.         /**
  404.          * Stops the sprite to mouse spring from being active
  405.          */
  406.         public function disableMouseSpring():void
  407.         {
  408.             hasMouseSpring = false;
  409.            
  410.             mouseSpring = null;
  411.         }
  412.        
  413.         /**
  414.          * The spring x coordinate in game world space. Consists of sprite.x + springOffsetX
  415.          */
  416.         public function get springX():int
  417.         {
  418.             return x + springOffsetX;
  419.         }
  420.        
  421.         /**
  422.          * The spring y coordinate in game world space. Consists of sprite.y + springOffsetY
  423.          */
  424.         public function get springY():int
  425.         {
  426.             return y + springOffsetY;
  427.         }
  428.        
  429.         /**
  430.          * Core update loop
  431.          */
  432.         override public function update():void
  433.         {
  434.             if (draggable && isDragged)
  435.             {
  436.                 updateDrag();
  437.             }
  438.            
  439.             if (isPressed == false && FlxG.mouse.justPressed())
  440.             {
  441.                 checkForClick();
  442.             }
  443.            
  444.             if (hasGravity)
  445.             {
  446.                 updateGravity();
  447.             }
  448.            
  449.             if (hasMouseSpring)
  450.             {
  451.                 if (springOnPressed == false)
  452.                 {
  453.                     mouseSpring.update();
  454.                 }
  455.                 else
  456.                 {
  457.                     if (isPressed == true)
  458.                     {
  459.                         mouseSpring.update();
  460.                     }
  461.                     else
  462.                     {
  463.                         mouseSpring.reset();
  464.                     }
  465.                 }
  466.             }
  467.            
  468.             super.update();
  469.         }
  470.        
  471.         /**
  472.          * Called by update, applies friction if the sprite has gravity to stop jittery motion when slowing down
  473.          */
  474.         private function updateGravity():void
  475.         {
  476.             //  A sprite can have horizontal and/or vertical gravity in each direction (positiive / negative)
  477.            
  478.             //  First let's check the x movement
  479.            
  480.             if (velocity.x != 0)
  481.             {
  482.                 if (acceleration.x < 0)
  483.                 {
  484.                     //  Gravity is pulling them left
  485.                     if (touching & WALL)
  486.                     {
  487.                         drag.y = frictionY;
  488.                        
  489.                         if ((wasTouching & WALL) == false)
  490.                         {
  491.                             if (velocity.x < toleranceX)
  492.                             {
  493.                                 //trace("(left) velocity.x", velocity.x, "stopped via tolerance break", toleranceX);
  494.                                 velocity.x = 0;
  495.                             }
  496.                         }
  497.                     }
  498.                     else
  499.                     {
  500.                         drag.y = 0;
  501.                     }
  502.                 }
  503.                 else if (acceleration.x > 0)
  504.                 {
  505.                     //  Gravity is pulling them right
  506.                     if (touching & WALL)
  507.                     {
  508.                         //  Stop them sliding like on ice
  509.                         drag.y = frictionY;
  510.                        
  511.                         if ((wasTouching & WALL) == false)
  512.                         {
  513.                             if (velocity.x > -toleranceX)
  514.                             {
  515.                                 //trace("(right) velocity.x", velocity.x, "stopped via tolerance break", toleranceX);
  516.                                 velocity.x = 0;
  517.                             }
  518.                         }
  519.                     }
  520.                     else
  521.                     {
  522.                         drag.y = 0;
  523.                     }
  524.                 }
  525.             }
  526.            
  527.             //  Now check the y movement
  528.            
  529.             if (velocity.y != 0)
  530.             {
  531.                 if (acceleration.y < 0)
  532.                 {
  533.                     //  Gravity is pulling them up (velocity is negative)
  534.                     if (touching & CEILING)
  535.                     {
  536.                         drag.x = frictionX;
  537.                        
  538.                         if ((wasTouching & CEILING) == false)
  539.                         {
  540.                             if (velocity.y < toleranceY)
  541.                             {
  542.                                 //trace("(down) velocity.y", velocity.y, "stopped via tolerance break", toleranceY);
  543.                                 velocity.y = 0;
  544.                             }
  545.                         }
  546.                     }
  547.                     else
  548.                     {
  549.                         drag.x = 0;
  550.                     }
  551.                 }
  552.                 else if (acceleration.y > 0)
  553.                 {
  554.                     //  Gravity is pulling them down (velocity is positive)
  555.                     if (touching & FLOOR)
  556.                     {
  557.                         //  Stop them sliding like on ice
  558.                         drag.x = frictionX;
  559.                        
  560.                         if ((wasTouching & FLOOR) == false)
  561.                         {
  562.                             if (velocity.y > -toleranceY)
  563.                             {
  564.                                 //trace("(down) velocity.y", velocity.y, "stopped via tolerance break", toleranceY);
  565.                                 velocity.y = 0;
  566.                             }
  567.                         }
  568.                     }
  569.                     else
  570.                     {
  571.                         drag.x = 0;
  572.                     }
  573.                 }
  574.             }
  575.         }
  576.        
  577.         /**
  578.          * Updates the Mouse Drag on this Sprite.
  579.          */
  580.         private function updateDrag():void
  581.         {
  582.             //FlxG.mouse.getWorldPosition(null, tempPoint);
  583.            
  584.             if (allowHorizontalDrag)
  585.             {
  586.                 x = int(FlxG.mouse.x) - dragOffsetX;
  587.             }
  588.            
  589.             if (allowVerticalDrag)
  590.             {
  591.                 y = int(FlxG.mouse.y) - dragOffsetY;
  592.             }
  593.            
  594.             if (boundsRect)
  595.             {
  596.                 checkBoundsRect();
  597.             }
  598.  
  599.             if (boundsSprite)
  600.             {
  601.                 checkBoundsSprite();
  602.             }
  603.            
  604.             if (snapOnDrag)
  605.             {
  606.                 x = int(Math.floor(x / snapX) * snapX);
  607.                 y = int(Math.floor(y / snapY) * snapY);
  608.             }
  609.         }
  610.        
  611.         /**
  612.          * Checks if the mouse is over this sprite and pressed, then does a pixel perfect check if needed and adds it to the FlxMouseControl check stack
  613.          */
  614.         private function checkForClick():void
  615.         {
  616.             if (mouseOver && FlxG.mouse.justPressed())
  617.             {
  618.                 //  If we don't need a pixel perfect check, then don't bother running one! By this point we know the mouse is over the sprite already
  619.                 if (clickPixelPerfect == false && dragPixelPerfect == false)
  620.                 {
  621.                     FlxMouseControl.addToStack(this);
  622.                     return;
  623.                 }
  624.                
  625.                 if (clickPixelPerfect && FlxCollision.pixelPerfectPointCheck(FlxG.mouse.x, FlxG.mouse.y, this, clickPixelPerfectAlpha))
  626.                 {
  627.                     FlxMouseControl.addToStack(this);
  628.                     return;
  629.                 }
  630.                
  631.                 if (dragPixelPerfect && FlxCollision.pixelPerfectPointCheck(FlxG.mouse.x, FlxG.mouse.y, this, dragPixelPerfectAlpha))
  632.                 {
  633.                     FlxMouseControl.addToStack(this);
  634.                     return;
  635.                 }
  636.             }
  637.         }
  638.        
  639.         /**
  640.          * Called by FlxMouseControl when this sprite is clicked. Should not usually be called directly.
  641.          */
  642.         public function mousePressedHandler():void
  643.         {
  644.             isPressed = true;
  645.            
  646.             if (clickable && clickOnRelease == false)
  647.             {
  648.                 clickCounter++;
  649.             }
  650.            
  651.             if (mousePressedCallback is Function)
  652.             {
  653.                 mousePressedCallback.apply(null, [ this, mouseX, mouseY ] );
  654.             }
  655.         }
  656.        
  657.         /**
  658.          * Called by FlxMouseControl when this sprite is released from a click. Should not usually be called directly.
  659.          */
  660.         public function mouseReleasedHandler():void
  661.         {
  662.             isPressed = false;
  663.            
  664.             if (isDragged)
  665.             {
  666.                 stopDrag();
  667.             }
  668.            
  669.             if (clickable && clickOnRelease == true)
  670.             {
  671.                 clickCounter++;
  672.             }
  673.            
  674.             if (throwable)
  675.             {
  676.                 velocity.x = FlxMouseControl.speedX * throwXFactor;
  677.                 velocity.y = FlxMouseControl.speedY * throwYFactor;
  678.             }
  679.            
  680.             if (mouseReleasedCallback is Function)
  681.             {
  682.                 mouseReleasedCallback.apply(null, [ this, mouseX, mouseY ] );
  683.             }
  684.         }
  685.        
  686.         /**
  687.          * Called by FlxMouseControl when Mouse Drag starts on this Sprite. Should not usually be called directly.
  688.          */
  689.         public function startDrag():void
  690.         {
  691.             isDragged = true;
  692.            
  693.             if (dragFromPoint == false)
  694.             {
  695.                 dragOffsetX = int(FlxG.mouse.x) - x;
  696.                 dragOffsetY = int(FlxG.mouse.y) - y;
  697.             }
  698.             else
  699.             {
  700.                 //  Move the sprite to the middle of the mouse
  701.                 dragOffsetX = (frameWidth / 2);
  702.                 dragOffsetY = (frameHeight / 2);
  703.             }
  704.         }
  705.        
  706.         /**
  707.          * Bounds Rect check for the sprite drag
  708.          */
  709.         private function checkBoundsRect():void
  710.         {
  711.             if (x < boundsRect.left)
  712.             {
  713.                 x = boundsRect.x;
  714.             }
  715.             else if ((x + width) > boundsRect.right)
  716.             {
  717.                 x = boundsRect.right - width;
  718.             }
  719.            
  720.             if (y < boundsRect.top)
  721.             {
  722.                 y = boundsRect.top;
  723.             }
  724.             else if ((y + height) > boundsRect.bottom)
  725.             {
  726.                 y = boundsRect.bottom - height;
  727.             }
  728.         }
  729.        
  730.         /**
  731.          * Parent Sprite Bounds check for the sprite drag
  732.          */
  733.         private function checkBoundsSprite():void
  734.         {
  735.             if (x < boundsSprite.x)
  736.             {
  737.                 x = boundsSprite.x;
  738.             }
  739.             else if ((x + width) > (boundsSprite.x + boundsSprite.width))
  740.             {
  741.                 x = (boundsSprite.x + boundsSprite.width) - width;
  742.             }
  743.            
  744.             if (y < boundsSprite.y)
  745.             {
  746.                 y = boundsSprite.y;
  747.             }
  748.             else if ((y + height) > (boundsSprite.y + boundsSprite.height))
  749.             {
  750.                 y = (boundsSprite.y + boundsSprite.height) - height;
  751.             }
  752.         }
  753.        
  754.         /**
  755.          * Called by FlxMouseControl when Mouse Drag is stopped on this Sprite. Should not usually be called directly.
  756.          */
  757.         public function stopDrag():void
  758.         {
  759.             isDragged = false;
  760.            
  761.             if (snapOnRelease)
  762.             {
  763.                 x = int(Math.floor(x / snapX) * snapX);
  764.                 y = int(Math.floor(y / snapY) * snapY);
  765.             }
  766.         }
  767.        
  768.         /**
  769.          * Gravity can be applied to the sprite, pulling it in any direction. Gravity is given in pixels per second and is applied as acceleration.
  770.          * If you don't want gravity for a specific direction pass a value of zero. To cancel it entirely pass both values as zero.
  771.          *
  772.          * @param   gravityX    A positive value applies gravity dragging the sprite to the right. A negative value drags the sprite to the left. Zero disables horizontal gravity.
  773.          * @param   gravityY    A positive value applies gravity dragging the sprite down. A negative value drags the sprite up. Zero disables vertical gravity.
  774.          * @param   frictionX   The amount of friction applied to the sprite if it hits a wall. Allows it to come to a stop without constantly jittering.
  775.          * @param   frictionY   The amount of friction applied to the sprite if it hits the floor/roof. Allows it to come to a stop without constantly jittering.
  776.          * @param   toleranceX  If the velocity.x of the sprite falls between 0 and +- this value, it is set to stop (velocity.x = 0)
  777.          * @param   toleranceY  If the velocity.y of the sprite falls between 0 and +- this value, it is set to stop (velocity.y = 0)
  778.          */
  779.         public function setGravity(gravityX:int, gravityY:int, frictionX:Number = 500, frictionY:Number = 500, toleranceX:Number = 10, toleranceY:Number = 10):void
  780.         {
  781.             hasGravity = true;
  782.            
  783.             this.gravityX = gravityX;
  784.             this.gravityY = gravityY;
  785.            
  786.             this.frictionX = frictionX;
  787.             this.frictionY = frictionY;
  788.            
  789.             this.toleranceX = toleranceX;
  790.             this.toleranceY = toleranceY;
  791.            
  792.             if (gravityX == 0 && gravityY == 0)
  793.             {
  794.                 hasGravity = false;
  795.             }
  796.            
  797.             acceleration.x = gravityX;
  798.             acceleration.y = gravityY;
  799.         }
  800.        
  801.         /**
  802.          * Switches the gravity applied to the sprite. If gravity was +400 Y (pulling them down) this will swap it to -400 Y (pulling them up)<br>
  803.          * To reset call flipGravity again
  804.          */
  805.         public function flipGravity():void
  806.         {
  807.             if (gravityX && gravityX != 0)
  808.             {
  809.                 gravityX = -gravityX;
  810.                 acceleration.x = gravityX;
  811.             }
  812.            
  813.             if (gravityY && gravityY != 0)
  814.             {
  815.                 gravityY = -gravityY;
  816.                 acceleration.y = gravityY;
  817.             }
  818.         }
  819.        
  820.         /**
  821.          * Returns an FlxPoint consisting of this sprites world x/y coordinates
  822.          */
  823.         public function get point():FlxPoint
  824.         {
  825.             return _point;
  826.         }
  827.        
  828.         public function set point(p:FlxPoint):void
  829.         {
  830.             _point = p;
  831.         }
  832.        
  833.         /**
  834.          * Return true if the mouse is over this Sprite, otherwise false. Only takes the Sprites bounding box into consideration and does not check if there
  835.          * are other sprites potentially on-top of this one. Check the value of this.isPressed if you need to know if the mouse is currently clicked on this sprite.
  836.          */
  837.         public function get mouseOver():Boolean
  838.         {
  839.             return FlxMath.pointInCoordinates(FlxG.mouse.x, FlxG.mouse.y, x, y, width, height);
  840.         }
  841.        
  842.         /**
  843.          * Returns how many horizontal pixels the mouse pointer is inside this sprite from the top left corner. Returns -1 if outside.
  844.          */
  845.         public function get mouseX():int
  846.         {
  847.             if (mouseOver)
  848.             {
  849.                 return FlxG.mouse.x - x;
  850.             }
  851.            
  852.             return -1;
  853.         }
  854.        
  855.         /**
  856.          * Returns how many vertical pixels the mouse pointer is inside this sprite from the top left corner. Returns -1 if outside.
  857.          */
  858.         public function get mouseY():int
  859.         {
  860.             if (mouseOver)
  861.             {
  862.                 return FlxG.mouse.y - y;
  863.             }
  864.            
  865.             return -1;
  866.         }
  867.        
  868.         /**
  869.          * Returns an FlxRect consisting of the bounds of this Sprite.
  870.          */
  871.         public function get rect():FlxRect
  872.         {
  873.             _rect.x = x;
  874.             _rect.y = y;
  875.             _rect.width = width;
  876.             _rect.height = height;
  877.            
  878.             return _rect;
  879.         }
  880.        
  881.     }
  882.  
  883. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement