:: Catégories

  • actionscript 3
  • Air
  • Flash
  • Flex
  • molehill
  • Non classé
  • papervision
  • Web

:: Derniers articles.

  • Nouveau Android Flash Game : Kick The Ducks !
  • Démo – Client FTP en Flex
  • Démo Molehill – particules
  • [TUTO] AS3 + APPARAT / TDSI, accélérez vos projets !
  • [FIXED] SecurityError: fileWriteResource
  • [Tutorial] Techniques d’optimisations en AS3
  • [Tutorial] Un client FTP en AS3 sous RoboLegs + SwfStudio – partie 2
  • [Tutorial] Un client FTP en AS3 sous RoboLegs + SwfStudio – partie 1

:: Tags

  • 3d actionscript3 ACTUce android as3 client code demo ducks Flash Flex flexunit framework ftp game jeux kick molehill optimisations particules robotlegs swfStudio tests unitaires the

:: Sites Exoa

  • Home
  • actionscript 3
  • Air
  • Flash
  • Flex
  • molehill
  • Non classé
  • papervision
  • Web

exoa labs

[TUTORIAL] La physique dans Flash

Aucun commentaire - mar 12th, 2010 - Flash, actionscript 3

Voici une petite démo de physique dans flash. Une balle rebondissante. Le sprite est soumis à des forces. Le poids, la friction, la poussée de la souris. Voici comment simuler la mécanique des forces en actionscript.
physique dans flash




















  1.  
  2. package Displays{
  3.  
  4. import flash.display.MovieClip;
  5. import flash.events.Event;
  6. import flash.events.MouseEvent;
  7. import flash.utils.getTimer;
  8. import Events.*;
  9. import Conf.*;
  10.  
  11. public class Ball extends MovieClip {
  12.  
  13. //______________________________________________________________ SETTINGS
  14. private var dragging :Boolean;
  15. private var _SPEED :Object;
  16. private var _LAST_TIME :Number;
  17. private var tmpX :Number;
  18. private var tmpY :Number;
  19.  
  20. //FORCES
  21. private var _FRICTION :Object;
  22. private var _POIDS :Object;
  23. private var _POUSSEE :Object;
  24.  
  25. public function Ball()
  26. {
  27. initForces();
  28. initObjects();
  29. addEventListener(Event.ADDED_TO_STAGE, initListeners);
  30. }
  31.  
  32. /**
  33. * Get Global force resulting all the forces other the object
  34. *
  35. * @param forces all forces as Array of Objects
  36. * @return obj the resulting force object
  37. **/
  38. private function getGlobalForce(... forces:Array):Object
  39. {
  40. var r:Object={x:0, y:0};
  41. for(var i:int=0; i<forces.length; i++) {
  42. r.x+=forces[i].x;
  43. r.y+=forces[i].y;
  44. }
  45. return(r);
  46. }
  47.  
  48. /**
  49. * Get the Acceleration
  50. *
  51. * @param timeElapsed time elapsed since last call
  52. * @param force the resulting force
  53. * @param weight the object weight
  54. * @return obj the acceleration object
  55. **/
  56. private function getAceleration(timeElapsed:Number, force:Object, weight:Number):Object
  57. {
  58. var velocity:Object = { x:0, y:0 };
  59. velocity.x = force.x / weight;
  60. velocity.y = force.y / weight;
  61.  
  62. velocity.x *= timeElapsed;
  63. velocity.y *= timeElapsed;
  64.  
  65. return(velocity);
  66. }
  67.  
  68. /***************************
  69. * THE RENDER BALL FUNCTION
  70. * *************************/
  71. public function render():void
  72. {
  73. //update friction
  74. _FRICTION = {x:-_SPEED.x*50, y:-_SPEED.y*50};
  75.  
  76. if(dragging){
  77. _POUSSEE={x:(stage.mouseX-tmpX)*_POIDS.y ,y:(stage.mouseY-tmpY)*_POIDS.y};
  78. }else {
  79. _POUSSEE.x *= 0.9;
  80. _POUSSEE.y *= 0.9;
  81. }
  82.  
  83. var globalForce:Object = getGlobalForce(_POIDS, _FRICTION, _POUSSEE);
  84.  
  85. var acceleration:Object=getAceleration((getTimer()-_LAST_TIME)/1000, globalForce, Constantes.BALL_WEIGHT);
  86.  
  87. _LAST_TIME=getTimer();
  88.  
  89. _SPEED.x += acceleration.x;
  90. _SPEED.y += acceleration.y;
  91.  
  92. collision();
  93.  
  94. if (dragging) { // if dragging, the ball follows the mouse cursor
  95. this.x = stage.mouseX;
  96. this.y = stage.mouseY;
  97. }else{ // else the ball get the acceleration
  98. this.x += Math.round(_SPEED.x);
  99. this.y += Math.round(_SPEED.y);
  100. }
  101.  
  102. tmpX = stage.mouseX;
  103. tmpY = stage.mouseY;
  104.  
  105. }
  106.  
  107. /***************************
  108. * THE COLLISION TESTS
  109. * *************************/
  110. private function collision():void
  111. {
  112. if (this.y +this.height / 2 > stage.stageHeight ) {
  113. this.y = stage.stageHeight - this.height / 2 ;
  114. _SPEED.y *= -0.8;
  115.  
  116. }
  117. if (this.y -this.height / 2 < 0 ) {
  118. this.y = this.height / 2 ;
  119. _SPEED.y *= -0.8;
  120.  
  121. }
  122. if (this.x +this.width / 2 > stage.stageWidth ) {
  123. this.x = stage.stageWidth - this.width / 2 ;
  124. _SPEED.x *= -0.8;
  125.  
  126. }
  127.  
  128. if (this.x -this.width / 2 < 0 ) {
  129. this.x = this.width / 2 ;
  130. _SPEED.x *= -0.8;
  131.  
  132. }
  133. }
  134.  
  135. /***************************
  136. * INIT OBJECT AND FORCE
  137. * *************************/
  138. private function initForces():void
  139. {
  140. _SPEED = { x:0, y:0 };
  141. _LAST_TIME = getTimer();
  142. _POIDS = { x:0, y:Constantes.GRAVITY * Constantes.BALL_WEIGHT };
  143. _POUSSEE = { x:0, y:0 };
  144. }
  145. private function initObjects():void {
  146.  
  147. this.graphics.lineStyle(1, 0);
  148. this.graphics.beginFill(0xFFFFFF);
  149. this.graphics.drawCircle(0, 0, 20);
  150. this.graphics.endFill();
  151.  
  152. }
  153.  
  154. /***************************
  155. * INIT LISTENERS
  156. * *************************/
  157. private function initListeners(e:Event):void
  158. {
  159. removeEventListener(Event.ADDED_TO_STAGE, initListeners);
  160. addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
  161. stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
  162. }
  163. private function onMouseUp(e:MouseEvent):void
  164. {
  165. dragging = false;
  166. }
  167.  
  168. private function onMouseDown(e:MouseEvent):void
  169. {
  170. dragging = true;
  171. }
  172.  
  173. }
  174.  
  175. }
  176.  

Les sources ici


    Commenter




      WORKS

    • Travaux

      PRODUCTS

    • ZenBook
    • E-ZEN Galery
    • FlashConf
    • EXOA MP3-Player

      SERVICES

    • Application Facebook
    • Advergame
    • Site Web
    • Référencement
    • Hébergement
    • Graphisme
    • Bannières & Animations
    • Application Flash

      EXOA

    • Contact
    • A propos
    • CGV
    • Mentions Légales

    © 2012 exoa labs, SIRET :517 668 349 00015 · Tél : 06.25.59.18.68 · contact@exoa.fr