toy.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. function LeapToy::createBackground( %this )
  23. {
  24. // Create the sprite.
  25. %background = new Sprite();
  26. // Set the sprite as "static" so it is not affected by gravity.
  27. %background.setBodyType( static );
  28. // Set the position.
  29. %background.Position = "0 0";
  30. // Set the size.
  31. %background.Size = "40 30";
  32. // Set to the furthest background layer.
  33. %background.SceneLayer = 31;
  34. // Set an image.
  35. %background.Image = "LeapToy:menuBackground";
  36. %background.createEdgeCollisionShape( -20, -15, -20, 15 );
  37. %background.createEdgeCollisionShape( 20, -15, 20, 15 );
  38. %background.createEdgeCollisionShape( -20, 15, 20, 15 );
  39. // Add the sprite to the scene.
  40. SandboxScene.add( %background );
  41. %this.createCircles();
  42. }
  43. //-----------------------------------------------------------------------------
  44. function LeapToy::createCircles( %this )
  45. {
  46. // Create the sprite.
  47. %circleOne = new Sprite();
  48. %circleTwo = new Sprite();
  49. // Set the sprite as "static" so it is not affected by gravity.
  50. %circleOne.BodyType = "Kinematic";
  51. %circleTwo.BodyType = "Kinematic";
  52. // Set the position.
  53. %circleOne.Position = "15 10";
  54. %circleTwo.Position = "15 10";
  55. // Set the size.
  56. %circleOne.Size = "15 15";
  57. %circleTwo.Size = "15 15";
  58. // Set to the furthest background layer.
  59. %circleOne.SceneLayer = 30;
  60. %circleTwo.SceneLayer = 30;
  61. // Set an image.
  62. %circleOne.Image = "LeapToy:complexCircle";
  63. %circleTwo.Image = "LeapToy:simpleCircle";
  64. %circleOne.AngularVelocity = 15;
  65. %circleTwo.AngularVelocity = -15;
  66. // Add the sprite to the scene.
  67. SandboxScene.add( %circleOne );
  68. SandboxScene.add( %circleTwo );
  69. }
  70. //-----------------------------------------------------------------------------
  71. function LeapToy::createGround( %this )
  72. {
  73. // Create the ground
  74. %ground = new Scroller();
  75. %ground.setBodyType("static");
  76. %ground.Image = "LeapToy:window";
  77. %ground.setPosition(0, -12);
  78. %ground.SceneLayer = 11;
  79. %ground.setSize(LeapToy.GroundWidth, 6);
  80. %ground.setRepeatX(LeapToy.GroundWidth / 40);
  81. %ground.createEdgeCollisionShape(LeapToy.GroundWidth/-2, 3, LeapToy.GroundWidth/2, 3);
  82. SandboxScene.add(%ground);
  83. }
  84. //-----------------------------------------------------------------------------
  85. // This will be called when the user presses the spacebar
  86. //
  87. // %val - Will be true if the event is down, false if it was released
  88. function LeapToy::pickSprite( %this, %val )
  89. {
  90. // Find out where the cursor is located, then convert to world coordinates
  91. %cursorPosition = Canvas.getCursorPos();
  92. %worldPosition = SandboxWindow.getWorldPoint(%cursorPosition);
  93. // If true, force an onTouchDown for the Sandbox input listener
  94. // If false, force an onTouchUp for the Sandbox input listener
  95. if (%val)
  96. {
  97. Sandbox.InputController.onTouchDown(0, %worldPosition);
  98. }
  99. else
  100. {
  101. Sandbox.InputController.onTouchUp(0, %worldPosition);
  102. }
  103. }