dcsimg
A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: yet another jumping problem

Hybrid View

  1. #1
    Member
    Join Date
    Jun 2009
    Posts
    30

    yet another jumping problem

    hy, i have run into another problem, i have tried to create a code that make a ball jump, however when i press space it doesnt seem to be working, i have attached the file so you can have a look at it.

    PHP Code:
    y_start hero_y;
    jumping false;
    jumpspeed 0;



    if (
    jumping) {
    hero_y += jumpspeed;
    jumpspeed += 1;
    }

    if (
    hero_y>=y_start) {
    hero_y y_start;
    jumping false;
    } else {
    if (
    Key.isDown(Key.SPACE)) {
    jumping true;
    jumpspeed = -14;
    }

    thank you
    Attached Files Attached Files

  2. #2
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,928
    A good tip is when things aren't working is to use trace() to see exactly what's happening.

    if (hero_y>=y_start) {
    hero_y = y_start;
    jumping = false;
    }

    I'm guessing that hero._y ( I noticed you've missed out the "." on all the _y properties ) is always greater > or equal = to y_start, as you set it to that value at the start.

    Drop a trace after the else where you check the keyboard, if it never gets triggered that's your problem.

    Squize.

  3. #3
    jtnw's Avatar
    Join Date
    May 2002
    Posts
    1,328
    What squize said, also
    Code:
    if (hero_y>=y_start) { 
    hero_y = y_start; 
    jumping = false; 
    } else { //hero._y was initially set to equal y_start, so this code never executes
    if (Key.isDown(Key.SPACE)) { 
    jumping = true; 
    jumpspeed = -14; 
    } 
    }
    make it

    Code:
    if (hero_y>=y_start) { 
    hero_y = y_start; 
    jumping = false; 
    }
    if (Key.isDown(Key.SPACE)) { 
    jumping = true; 
    jumpspeed = -14; 
    }
    [edit]opps, didn't really read squizes post, he mentioned this too[/edit]
    j

  4. #4
    Member
    Join Date
    Jun 2009
    Posts
    30
    thank you Squize and jtnw, but what i am trying to do is that the hero can jump no matter what its postion is i.e. the hero doesnt have to be at a fixed location, the _ystart will be determined by the location of the heros._y.... i have tried using the fix that jtnw suggest and using trace that squize suggested but it hasnt worked.

    PHP Code:
    //Movement
    function Walking() {

        if(
    Key.isDown(Key.RIGHT)){
        
    hero._x += 10;
        }

        if(
    Key.isDown(Key.LEFT)){
        
    hero._x -= 10;
        }

        if(
    Key.isDown(Key.UP)){
        
    hero._y -= 10;
        }

        if(
    Key.isDown(Key.DOWN)){
        
    hero._y += 10;
        }
    }



    //Jump
    function Jump() {
        
    y_start hero._y;
    jumping false;
    jumpspeed 0;


    if (
    jumping) {
    hero._y += jumpspeed;
    jumpspeed += 1;
    }

    if (
    hero._y>=y_start) { 
    hero._y y_start
    jumping false
    }

    if (
    Key.isDown(Key.SPACE)) { 
    jumping true
    jumpspeed = -14;
    trace("jumping");
    }
    }


    onEnterFrame = function()

    Walking();
    Jump();


    p.s. i have added the walking function and also attached the new .fla.... thank you
    Attached Files Attached Files

  5. #5
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    Not shure if this is what you want but it jumps :P
    Code:
    var jcount:Number = 1;
    var grav:Number = 0;
    //Jump
    function Jump() {
    
    
    if (jcount == 0) {
    grav = jumpspeed;
    jumpspeed += 1;
    }
    jcount ++;
    
    if (jumping) {
    	hero._y += grav;
    	grav ++;
    	if (hero._y>=y_start) { 
    		hero._y = y_start; 
    		jumping = false; 
    	}
    }
    
    if (Key.isDown(Key.SPACE) && !jumping) { 
    jcount = 0;
    jumping = true; 
    y_start = hero._y;
    jumpspeed = -14;
    trace("jumping");
    }
    }
    the jcount stuff is to set the jumpspeed to the gravity only once (when it is equal to 0). This is because, if it is done in other way, keeping spacebar pressed would make the guy float until you release it. And that because an onEnterframe event is used for the spacebar pressing instead of a keyDown event. And as and advice, it will be better with a higher framerate, it would make it looks more smooth.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

  6. #6
    Member
    Join Date
    Jun 2009
    Posts
    30
    thanks Phobik, your code works brilliantly but, when u press up and space, the ball jumps higher, is it possible to make it so that when space is pressed, until it lands on the floor no arrow key function can be executed?

  7. #7
    President PhobiK's Avatar
    Join Date
    Jan 2005
    Location
    Guatemala
    Posts
    918
    yes, you can do it by coding someting like if(Key.isDown(Key.UP) && !jumping) ... just say that they can move only when jumping is false. Hope it helps.
    This is MC. His _parents sent him to stop() by the super market to buy some _root beer if he wanted to gotoAndPlay() with his friends at the park later.

    This is my Blog!... The gaming Process
    Please check out my site: Giddel Creations

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