ScoreManager.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "atomic component";
  2. exports.component = function(self) {
  3. self.start = function() {
  4. self.scores = 0;
  5. self.subscribeToEvent(Atomic.PhysicsBeginContact2DEvent(function(data){
  6. //check if a ball collide with brick
  7. if ((data.nodeA.name == "Ball" && data.nodeB.name.indexOf("Brick")>-1) || (data.nodeB.name == "Ball" && data.nodeA.name.indexOf("Brick")>-1)) {
  8. //add scores
  9. self.scores += 10;
  10. self.updateText();
  11. }
  12. }));
  13. var view = new Atomic.UIView();
  14. // Create a layout, otherwise child widgets won't know how to size themselves
  15. // and would manually need to be sized
  16. var layout = new Atomic.UILayout();
  17. // specify the layout region
  18. layout.rect = view.rect;
  19. view.addChild(layout);
  20. // we're laying out on the X axis so "position" controls top and bottom alignment
  21. layout.layoutPosition = Atomic.UI_LAYOUT_POSITION.UI_LAYOUT_POSITION_LEFT_TOP;
  22. // while "distribution" handles the Y axis
  23. layout.layoutDistributionPosition = Atomic.UI_LAYOUT_DISTRIBUTION_POSITION.UI_LAYOUT_DISTRIBUTION_POSITION_LEFT_TOP;
  24. var fd = new Atomic.UIFontDescription();
  25. fd.id = "Vera";
  26. fd.size = 18;
  27. self.scoreText = new Atomic.UIEditField();
  28. self.scoreText.fontDescription = fd;
  29. self.scoreText.readOnly = true;
  30. self.scoreText.multiline = true;
  31. self.scoreText.adaptToContentSize = true;
  32. self.scoreText.text = "Scores: ";
  33. layout.addChild(self.scoreText);
  34. self.updateText();
  35. };
  36. self.updateText = function() {
  37. self.scoreText.text = "Score: " + self.scores;
  38. };
  39. };