UI.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var game = Atomic.game;
  2. var view = game.uiView;
  3. var UI = Atomic.UI;
  4. var UIButton = Atomic.UIButton;
  5. var UITextField = Atomic.UITextField;
  6. var UILayout = Atomic.UILayout;
  7. // create the start ui programmatically, we could also
  8. // use a ui template
  9. var window = new Atomic.UIWindow();
  10. // disable close button
  11. window.settings = UI.WINDOW_SETTINGS_DEFAULT & ~UI.WINDOW_SETTINGS_CLOSE_BUTTON;
  12. window.text = "Physics Platformer";
  13. // root content layout
  14. var layout = new UILayout();
  15. layout.axis = UI.AXIS_Y;
  16. // give ourselves a little more spacing
  17. layout.spacing = 18;
  18. layout.layoutSize = UI.LAYOUT_SIZE_AVAILABLE;
  19. window.contentRoot.addChild(layout);
  20. var text = new UITextField();
  21. text.text = "Please select the time of day:";
  22. layout.addChild(text);
  23. // Buttons layout
  24. var buttonLayout = new UILayout();
  25. buttonLayout.layoutDistribution = UI.LAYOUT_DISTRIBUTION_GRAVITY;
  26. layout.addChild(buttonLayout);
  27. var button = new UIButton();
  28. button.text = "Daytime";
  29. button.onClick = function () {
  30. runPlatformer(true);
  31. // must return handled, as we're being GC'd here (window is being removed)
  32. return true;
  33. }
  34. buttonLayout.addChild(button);
  35. button = new UIButton();
  36. button.text = "Nighttime";
  37. button.onClick = function () {
  38. runPlatformer(false);
  39. // must return handled, as we're being GC'd here (window is being removed)
  40. return true;
  41. }
  42. buttonLayout.addChild(button);
  43. window.resizeToFitContent();
  44. // add to the root view and center
  45. view.addChild(window);
  46. window.center();
  47. function runPlatformer(daytime) {
  48. view.removeChild(window)
  49. var musicFile = game.cache.getResource("Sound", "Sounds/JumpingBat.ogg");
  50. musicFile.looped = true;
  51. var musicNode = game.scene.createChild("MusicNode");
  52. var musicSource = musicNode.createComponent("SoundSource");
  53. musicSource.gain = 1.0;
  54. musicSource.soundType = Atomic.SOUND_MUSIC;
  55. musicSource.play(musicFile);
  56. var platformerNode = game.scene.createChild("Platformer");
  57. var platformer = platformerNode.createJSComponent("Platformer");
  58. platformer.init(daytime);
  59. }
  60. function start() {
  61. }
  62. function update(timeStep) {
  63. }