A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Problems with platform character

  1. #1
    Junior Member
    Join Date
    Jul 2010
    Posts
    9

    Problems with platform character

    Hey there,

    I would be very glad if someone could help me out so i can continue making my game. I am making a platformer and i got two problems with the hero.
    The hero is controlled with the following script.

    code:

    onClipEvent (load) {
    var ground:MovieClip = _root.ground;
    var grav:Number = 0;
    var gravity:Number = 2;
    var speed:Number = 8;
    var maxJump:Number = -16;
    var touchingGround:Boolean = false;
    }
    onClipEvent (enterFrame) {
    _y += grav;
    grav += gravity;
    while (ground.hitTest(_x, _y, true)) {
    _y -= gravity;
    grav = 0;
    }
    if (ground.hitTest(_x, _y+5, true)) {
    touchingGround = true;
    } else {
    touchingGround = false;
    }
    if (_root.charMoveRight==true)
    if (Key.isDown(Key.RIGHT)) {
    this.gotoAndStop("right")
    _x += speed;
    }
    if (_root.charMoveLeft==true)
    if (Key.isDown(Key.LEFT)) {
    this.gotoAndStop("left")
    _x -= speed;
    }
    if (_root.charMoveUp==true)
    if (Key.isDown(Key.UP) && touchingGround) {
    this.gotoAndStop("spring")
    _root.charMoveLeft = true
    _root.charMoveRight = true;
    grav = maxJump;
    }
    if (_root.charMoveDown==true)
    if (Key.isDown(Key.DOWN)) {
    this.gotoAndStop("buk")
    _root.charMoveLeft = false
    _root.charMoveRight = false;
    }
    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;
    }



    1. I can't seem to figure out how to set the "hero" movieclip to the frame named "nothing" when none of the keys that control the hero are pressed. (so the hero is doing nothing). When the hero stands still, it will still appear like he is walking or jumping (whatever has been done last)

    How do i tell Flash that something must happen when none of a certain group of keys are pressed?

    2. When you jump, the "jump" frame of the hero is shown. But as soon as you move right or left while jumping, the "right" or "left" walk frames are shown. How do i tell flash to not show the "right" or "left" frames after pressing the jump key until the hero lands back on the ground? The instance of the ground is "ground".

    Many thanks in advance!

  2. #2
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    For your first question, you just need to check that none of the keys are pressed AND the character is on the ground:
    Code:
    if(!(Key.isDown(Key.UP) || Key.isDown(Key.DOWN) || Key.isDown(Key.LEFT) || Key.isDown(Key.RIGHT)) && touchingGround){
    	this.gotoAndStop("nothing");
    }
    For the second question, only set the frame to "left" or "right" if the character is on the ground:
    Code:
    if (_root.charMoveRight==true) 
    if (Key.isDown(Key.RIGHT)) {
    	if(touchingGround)
    		this.gotoAndStop("right")
    	_x += speed;
    }
    
    if (_root.charMoveLeft==true) 
    if (Key.isDown(Key.LEFT)) {
    	if(touchingGround)
    		this.gotoAndStop("left")
    	_x -= speed;
    
    }

  3. #3
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks a lot man, it works! You really helped me out.

  4. #4
    Junior Member
    Join Date
    Jul 2010
    Posts
    9
    Thanks a lot, that works! But there is still something that isn't working.
    How can i tell Flash that the "spring" (dutch for jump) frame is always shown when the character is not on the ground? He also doesn't flip 180 degrees when jumping, so when you jump backward the animation of jumping forward is shown. Sorry, but i am not very handy at actionscript. I would highly appreciate it if you could tell me how to do this... thanks

  5. #5
    var x:Number = 1; x /= 0;
    Join Date
    Dec 2004
    Posts
    549
    just add this to the end of your script.
    Code:
    if(!touchingGround) this.gotoAndStop("spring");

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