Background.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334
  1. "atomic component";
  2. //random function returns a random float number from min to max
  3. function random(min, max) {
  4. return (Math.random() * (max - min + 1)) + min;
  5. }
  6. //A Background component
  7. exports.component = function(self) {
  8. self.start = function() {
  9. //looping main game song
  10. var music = Atomic.cache.getResource("Sound", "Sounds/crazy-space.ogg");
  11. music.looped = true;
  12. //get default zone
  13. var zone = Atomic.renderer.getDefaultZone();
  14. //fog color with ortho camera is a clear clear
  15. //so we set a clear color
  16. zone.setFogColor([0.282, 0.361, 0.557]);
  17. //add stars on a background
  18. for (var i = 0; i < 100; i++) {
  19. //create a star node
  20. var starNode = self.node.createChild("Star");
  21. //set its position to a random number
  22. starNode.position2D = [random(-Atomic.graphics.width/2, Atomic.graphics.width/2)*Atomic.PIXEL_SIZE, random(-Atomic.graphics.height/2, Atomic.graphics.height/2)*Atomic.PIXEL_SIZE];
  23. //add static sprite component to display a 2d sprite
  24. var star = starNode.createComponent("StaticSprite2D");
  25. //set layer of a star to -100
  26. //it means that we move star node to 'background' layer
  27. star.layer = -100;
  28. //load a sprite2D
  29. star.sprite = Atomic.cache.getResource("Sprite2D", "Sprites/star.png");
  30. }
  31. };
  32. };