Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.42 KB | None | 0 0
  1. package {
  2.  
  3. //import flash.display.DisplayObject;
  4. import flash.display.*;
  5. import flash.events.*;
  6. import flash.geom.Rectangle;
  7. import flash.media.SoundChannel;
  8. import flash.media.Sound;
  9.  
  10. public class char extends MovieClip {
  11.  
  12. var abletojump:Boolean;
  13. var speed:Number = 7;
  14. var jumpspeed:Number = 0;
  15. var jumpheight:Number = 25;
  16. var fall:Number = 1;
  17. var maxgravity:Number = 15;
  18. var platforms:Number = 100;
  19. var jump:Boolean = true;
  20. var charx:Number;
  21. var chary:Number;
  22. var charw:Number;
  23. var charh:Number;
  24. var i:Number;
  25. var plat:DisplayObject;
  26. var door:DisplayObject;
  27. var ladder:DisplayObject;
  28. var ladderTop:DisplayObject;
  29. var hazard:DisplayObject;
  30. var chest:DisplayObject;
  31. var direction:String = "right";
  32. var startMc:startBtn = new startBtn();
  33. var climbing:Boolean = false;
  34. var alive:Boolean = true;
  35. var sfxChannel:SoundChannel = new SoundChannel();
  36. var jumpSound:Sound = new jumpStart();
  37. var landSound:Sound = new land();
  38. var clickSound:Sound = new click();
  39. var cashSound:Sound = new cash();
  40. var completeSound:Sound = new complete();
  41. var failSound:Sound = new failure();
  42. var chestCollected:Boolean = false;
  43.  
  44. public function char():void {
  45. this.addEventListener(Event.ADDED_TO_STAGE, initiate);
  46. }
  47.  
  48. function initiate(e:Event):void{
  49. stage.addEventListener(TouchEvent.TOUCH_BEGIN, levelStart);
  50. (root as MovieClip).addChild(startMc);
  51. startMc.x = 540;
  52. startMc.y = 960;
  53. this.gotoAndStop(1);
  54. }
  55.  
  56. function levelStart(e:TouchEvent):void{
  57. sfxChannel = clickSound.play();
  58. stage.removeEventListener(TouchEvent.TOUCH_BEGIN, levelStart);
  59. (root as MovieClip).removeChild(startMc);
  60. addEventListener(Event.ENTER_FRAME, theenter);
  61. stage.addEventListener(TouchEvent.TOUCH_BEGIN, jumpHandler);
  62. }
  63.  
  64. function theenter(evt:Event):void {
  65. charx = this.x;
  66. chary = this.y;
  67. charw = this.width;
  68. charh = this.height;
  69. this.x += speed;
  70. this.y += fall;
  71. if (direction == "right" && climbing == false) {
  72. if(abletojump == true){
  73. this.gotoAndStop("runRight");
  74. }
  75. speed = 7;
  76. }
  77. else if (direction == "left" && climbing == false) {
  78. if(abletojump == true){
  79. this.gotoAndStop("runLeft");
  80. }
  81. speed = -7;
  82. }
  83. if (climbing == true){
  84. fall = -5;
  85. abletojump = false;
  86. speed = 0;
  87. }
  88. if (abletojump == false && climbing == false && alive == true){
  89. this.gotoAndStop("jump" + direction)
  90. }
  91. if (fall>0) {
  92. abletojump = false;
  93. }
  94. if(climbing == false){
  95. fall++;
  96. }
  97. if (fall >= maxgravity) {
  98. fall = maxgravity;
  99. }
  100. if (jump == true) {
  101. if (jumpspeed > 0) {
  102. jumpspeed--;
  103. fall = 0;
  104. this.y -= jumpspeed;
  105. }
  106. }
  107. else {
  108. jumpspeed = jumpheight;
  109. }
  110. for (i = 0; i <= (root as MovieClip).levelContainer.numChildren -1; i++) {
  111.  
  112. if((root as MovieClip).levelContainer.getChildAt(i).name == "finalDoor"){
  113. door = (root as MovieClip).levelContainer.getChildAt(i);
  114. if(this.hitTestObject(door) && abletojump == true && door.x + 5 > charx && door.x - 5 < charx && alive == true){
  115. levelComplete();
  116. }
  117. }
  118.  
  119. if((root as MovieClip).levelContainer.getChildAt(i).name == "chest"){
  120. chest = (root as MovieClip).levelContainer.getChildAt(i);
  121. if(this.hitTestObject(chest) && abletojump == true && chest.x + chest.width/2 > charx && chest.x - chest.width/2 < charx && alive == true && chestCollected == false){
  122. (chest as MovieClip).play();
  123. chestCollected = true;
  124. sfxChannel = cashSound.play();
  125. }
  126. }
  127.  
  128.  
  129. if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "p"){
  130. plat = (root as MovieClip).levelContainer.getChildAt(i);
  131. }
  132. if (plat is MovieClip) {
  133. if (this.hitTestObject(plat) && climbing == false) {
  134. //if char bounces left
  135. if(charx + charw/2 < plat.x && chary + charh/2 > plat.y - plat.height/2 + 10 && chary - charh/2 < plat.y + plat.height/2 - 10){
  136. direction = "left";
  137. }
  138. //if char bounces right
  139. if(charx - charw/2 > plat.x && chary + charh/2 > plat.y - plat.height/2 + 10 && chary - charh/2 < plat.y + plat.height/2 - 10){
  140. direction = "right";
  141. }
  142. //land on top
  143. if(chary + charh/2 < plat.y - plat.height/2 + 10 && charx > plat.x - plat.width/2 && charx < plat.x + plat.width/2){
  144. if(alive == false){
  145. death();
  146. }
  147. if(alive == true && fall > 14){
  148. sfxChannel = landSound.play();
  149. }
  150. this.y = plat.y - plat.height / 2 - charh / 2;
  151. jumpspeed = jumpheight;
  152. fall = 0;
  153. jump = false;
  154. abletojump = true;
  155. }
  156. //hit from underneath
  157. if(chary - charh/2 > plat.y + plat.height/2 - 10 && charx > plat.x - plat.width/2 && charx < plat.x + plat.width/2 ){
  158. this.y = plat.y + plat.height / 2 + charh / 2;
  159. jumpspeed *= -1;
  160. jump = false;
  161. }
  162. }
  163. }
  164.  
  165. if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "T"){
  166. ladderTop = (root as MovieClip).levelContainer.getChildAt(i);
  167. if(this.hitTestObject(ladderTop) && climbing == true && chary > ladderTop.y){
  168. climbing = false;
  169. }
  170. }
  171.  
  172. if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "b"){
  173. hazard = (root as MovieClip).levelContainer.getChildAt(i);
  174. if(alive == true && this.hitTestObject(hazard) && charx < hazard.x + hazard.width/2 && charx > hazard.x - hazard.width/2 && chary < hazard.y + hazard.height/2 && chary > hazard.y - hazard.height/2){
  175. alive = false;
  176. sfxChannel = failSound.play();
  177. if(direction == "right"){
  178. this.gotoAndStop("dieLeft");
  179. }
  180. if(direction == "left"){
  181. this.gotoAndStop("dieRight");
  182. }
  183. }
  184. }
  185.  
  186. else if((root as MovieClip).levelContainer.getChildAt(i).name.substr(0,1) == "l"){
  187. ladder = (root as MovieClip).levelContainer.getChildAt(i);
  188. if (ladder is MovieClip) {
  189. if (this.hitTestObject(ladder) && charx < ladder.x + 5 && charx > ladder.x - 5 && chary + charh/2 > ladder.y - ladder.height/2 + 5 && alive == true) {
  190. climbing = true;
  191. this.x = ladder.x;
  192. this.gotoAndStop("climb");
  193. }
  194. }
  195. }
  196. }
  197. }
  198.  
  199. function jumpHandler(e:TouchEvent):void {
  200. if (abletojump == true) {
  201. sfxChannel = jumpSound.play();
  202. jump = true;
  203. abletojump = false;
  204. }
  205. }
  206.  
  207. function death():void{
  208. removeEventListener(Event.ENTER_FRAME, theenter);
  209. stage.removeEventListener(TouchEvent.TOUCH_BEGIN, jumpHandler);
  210. (root as MovieClip).targetFrame = "deathScreen";
  211. (root as MovieClip).fadeMaker();
  212. }
  213.  
  214. function levelComplete():void{
  215. removeEventListener(Event.ENTER_FRAME, theenter);
  216. stage.removeEventListener(TouchEvent.TOUCH_BEGIN, jumpHandler);
  217. sfxChannel = completeSound.play();
  218. this.gotoAndStop("stand" + direction);
  219. if(chestCollected == true){
  220. (root as MovieClip).mySharedObject.data.cash += 10;
  221. (root as MovieClip).mySharedObject.data.chestsOpened[(root as MovieClip).currentLevel -1] = 1;
  222. (root as MovieClip).mySharedObject.flush();
  223. }
  224. if((root as MovieClip).currentLevel == (root as MovieClip).mySharedObject.data.highestLevel){
  225. (root as MovieClip).mySharedObject.data.highestLevel += 1;
  226. (root as MovieClip).mySharedObject.flush();
  227. }
  228. (root as MovieClip).targetFrame = "Complete";
  229. (root as MovieClip).fadeMaker();
  230. }
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement