A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: platform game jumping help

  1. #1
    Junior Member
    Join Date
    Mar 2015
    Posts
    13

    Red face platform game jumping help

    Hey I have been coding character controls for a side scroller, just for a challenge and have run into a frustrating hitch. When my character jumps if I just tap the spacebar it doesn't play the jump animation at all and if I hold it too long, it'll go through the animation and then hover in the air as my gravity code is set to work when it is released. For this, I was thinking if there was a way to basically suspend all the code on the spacebar from the last frame of the jump animation allowing the character to fall. Both issues I am clueless and are making me rip my hair out. I would appreciate any help or thoughts you can offer very very much.

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    What code are you using so far, even better attach your current file (fla and associated files), is it AS1/AS2/AS3 ?

  3. #3
    Junior Member
    Join Date
    Mar 2015
    Posts
    13

    Red face thanks for responding.

    Quote Originally Posted by fruitbeard View Post
    Hi,

    What code are you using so far, even better attach your current file (fla and associated files), is it AS1/AS2/AS3 ?
    Because my internet I acess from my tablet and until I fix my pc I have no communication between the two I am going to have to type it out by hand so if there is typos please forgive as well I am going to only give jumping and gravity controls to seave the endless reams of code redundant to this topic.

    Code:
    onClipEvent (load){
    speed =6;
    isJumping = false;
    jumpSpeed =0;
    srartY = _y;
    var ground:MovieClip =_root.ground;
    var grav:Number =0;
    var gravity:Number =2;
    var speed:Number =15;
    var maxJump:Number = -12;
    var touching Ground:Boolean =false; 
    }
    
    onClipEvent (enterFrame) {
    _y += grav;
    grav += gravity; 
    while (ground.hitTest (_x, _y, true)){
    _y -= gravity;
    grav =0;
    }
    if ( Key.isDown (Key.SPACE) && touching Ground) {
    grav =maxJump;
    }
    if ( Key.isDown (Key.SPACE) && (_global.notJumping =true)){
    /*the above global not jumping is applied to the last frame of jumping
    *sequence to tell when jump is finished*/
    _y -=gravity; 
    grav =0;
    }
    if ( ground.hitTest (_x+(_width/2),_y-(_height/2), true)){
    _x -= speed;
    }
    if ( ground.hitTest (_x-(_width/2),_y-(_height/2), true)){
    _x += speed;
    }
    if ( ground.hitTest ( _ x, _ y - ( _height), true)){
    grav = 3; 
    }
    if ( isJumping){
    _ y += jumpSpeed; 
    jumpSpeed +=1;
    if (_y> =startY) {
    _y = startY;
    isJumping =false; 
    speed =6;
    }
    } else {
    if (Key. isDown (Key.SPACE)){
    _global. CharacterJump =true;
    this.go toAndStop ("jump");
    isJumping =true;
    jumpSpeed =-5;
    speed =6;
    }
    }
    
    if ( Key.isDown (Key.SPACE)){
    this.walking.enabled =false;
    } else{
    //here is where I say if left key is pressed walk left or if right key is pressed
    // walk right. More endless code that doesn't pertain to my jumping issue
    //and which works just fine.
    }

    I hope you can understand the issue with the snippet of code I provided and thank you immensely for taking the time to review it. Mostly I hope I didn't leave too many typos typing it by hand. Thanks again.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,785
    Hi,

    if you attach your file we could possibly be of more help as it is time consuming trying to copy your file and fix code errors at the same time, however, if you look at these sites you may be able to figure it yourself, code looks virtually the same too.

    http://www.newgrounds.com/bbs/topic/1192299

    https://stackoverflow.com/questions/...ing-my-mc-jump

    http://www.actionscript.org/forums/s....php3?t=195905

  5. #5
    Junior Member
    Join Date
    Mar 2015
    Posts
    13

    Red face thanks for trying to help.

    Quote Originally Posted by fruitbeard View Post
    Hi,

    if you attach your file we could possibly be of more help as it is time consuming trying to copy your file and fix code errors at the same time, however, if you look at these sites you may be able to figure it yourself, code looks virtually the same too.

    http://www.newgrounds.com/bbs/topic/1192299

    https://stackoverflow.com/questions/...ing-my-mc-jump

    http://www.actionscript.org/forums/s....php3?t=195905

    Thanks again for trying to help your heart certainly is in the right place. I have stumbled across those articles many times. I have been searching the answer for this issue for two weeks now. Knowing me whatever it is I'm missing, it'll be right under my nose probably. Unfortunately I haven't found an article yet that has offered aid. Thanks anyways I'll just have to keep looking.

  6. #6
    Junior Member
    Join Date
    Mar 2015
    Posts
    13

    Red face

    Okay I have been mucking with the code all day and though as I said those links that you provided didn't overly help as I didn't know how to customize them. I'm not the most experienced if you may have guessed. I have rigged up a code that sort of works. He'll execute a perfect jump despite how long I hold the spacebar, but he'll only do it once. Also, I still have not ironed out if the jump button is only tapped to still go through with the full code and animation. Below I have typed basically the changes I have made and then some so you can see where the changes sit in the code.
    Code:
    onClipEvent ( enterFrame ) {
    keysDown = false; 
    _y += grav; 
    grav + = gravity; 
    while ( ground.hitTest ( _ x, _ y, true ) ) {
    _ y - = gravity; 
    grav = 0; 
    }
    if ( Key.isDown ( Key.SPACE )  & & touchingGround){
    grav  = maxJump; 
    }
    if ( ground.hitTest ( _ x + ( _ width / 2 ), _ y - ( _ height / 2 ), true ) ) {
    _ x - = speed; 
    }
    if ( ground.hitTest ( _ x, _ y - ( height ), true ) ) {
    grav = 3;
    }
    if ( isJumping ) {
    _ y + = jumpSpeed; 
    jumpSpeed + = 1; 
    if ( _ y > startY){
    _y = startY; 
    IsJumping = false; 
    speed = 6; 
    }
    } else {
    if ( Key.isDown ( Key.SPACE ) & & ( _ global.EndJump == true) && timer ==0){
    //I declared timer =0 in a var at top and _global.EndJump becomes true at the
    //last frame of jump cycle hence the end jump terminology
    _global.EndJump = false; 
    _global. CharacterJump = true // basically resets the jump so it always plays
    //from the start ( it worked fine before I muddied the code, as I said it now only 
    //Plays once unfortunately.
    this.gotoAndStop ( " jump " ); 
    isJumping = true; 
    jumpSpeed =- 5;
    speed = 6; 
    CheckJump = true; 
    timer = 0; 
    time++;
    countupInterval = setInterval ( countup,  1000);
    } else if ( Key.isDown ( Key.SPACE ) & & timer > 0 ) {
    clearInterval ( countupInterval ); 
    timer = 0; 
    break
    }
    
    if (! ground.hitTest ( _ x, _ y, true ) & & _global. JumpTest = = false & & CheckJump = = true ) {
    break
    }
    
    if ( Key.isDown ( Key.SPACE ) ) {
    this.walking.enabled = false; 
    } else {
    
    //this is all my character movement controls as you know
    I am still looking for why the jump won't reset after its flawless execution. I suspect perhaps maybe the timer is the culprit. It was put there to prevent the character from jumping again and again if the spacebar is held down. That issue will need more mucking. This was more intended as an update. I should also mention, being what I consider a noob, I am always open to suggestions how to tidy up sloppy code and so forth, I want to learn good habits. Thanks again for all that you have done.
    Last edited by frogies_rock003; 03-29-2015 at 06:39 PM.

  7. #7
    Junior Member
    Join Date
    Mar 2015
    Posts
    13
    Well I have basically got it all figured out, thanks for all the help. The only thing I would like to pursue is getting flash to distinguish between a key tap and a key hold so as to execute the code differently depending. Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center