main.cs 5.2 KB

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