main.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 ScrollerToy::create( %this )
  23. {
  24. // Reset the toy.
  25. ScrollerToy.reset();
  26. }
  27. //-----------------------------------------------------------------------------
  28. function ScrollerToy::destroy( %this )
  29. {
  30. }
  31. //-----------------------------------------------------------------------------
  32. function ScrollerToy::reset( %this )
  33. {
  34. // Clear the scene.
  35. SandboxScene.clear();
  36. // Create some scrollers.
  37. %this.createBackground();
  38. %this.createFarScroller();
  39. %this.createNearScroller();
  40. }
  41. //-----------------------------------------------------------------------------
  42. function ScrollerToy::createBackground( %this )
  43. {
  44. // Create the sprite.
  45. %object = new Sprite();
  46. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  47. // Set the position.
  48. %object.Position = "0 0";
  49. // Set the size.
  50. %object.Size = "100 75";
  51. // Set to the furthest background layer.
  52. %object.SceneLayer = 31;
  53. // Set an image.
  54. %object.Image = "ToyAssets:jungleSky";
  55. // Add the sprite to the scene.
  56. SandboxScene.add( %object );
  57. }
  58. //-----------------------------------------------------------------------------
  59. function ScrollerToy::createFarScroller( %this )
  60. {
  61. // Create the scroller.
  62. %object = new Scroller();
  63. // Note this scroller for the touch controls.
  64. ScrollerToy.FarScroller = %object;
  65. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  66. // Set the position.
  67. %object.Position = "0 -10";
  68. // Set the size.
  69. %object.Size = "100 75";
  70. // Set to the furthest background layer.
  71. %object.SceneLayer = 31;
  72. // Set the scroller to use a static image.
  73. %object.Image = "ToyAssets:TreeBackground2";
  74. // We don't really need to do this as the frame is set to zero by default.
  75. %object.Frame = 0;
  76. // Set the scroller moving in the X axis.
  77. %object.ScrollX = 25;
  78. // Set the scroller to only show half of the static image in the X axis.
  79. %object.RepeatX = 0.5;
  80. // Add the sprite to the scene.
  81. SandboxScene.add( %object );
  82. }
  83. //-----------------------------------------------------------------------------
  84. function ScrollerToy::createNearScroller( %this )
  85. {
  86. // Create the scroller.
  87. %object = new Scroller();
  88. // Note this scroller for the touch controls.
  89. ScrollerToy.NearScroller = %object;
  90. // Always try to configure a scene-object prior to adding it to a scene for best performance.
  91. // Set the position.
  92. %object.Position = "0 -10";
  93. // Set the size.
  94. %object.Size = "100 75";
  95. // Set to the furthest background layer.
  96. %object.SceneLayer = 31;
  97. // Set the scroller to use a static image.
  98. %object.Image = "ToyAssets:TreeBackground1";
  99. // We don't really need to do this as the frame is set to zero by default.
  100. %object.Frame = 0;
  101. // Set the scroller moving in the X axis.
  102. %object.ScrollX = 40;
  103. // Set the scroller to only show half of the static image in the X axis.
  104. %object.RepeatX = 0.5;
  105. // Add the sprite to the scene.
  106. SandboxScene.add( %object );
  107. }
  108. //-----------------------------------------------------------------------------
  109. function ScrollerToy::onTouchDown(%this, %touchID, %worldPosition)
  110. {
  111. // Set the scrollers speed to be the distance from the farground scrollers origin.
  112. // Also use the sign to control the direction of scrolling.
  113. %scrollerSpeed = %worldPosition.x - ScrollerToy.FarScroller.Position.x;
  114. // Set the scroller speeds.
  115. ScrollerToy.FarScroller.ScrollX = %scrollerSpeed;
  116. ScrollerToy.NearScroller.ScrollX = %scrollerSpeed * 1.5;
  117. }