A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Move and jump

  1. #1
    Member
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    97

    Move and jump

    Hi,
    I know that some of you make games where the hero can move right and left and jump (example: HeliAttack3). I'd like to make a game like that but I want to know what's the best code I can use for that. So, I only want the hero to move right-left, to jump and, if possible, to double jump. I'm currently using a code that I found in a tutorial but that doesn't work very well.
    Thanks a lot, your help is very appreciated!

    GameFlasher

  2. #2
    Senior Member
    Join Date
    Feb 2004
    Posts
    782
    Instead of just reading the tutorials and swiping the code, why don't you post some of the code you've written?

    Generally, you would use a key listener to tell if a key has been pressed and just adjust the charater x,y position when a certain key is pressed.

    Jumping, you would need some set gravity.

  3. #3
    Member
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    97
    I want the background to move, so that the hero always stay in the middle of the screen.
    Actually, I use this code to move left:

    on (keyPress "<Left>")
    {
    background._X = background._X + 10;
    }

    And this one to move right:

    on (keyPress "<Right>")
    {
    background._X = background._X + -10;
    }

    These ones work well, but I have more difficulties with jumping. I know how to use gravity, but only when the hero moves. With the background, I don't really know... And that's another point; I want the hero to jump by itself (not the background), but if the hero jump while running right or left, the background has to move.
    Can you help me?
    Last edited by GameFlasher; 07-13-2005 at 03:42 PM. Reason: Sorry, I did an error with my code...!

  4. #4
    Senior Member
    Join Date
    Feb 2004
    Posts
    782
    Set the gravity to 0 when the hero is on the ground and then set the gravity to whatever when he is not.
    its like:
    [code]
    if(this._y>400)
    {
    gravity=0;
    }
    else
    {
    gravity=3;
    }

    Make another layer as the background and make the backgroound a movie clip that moves when the hero does.

  5. #5
    Member
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    97
    Archbob, can you tell me...
    If to move the background LEFT I use this: +10
    and to move the background RIGHT I use this: +-10
    what will I have to use to move the background down (when the hero goes Up (jump))
    Thanks!

  6. #6
    AquaCorpse saves the day again
    Join Date
    Jun 2005
    Posts
    40
    you don't need the background to move down unless he can just so high he jumps above the objects in the background
    aqua corpse he's a really cool guy...

  7. #7
    Senior Member
    Join Date
    Feb 2004
    Posts
    782
    Actually I think he wants a scrolling background for one of those games where he's continually jumping up pegs and stuff. Unfortunately I think those type games are draw bit-by-bit alot. I'm talking about a game like Castle Cat. Not sure how to make a game like that.

  8. #8
    Senior Member
    Join Date
    Jun 2005
    Posts
    203
    Make your background into a movie clip and give it the instance "ground" and put this code in it (By background I mean the the collision boxes, just an outline of the actual level where the character will be able to walk and stuff)
    Code:
    onClipEvent (load) {
    	_root.falltime = 0;
    	groundspeed = 10;
    }
    onClipEvent (enterFrame) {
    	if (Key.isDown(Key.SPACE) && !jumping) {
    		vel_y = 38;
    		jumping = true;
    		_root.character.gotoAndStop("jumpframe#");
    	}
    	if (jumping == true) {
    		vel_y -= 2;
    		if (vel_y<=-15) {
    			vel_y = -15;
    		}
    		if (vel_y<15) {
    			_root.character.gotoAndStop("fallingframe#");
    			_root.falltime2++;
    		}
    		this._y += vel_y;
    	}
    	if (this.hitTest(_root.character._x, _root.character._y+18, true)) {
    		vel_y = 0;
    		_root.character.gotoAndStop(1);
    		_root.falling = false;
    		_root.falltime = 0;
    		jumping = false;
    	} else {
    		this._y -= 20;
    		_root.falltime++;
    		if (jumping == false and _root.falltime>=5) {
    			_root.character.gotoAndStop(3);
    			_root.falling = true;
    		}
    	}
    }
    onClipEvent (enterFrame) {
    	if (_root.scrollleft == true and _root.stopscroll == false) {
    		this._x -= groundspeed;
    	} else if (_root.scrollright == true and _root.stopscroll2 == false) {
    		this._x += groundspeed;
    	}
    	if (this.hitTest(_root.character._x, _root.character._y, true)) {
    		_root.ground._y += 5;
    	}
    }
    then name your character "character" and make the registration point to where his feet are (the "+" thing) and put this code in him:
    Code:
    onClipEvent (load) {
    	speed2 = 8;
    	_root.scrollright = false;
    	_root.scrollleft = false;
    	_root.stopscroll2 = false;
    	_root.stopscroll = false;
    	boundry = _x=100;
    	boundry2 = _x=325;
    }
    onClipEvent (enterFrame) {
    	if (_root.ground.hitTest(this._x+30, this._y-25, true)) {
    		_root.stopscroll = true;
    	} else {
    		_root.stopscroll = false;
    	}
    	if (_root.ground.hitTest(this._x-30, this._y-25, true)) {
    		_root.stopscroll2 = true;
    	} else {
    		_root.stopscroll2 = false;
    	}
    	if (Key.isDown(Key.RIGHT) && !_root.ground.hitTest(this._x+15, this._y-25, true)) {
    		_xscale = 100;
    		if (this._x<boundry2) {
    			this._x += speed2;
    		} else {
    			_root.scrollleft = true;
    		}
    	} else if (Key.isDown(Key.LEFT) && !_root.ground.hitTest(this._x-15, this._y-25, true)) {
    		_xscale = -100;
    		if (this._x>boundry) {
    			this._x -= speed2;
    		} else {
    			_root.scrollright = true;
    		}
    	}
    }
    onClipEvent (keyUp) {
    	if (Key.getCode() == Key.RIGHT) {
    		_root.scrollleft = false;
    	} else if (Key.getCode() == Key.LEFT) {
    		_root.scrollright = false;
    	}
    }
    There you have it, you can move, the background scrolls, you can jump, etc. Now you can mask the background with the actual art of the level, and have the registration points match up and put this code in the art background:
    Code:
    onClipEvent(enterFrame){
    this._x = _root.ground._x
    this._y = _root.ground._y
    }
    Tsehehehehehe

  9. #9
    Member
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    97
    Thanks The Mercenary,
    Works very well, exactly what I wanted!

  10. #10
    Senior Member random10122's Avatar
    Join Date
    Mar 2002
    Location
    Sheffield, UK
    Posts
    1,747
    Now the challenge is to understand it eh? ;-)

    fracture2 - the sequel
    fracture - retro shooter
    blog - games, design and the rest

    "2D is a format, not a limitation" -Luis Barriga

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