PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Alien.hx

http://github.com/domrein/FlxInvaders-Haxe
Haxe | 97 lines | 66 code | 14 blank | 17 comment | 5 complexity | 152b8103acd526c23809019f6d3e6204 MD5 | raw file
  1. import org.flixel.FlxSprite;
  2. import org.flixel.FlxU;
  3. import org.flixel.FlxG;
  4. import flash.display.BitmapData;
  5. import flash.display.Bitmap;
  6. import flash.display.Sprite;
  7. import flash.events.Event;
  8. import flash.net.URLRequest;
  9. import flash.display.Loader;
  10. #if flash9
  11. class AlienImgData extends BitmapData {public function new(){super(0,0);}}
  12. class AlienImg extends Bitmap{public function new(){super(new AlienImgData());}}
  13. #elseif iphone
  14. class AlienImg extends Bitmap{public function new(){super(BitmapData.load("tempAlienSmall.png"));}}
  15. #elseif cpp
  16. class AlienImg extends Bitmap{public function new(){super(BitmapData.load("/Users/pmilham/Dropbox/Projects/XCode/Flixel/haxe/res/tempAlienSmall.png"));}}
  17. #end
  18. class Alien extends FlxSprite {
  19. var bullets:Array<Dynamic>; //Reference to the bullets the enemies shoot at you
  20. static var bulletIndex:Int; //Tracker or marker for the bullet list
  21. var shotClock:Float; //A simple timer for deciding when to shoot
  22. var originalX:Int; //Saves the starting horizontal position (for movement logic)
  23. ///*[Embed(source="alien.png")]*/ var ImgAlien:Class<Dynamic>; //The graphic of the squid monster
  24. //This is the constructor for the squid monster.
  25. //We are going to set up the basic values and then create a simple animation.
  26. public function new(X:Int, Y:Int, Color:Int, Bullets:Array<Dynamic>) {
  27. super(X,Y); //Initialize sprite object
  28. //forAlien = true;
  29. loadGraphic(AlienImg,true); //Load this animated graphic file
  30. //Saving off some of the values we passed in
  31. originalX = X;
  32. color = Color; //setting the color tints the plain white alien graphic
  33. bullets = Bullets;
  34. bulletIndex = 0;
  35. restart(); //Resets the timer for the bullet shooting logic
  36. //Time to create a simple animation! alien.png has 3 frames of animation in it.
  37. //We want to play them in the order 1, 2, 3, 1 (but of course this stuff is 0-index).
  38. //To avoid a weird, annoying appearance the framerate is randomized a little bit
  39. // to a value between 6 and 10 (6+4) frames per second.
  40. addAnimation("Default",[0,1,0,2],Math.floor(6+FlxU.random()*4));
  41. //Now that the animation is set up, it's very easy to play it back!
  42. play("Default");
  43. //Everybody move to the right!
  44. velocity.x = 10;
  45. }
  46. //Basic game loop is BACK y'all
  47. public override function update():Void
  48. {
  49. //If alien has moved too far to the left, reverse direction and increase speed!
  50. if(x < originalX - 8)
  51. {
  52. x = originalX - 8;
  53. velocity.x = 10;
  54. velocity.y++;
  55. }
  56. if(x > originalX + 8) //If alien has moved too far to the right, reverse direction
  57. {
  58. x = originalX + 8;
  59. velocity.x = -10;
  60. }
  61. //Then do some bullet shooting logic
  62. if(y > FlxG.height * 0.35)
  63. shotClock -= FlxG.elapsed; //Only count down if on the bottom half of the screen
  64. if(shotClock <= 0)
  65. {
  66. //We counted down to zero, so it's time to shoot a bullet!
  67. restart(); //First, reset the shot clock
  68. var b:FlxSprite = bullets[bulletIndex]; //Then look up the bullet
  69. b.reset(x + width / 2 - b.width, y);
  70. b.velocity.y = 65;
  71. bulletIndex++;
  72. if(bulletIndex >= bullets.length)
  73. bulletIndex = 0;
  74. }
  75. //Finally, the all important basic game loop update
  76. super.update();
  77. }
  78. //This function just resets our bullet logic timer to a random value between 1 and 11
  79. function restart():Void
  80. {
  81. shotClock = 1+FlxU.random()*10;
  82. }
  83. }