A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [F8] [HELP] Character floats when jumping :S

  1. #1
    Member
    Join Date
    Sep 2007
    Location
    Newport, Wales
    Posts
    31

    [F8] [HELP] Character floats when jumping :S

    Hey ho, I've attached the fla. Just basics of platform game making the character able to jump and stand on platforms. I've had this problem both times I've done this code.

    Problem: The character floats to a platform if you jump near it or under it. Why is this and how can I fix it? Also why does the character sort of shake up and down when standing still?

    code:
    onClipEvent(load){
    var jump:Number = 0;
    var jumping:Boolean = false;
    var falling:Boolean = false;
    var g:Number = 6;
    }
    onClipEvent(enterFrame){
    if(jumping == true || falling == true){
    this._y -= jump;
    }
    if(jumping == false){
    if(!falling){
    jump = 0;
    }
    if(Key.isDown(Key.UP)){
    jump = 15;
    jumping = true
    }

    }
    if(jumping == true){
    jump -= 1;
    }
    if(jump <= -10){
    jump = -10;
    }
    for(i=0;i<g;i++){
    if(jumping == true && this.hitTest(_root["ground"+i])){

    jumping = false;
    }
    }
    if(Key.isDown(Key.LEFT)){
    this._x -= 5;
    }else if(Key.isDown(Key.RIGHT)){
    this._x += 5;
    }
    for(i=0;i<g;i++){
    if(!jumping && !this.hitTest(_root["ground"+i]) && !falling){
    falling = true;
    this._y -= 1;
    }
    }
    if(falling == true){
    for(i=0;i<g;i++){
    if(this.hitTest(_root["ground"+i])){
    falling = false;
    }
    }
    jump -= 1;
    }
    }


    (This code is on the character)
    Attached Files Attached Files

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    I only looked at the code breifly but I'm pretty sure that the floating problem is caused by your hitTests. In a situation like this, test the characters feet, not the clip itself.

    For problem two, he shakes because when you land on the ground, gravity should be set to 0. Reset it back to it's default value when the play falls or jumps.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    Member
    Join Date
    Sep 2007
    Location
    Newport, Wales
    Posts
    31
    how can you seperate the feet? just put a foot movie clip within the character movieclip?? What should I add to set gravity to 0 when on the platform?
    Last edited by Sephystryx; 10-21-2007 at 07:04 AM.

  4. #4
    Wait- what now? tidenburg's Avatar
    Join Date
    Dec 2005
    Posts
    1,471
    I'll take this opportunity to post yet again:
    http://tidenburg.co.uk/node/3
    that tutorial includes how to stop the shaking and snapping to platforms.
    "I'd only told them the truth. Was that so selfish? Our integrity sells for so little, but it is all we really have. It is the very last inch of us, but within that inch, we are free."

  5. #5
    Member
    Join Date
    Sep 2007
    Location
    Newport, Wales
    Posts
    31
    Just completed first tutorial, really great. So much clearer than others I read and the hitbox thing on the characters feet is exactly what I needed! GReat job! Although the character becomes stretched when I press left/right

  6. #6
    Wait- what now? tidenburg's Avatar
    Join Date
    Dec 2005
    Posts
    1,471
    OK. I sent you the answer via PM but for others who have this problem:
    Ok the reason its stretching is because at some point you've made the player movieclip a different size using the free transform tool. To fix this we have to find out what xscale your player is currently at which we do by adding player.dScale as a variable at the beginning. Then instead of make ._xscale -100 and 100 we make it -player.dScale and player.dScale. Here is the fixed code.
    Code:
        player.dScale = player._xscale
    
        player.gravity = 0;
    
        platforms = 3;
    
        player.xmove = 0;
    
        xinc = 1;
    
        xmax = 10;
    
        onEnterFrame = function () {
    
        	for (i=1; i<=platforms; i++) {
    
        		if (i == 1) {
    
        			player.hits = 0;
    
        		}
    
        		if (player.hitTest(_root['ground'+i]) && player.gravity>=0) {
    
        			player.hits++;
    
        			player._y = _root['ground'+i]._y;
    
        		}
    
        	}
    
        	if (player.hits>0) {
    
        		if (Key.isDown(Key.UP)) {
    
        			player.gravity = -10;
    
        		} else {
    
        			player.gravity = 0;
    
        		}
    
        	} else {
    
        		player.gravity++;
    
        	}
    
    
    
        	player._y += player.gravity;
    
        	if (Key.isDown(Key.LEFT) && player.xmove>-xmax) {
    
        		player.xmove -= xinc;
    
        		player._xscale = player.dScale;
    
        	} else if (Key.isDown(Key.RIGHT) && player.xmove<xmax) {
    
        		player.xmove += xinc;
    
        		player._xscale = -player.dScale;
    
        	} else {
    
        		player.xmove *= 0.9;
    
        	}
    
        	player._x += player.xmove;
    
        };
    Theres probably another, simpler way around this but thats my method
    "I'd only told them the truth. Was that so selfish? Our integrity sells for so little, but it is all we really have. It is the very last inch of us, but within that inch, we are free."

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