A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [F8] Help fix my code!

  1. #1
    Junior Member
    Join Date
    Jun 2006
    Posts
    10

    [F8] Help fix my code!

    So Its my first time programming a game, and its a simple platformer. All of the tutorials I found really sucked, so I've basically been working it out myself. I managed to get the character to move and jump, but the one issue I'm having is that when the character falls onto the ground, because of the amount of pixels it moves down each frame, he ends up inside the ground. The way I tryed to solve this was to have him move up a pixel at a time until its back on the top, but that causes him to slowly crawl back up. What I need is help making it go right on top of the ground on impact. I'll some code.

    Also to clear it up, the code makes the ground move around the player, who stays still, and the ground is called groundMC, while the player is called charMC.
    both MCs are placed on frame one of the main timeline.

    Code:
    //set the variables for movement
    onClipEvent (load) {
    	isJumping = true;
    	gravity = 0;
    	speed = 5;
    }
    //Cause the character to fall
    onClipEvent (enterFrame) {
    	this._y -= gravity;
    }
    //Stop the character from falling if they touch the ground
    onClipEvent (enterFrame) {
    	if (this.hitTest(this._parent.charMC._x, this._parent.charMC._y+(this._parent.charMC._height), true)) {
    		gravity = 0;
    		isJumping = false;
    		this._y = this._y + 1;
    		starty = this._y;
    	}
    	if (!this.hitTest(this._parent.charMC._x, this._parent.charMC._y+(this._parent.charMC._height +1), true)) {
    		gravity += 1;
    	}
    }
    //Set Movement Keys
    onClipEvent (enterFrame) {
    	if (Key.isDown(Key.RIGHT)){
    		this._x -= speed;
    	}
    	if (Key.isDown(Key.LEFT)){
    		this._x += speed;
    	}
    	if (Key.isDown(Key.SPACE)) {
    		if (isJumping == false) {
    			this._y = this._y+7;
    			isJumping = true;
    			gravity -= 14;
    			this._y += gravity;
    		}
    	}
    	
    }
    onClipEvent (enterFrame) {
    	if(isJumping == true) {
    		if (this._y <= starty - 100) {
    			gravity += 1;
    		}
    	}
    }

  2. #2
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    well, i tried to re-create you're problem with the sample code you've given me but no go. I think a problem like this calls for an attached fla. I'm going to say the problem is with the hitTest somewhere but I can't say for sure
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

  3. #3
    Junior Member
    Join Date
    Jun 2006
    Posts
    10
    Here, I've attached the .fla, if you can make it work, I will be very impressed.
    Although I do believe I've made some minor alterations to the posted code, but it will be in the fla anyways.
    Attached Files Attached Files

  4. #4
    Developing For Dunkets mneil's Avatar
    Join Date
    Mar 2007
    Location
    Lincoln City
    Posts
    2,156
    Rather than go through this and make changes I think I'll just lend you some code I've got. Some of it I've taken from others, some of it I wrote myself, and some of I don't know about. I've got two examples. One is titled gravity, which is just an example of gravity based on a ball. The second is an adaptation of the first where I've animated a character and applied the ball physics to that character. I'm hoping that will help you. I'll be away from my computer for the weekend starting in about 20 min so I thought the files would be some temporary help for you to pick at.
    Last edited by mneil; 08-19-2008 at 02:31 PM.
    http://code.mneilsworld.com/
    Text Effects | Bubbles | Dynamic Resize
    4 weeks , 20 papers ... thats 2 dollars .....Caassshhh!

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