Home Contact RSS

Gravity

1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5 out of 5)
Loading ... Loading ...

A variation of the gravity with throwable ball experiment. It has optional gravity.

Click the ball to drag and release to drop or throw it.
Press the space bar to add more balls (up to 30).
Press the down arrow to turn gravity off, and pres the up arrow to turn it back on.

You can make some pretty interesting movements. The source fla file is at the bottom of the post.

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)

Here’s the Actionscript:

  1. //global variables initialized in frame 1
  2. gravity=2;
  3. drag=.99;
  4. bounce=.8;
  5. _root.id = 1;
  6.  
  7. //on the ball
  8. onClipEvent (load) {
  9. //random placement
  10. this._x = random(Stage.width);
  11. this._y = random(Stage.height);
  12. //random x and y speed
  13. this.xspeed = Math.random() * 30;
  14. this.yspeed = Math.random() * 30;
  15. //give this ball an id
  16. this.ballNum = _root.id;
  17. //increment id until 30 and then reset
  18. if (_root.id < 30) {
  19. _root.id++;
  20. }
  21. else {
  22. _root.id = 2;
  23. }
  24. }
  25. //drag ball on press
  26. on (press) {
  27. startDrag(this);
  28. this.dragging = true;
  29. }
  30. //create new ball with space bar
  31. on (keyPress "<Space>") {
  32. if (this.ballNum == 1) {
  33. duplicateMovieClip(_root.ball, "ball" + _root.id, _root.id);
  34. }
  35. }
  36. //toggle gravity with up and down arrow
  37. on (keyPress "<Down>") {
  38. _root.gravity = 0;
  39. }
  40. on (keyPress "<Up>") {
  41. _root.gravity = 2;
  42. }
  43. //drop on release
  44. on (release, releaseOutside) {
  45. stopDrag();
  46. this.dragging = false;
  47. }
  48. onClipEvent (enterFrame) {
  49. //if not dragging
  50. if (!this.dragging) {
  51. //calculate new x and y position
  52. this._y = this._y + this.yspeed;
  53. this._x = this._x + this.xspeed;
  54. //bounce off the bottom of stage and reverse yspeed
  55. if (this._y + this._height / 2 > Stage.height) {
  56. this._y = Stage.height - this._height / 2;
  57. this.yspeed = -this.yspeed * _root.bounce;
  58. }
  59. //bounce off the top of stage and reverse yspeed
  60. if (this._y - this._height / 2 < 0) {
  61. this._y = this._height / 2;
  62. this.yspeed = -this.yspeed * _root.bounce;
  63. }
  64. //bounce off right of stage
  65. if (this._x + this._width / 2 > Stage.width) {
  66. this._x = Stage.width - this._width / 2;
  67. this.xspeed = -this.xspeed * _root.bounce;
  68. }
  69. //bounce off left of stage
  70. if (this._x - this._width / 2 < 0) {
  71. this._x = this._width / 2;
  72. this.xspeed = -this.xspeed * _root.bounce;
  73. }
  74. //recalculate x and y speeds figuring in drag (friction) and gravity for y
  75. this.yspeed = this.yspeed * _root.drag + _root.gravity;
  76. this.xspeed = this.xspeed * _root.drag;
  77. }
  78. else {
  79. //if dragging then calculate new speeds according to dragging movement and speed
  80. this.xspeed = this._x - this.oldx;
  81. this.yspeed = this._y - this.oldy;
  82. this.oldx = this._x;
  83. this.oldy = this._y;
  84. }
  85. }

Here is the source to download and play with. I learned most of this from here, Keith Peter’s gravity tutorial!

Tags: , , , , ,

Related posts

flashden banner

Leave a Comment