Advertisement
Guest User

Untitled

a guest
May 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 2.36 KB | None | 0 0
  1. package;
  2.  
  3. import flixel.FlxG;
  4. import flixel.FlxSprite;
  5. import flixel.math.FlxVelocity;
  6. import flixel.math.FlxAngle;
  7. import flixel.FlxObject;
  8.  
  9. class Ball extends FlxSprite
  10. {
  11.     /**
  12.      *  The name of the current FlxSprite
  13.      *  Use it to differentiate between anonymous masses of Sprites like in FlxGroups!
  14.      */
  15.     public var name:String;
  16.  
  17.     public var speed = 0;
  18.  
  19.     public function new(?X:Int = 0, ?Y:Int = 0)
  20.     {
  21.         super(X, Y);
  22.  
  23.         loadGraphic(AssetPaths.ball__png, false, 15, 15);
  24.         screenCenter();
  25.         elasticity = 1.0;
  26.         set_alpha(Reg.spriteAlpha);
  27.         name = "ball";
  28.     }
  29.  
  30.     override public function update(elapsed:Float):Void
  31.     {
  32.         updateFacing();
  33.  
  34.         super.update(elapsed);
  35.     }
  36.  
  37.     function updateFacing()
  38.     {
  39.         if (velocity.y < 0 && Math.abs(velocity.y) > Math.abs(velocity.x))
  40.             facing = FlxObject.UP;
  41.         if (velocity.y > 0 && velocity.y > Math.abs(velocity.x))
  42.             facing = FlxObject.DOWN;
  43.         if (velocity.x < 0 && Math.abs(velocity.x) > Math.abs(velocity.y))
  44.             facing = FlxObject.LEFT;
  45.         if (velocity.x > 0 && velocity.x > Math.abs(velocity.y))
  46.             facing = FlxObject.RIGHT;
  47.        
  48.         FlxG.watch.addQuick("Normal X", velocity.x);
  49.         FlxG.watch.addQuick("Normal Y", velocity.y);
  50.         FlxG.watch.addQuick("Math.abs() X", Math.abs(velocity.x));
  51.         FlxG.watch.addQuick("Math.abs() Y", Math.abs(velocity.y));
  52.         FlxG.watch.addQuick("Facing", facing);
  53.     }
  54.  
  55.     public function resetBall()
  56.     {
  57.         screenCenter();
  58.  
  59.         if (Reg.lives == 3)
  60.             speed = FlxG.random.int(200, 300);
  61.  
  62.         var randomAngle = FlxG.random.float(-180, 180);
  63.         var randomDirection = FlxVelocity.velocityFromAngle(randomAngle, speed);
  64.         velocity.set(randomDirection.x, randomDirection.y);
  65.     }
  66.  
  67.     public function increaseSpeed()
  68.     {
  69.         speed += 15;
  70.         Reg.speed += 15;
  71.         var curAngle = FlxAngle.asDegrees(Math.atan2(velocity.y, velocity.x));
  72.         var newSpeed = FlxVelocity.velocityFromAngle(curAngle, speed);
  73.           //FlxVelocity.velocityFromFacing(this, speed);
  74.         velocity.set(newSpeed.x, newSpeed.y);
  75.     }
  76.  
  77.     public function updateBallPos(collideObj:FlxNamedSprite)
  78.     {
  79.         /*
  80.         switch (collideObj.name)
  81.         {
  82.             case "paddle":
  83.         }
  84.         */
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement