A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: [help] How to create simple gravity

  1. #1
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874

    [help] How to create simple gravity

    I would like to create a simple gravitational pull...

    When my hero moves "player" i want him to roll to a stop, like friction...

    And when he falls down a hill i want him to increase to a sertain speed and when he hits the gound bounce a little bit...

    i dont know were to start or if there is some code in Fash that has sumthing like this already buily in....

    PS: i would like your sugestions to be flash 5+ as i use flash MX 2004 thanks
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  2. #2
    Insignificant Member Joshman_123's Avatar
    Join Date
    Nov 2004
    Posts
    395
    Learn yourself then do it yourself. Stop asking everyone here to code a game for you.

  3. #3
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    Sorry...
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  4. #4
    Insignificant Member Joshman_123's Avatar
    Join Date
    Nov 2004
    Posts
    395
    That's fine. Common mistake. No one here is going to do that for free though.
    I have like, a gazillion posts on my other account.

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,223
    These are simple and well written tutorials including gravity and easing:
    http://www.bit-101.com/tutorials/

  6. #6
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    Quote Originally Posted by Joshman_123
    Learn yourself then do it yourself. Stop asking everyone here to code a game for you.
    You'll be happy to know i have spent the last 2 hours trying to do it all by my self...

    But i have come across a problem, which i hope some one will be able to help me fix. I cant get my MC called player to deaccerlerate to a stop when your arnt pressing down any of the movment keys...
    Code:
    // Acceleration and Top Speed
    onClipEvent (load) {
    	weopon.gotoAndStop(1);
    	gotoAndStop(1);
    	acceleration = .1;
    	speed = 10;
    }
    // Accerlation Right
    onClipEvent (enterFrame) {
    	if (Key.isDown(Key.RIGHT)) {
    		acceleration++;
    		gotoAndStop(2);
    		if (acceleration>speed) {
    			acceleration--;
    		}
    	}
    	// Accerlation Left
    	if (Key.isDown(Key.LEFT)) {
    		acceleration--;
    		gotoAndStop(1);
    		if (acceleration<-speed) {
    			acceleration++;
    		}
    // Deaccerlation **** THIS IS WERE MY PROBLEM IS ****
    		if (Key.isDown()) {
    			if (acceleration<0) {
    				acceleration++;
    			} else {
    				if (acceleration>0) {
    					accerlation--;
    				}
    			}
    		}
    	}
    	_x += acceleration;
    }
    Whats wrong? to me that code seems perfectly normal, I was even proud of my self for comming up with such a kewl thing. (Sorry for being a newbie)
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  7. #7
    Feeling adventurous? T1ger's Avatar
    Join Date
    Mar 2004
    Posts
    850
    Quote Originally Posted by Joshman_123
    Learn yourself then do it yourself. Stop asking everyone here to code a game for you.

    That's fine. Common mistake. No one here is going to do that for free though.
    Why so harch on a new guy? He just asked a question. If you dont like it, you dont have to reply. And someone wondered why newbies don't stick around...

    if (Key.isDown()) wont work, try this instead:
    code:

    if (Key.isDown(Key.RIGHT)) {
    //acclerate right
    } else if (Key.isDown(Key.LEFT)) {
    //acclerate left
    } else {
    //deacclerate
    }



    the thing is, when the player presses both left and right at the same time, he acclerates right. Not a big problem, but if you want him to deacclerate when no or both buttons are pressed, just say so, and I'll help you

    Good luck
    I don't have a photograph, but you can have my footprints. They're upstairs in my socks.

  8. #8
    Insignificant Member Joshman_123's Avatar
    Join Date
    Nov 2004
    Posts
    395
    Quote Originally Posted by T1ger
    Why so harch on a new guy? He just asked a question. If you dont like it, you dont have to reply. And someone wondered why newbies don't stick around...
    He has a few other threads though. I didn't mean to come across so harsh.


    I'm pretty sure it's "deceleration" by the way.
    I have like, a gazillion posts on my other account.

  9. #9
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    Thanks for that code T1ger, i have now gotten it to work but.... it only decelerates left, the right side dosnt seem to work?

    Here is my code, I have tryed heeps of ways to get it to work but i cant.
    Also thanks again T1ger, you'll notice its much shorter now

    Code:
     onClipEvent (enterFrame) {
    	if (Key.isDown(Key.RIGHT)) {
    		acceleration++;
    		gotoAndStop(2);
    		if (acceleration>speed) {
    			acceleration--;
    		}
    	} else if (Key.isDown(Key.LEFT)) {
    		acceleration--;
    		gotoAndStop(1);
    		if (acceleration<-speed) {
    			acceleration++;
    		}
    //         ****  Deceleration Code  ****
    	} else {
    		if (acceleration<0) {
    			acceleration++;
    		} else {
    			if (acceleration>0) {
    				accerlation--;
    			}
    		}
    	}
    	_x += acceleration;
    }
    P.S. I also tryed the deceleration code with }else if ( rather than what i have now }else{ if (
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  10. #10
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    Also joshman it kewl... i have bin posting a lot of help lately... its just that i love all this flash stuff and i am trying to learn as much as i can as fast as i can so please try and not get angry with me for being a noob and asking for lots of things.

    I learn faster breaking down code and looking at it than trying to figure it out my self. I also rewrite all the code again so i understand it and save them for future reference (just in case i forget).

    Thanks guys for helping me, ya never know i might even end up helping you one day :P
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  11. #11
    Insignificant Member Joshman_123's Avatar
    Join Date
    Nov 2004
    Posts
    395
    I was at that stage not long ago. It's just the first hurdle of understanding the ActionScript statements and what they do. The best tutorial I've done is the ActionScript tutorial at www.newgrounds.com/help or www.newgrounds.com/flash or something. I recommend that to you. Good luck and I'll try to help you as much as I can. Just cut back on the thread-making and you'll be right
    I have like, a gazillion posts on my other account.

  12. #12
    Script kiddie VENGEANCE MX's Avatar
    Join Date
    Jun 2004
    Location
    England
    Posts
    2,590
    Yeah, I don't suppose you could narrow it down a little bit?

    Here's one I made for COMPLETE beginners who want to learn how to make buttons and use basic syntax:

    http://www.newgrounds.com/portal/view/219775

    And here's another I made for creating an FPS from scratch, although I think there's a bug or two in it somewhere:

    http://www.newgrounds.com/portal/view/201217
    http://www.birchlabs.co.uk/
    You know you want to.

  13. #13
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    Thanks guy's.

    Do any of your know what is wrong with my code? No matter how many different ways i try it wont work.

    Please Help
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

  14. #14
    Senior Member TeroLebe's Avatar
    Join Date
    Mar 2003
    Location
    Lahti, Finland
    Posts
    302
    Quote Originally Posted by Gloo pot
    Do any of your know what is wrong with my code? No matter how many different ways i try it wont work.
    Please Help

    accerlation-- shoud be spelled acceleration--, it's very common error to have typo in code, so be sure you learn to spot these out with yourself.

    This is just a short code, so it could get very hard later on if somebody else always have to find the small typo.

  15. #15
    Senior Member Gloo pot's Avatar
    Join Date
    Aug 2005
    Location
    Australia Mate!
    Posts
    874
    oh my bad, thanks it works now, i bet that was the problem in my first attemp
    92.7 Fresh FM for all your South Aussies - Doof Doof music FTW people!

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