Actionscript 3 Making the character to Jump -
i making platformer game. having issue because whenever pressed spacebar jump, character stuck in mid-air. however, can resolved problem holding spacebar , character land.
the issue @ mainjump()
located within boy
class.
i seen many people solved problem using action timeline, main problem is, there anyway can solve problem using external class?
main class
package { import flash.display.*; import flash.text.*; import flash.events.*; import flash.utils.timer; import flash.text.*; public class experimentingmain extends movieclip { var count:number = 0; var mytimer:timer = new timer(10,count); var classboy:boy; //var activategravity:gravity = new gravity(); var leftkey, rightkey, spacekey, stopanimation:boolean; public function experimentingmain() { mytimer.addeventlistener(timerevent.timer, scoreup); mytimer.start(); classboy = new boy(); addchild(classboy); stage.addeventlistener(keyboardevent.key_down, pressthedamnkey); stage.addeventlistener(keyboardevent.key_up, liftthedamnkey); } public function pressthedamnkey(event:keyboardevent):void { if (event.keycode == 37) { leftkey = true; stopanimation = false; } if (event.keycode == 39) { rightkey = true; stopanimation = false; } if (event.keycode == 32) { spacekey = true; stopanimation = true; } } public function liftthedamnkey(event:keyboardevent):void { if (event.keycode == 37) { leftkey = false; stopanimation = true; } if (event.keycode == 39) { rightkey = false; stopanimation = true; } if (event.keycode == 32) { spacekey = false; stopanimation = true; } } public function scoreup(event:timerevent):void { scoresystem.text = string("score : "+mytimer.currentcount); } } }
boy class
package { import flash.display.*; import flash.events.*; public class boy extends movieclip { var leftkeydown:boolean = false; var upkeydown:boolean = false; var rightkeydown:boolean = false; var downkeydown:boolean = false; //the main character's speed var mainspeed:number = 5; //whether or not main guy jumping var mainjumping:boolean = false; //how should jump start off var jumpspeedlimit:int = 40; //the current speed of jump; var jumpspeed:number = 0; var thecharacter:movieclip; var currentx,currenty:int; public function boy() { this.x = 600; this.y = 540; addeventlistener(event.enter_frame, boymove); } public function boymove(event:event):void { currentx = this.x; currenty = this.y; if (movieclip(parent).leftkey) { currentx += mainspeed; movieclip(this).scalex = 1; } if (movieclip(parent).rightkey) { currentx -= mainspeed; movieclip(this).scalex = -1; } if (movieclip(parent).spacekey) { mainjump(); } this.x = currentx; this.y = currenty; } public function mainjump():void { currenty = this.y; if (! mainjumping) { mainjumping = true; jumpspeed = jumpspeedlimit * -1; currenty += jumpspeed; } else { if (jumpspeed < 0) { jumpspeed *= 1 - jumpspeedlimit / 250; if (jumpspeed > -jumpspeedlimit/12) { jumpspeed *= -2; } } } if (jumpspeed > 0 && jumpspeed <= jumpspeedlimit) { jumpspeed *= 1 + jumpspeedlimit / 120; } currenty += jumpspeed; if (currenty >= stage.stageheight - movieclip(this).height) { mainjumping = false; currenty = stage.stageheight - movieclip(this).height; } } } }
you've got mainjumping
variable true while jump running. why not utilize that?
if (movieclip(parent).spacekey || mainjumping) { mainjump(); }
actionscript-3
No comments:
Post a Comment