A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Problems with jumping in a platformer.

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    23

    Problems with jumping in a platformer.

    I'm making a platforming game, but whenever my character comes down from a jump, he never lands at the same Y position. Sometime he lands a few pixels above the ground, sometimes he lands well below the ground. All I have in place a hit test--when my character and the ground collide, he stops falling. But I want something more consistent. Does anyone have any ideas?

    Code:
    if (jump)
    	{blob._y -= jumpSpeed;
    	jumpSpeed -= gravity;}
    
    if (jump && _root.ground.Floor.hitTest(_root.blob))
    	{jump = false;
    	jumpSpeed = 40;}

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,139
    when your object is falling it isn't moving through all the pixels on the screen. Slow your movie down to 1fps to see what your game is calculating every frame. You'll see that the object jumps from its last position to its new position instantly. So what's happening is your object may be just slightly above your floor (1px) and then in the next frame be 10 pixels into the floor. Your logic catches this event and stops the movement, but you don't correct the visual position.

    So, you need to find the y coordinate of the top of your Floor object and set your falling object to that, maybe something like this:

    _root.blob._y = _root.ground.Floor._y - (_root.ground.Floor._height / 2) - (_root.blob._height / 2);

    assuming that both the blob and floor are centered.
    lather yourself up with soap - soap arcade

  3. #3
    Senior Member ozmic66's Avatar
    Join Date
    Oct 2005
    Posts
    472
    Yea, it all comes down to finding the y value of the ground so you can re-position your player there. If your floor is just a rectangle it's easy to tell where the top is, but if it's an irregular shape (or just something you drew) you'd need a slightly different approach.

    For irregular shapes:
    Instead of doing a single hit test where the player is currently, you could do what's called a sweep test. You'd do several hittests along the the line connecting the player's previous and current position. So starting where the player was a frame ago, you'd hitTest at regular intervals, in essence 'sweeping' the whole area that was covered during the last frame. The first point to hit the ground plane can be used as the surface. The more tests you do per line, the more precise a location you'd get... it's a bit of a balancing act to find what looks good

    If you feel like a picture would help, let me know
    Pixelwave Flash-based iPhone framework
    iPhone Games: Flyloop | Freedom Run

    Twitter: Oztune

  4. #4
    Junior Member
    Join Date
    May 2008
    Posts
    23
    Okay, I think I figured it out. I gave each platform a certain Y value, and as the main character approaches the platform, he falls until he's close enough, and then snaps directly to that Y value. Thanks.

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