Advertisement
Guest User

Untitled

a guest
Sep 17th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var obj1 = _root.attachMovie("obj1","obj1"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:100,_y:200});
  2. obj1.xspeed = 0;
  3. obj1.yspeed = 0;
  4.  
  5. var obj2 = _root.attachMovie("obj2","obj2"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:450,_y:200});
  6. obj2.xspeed = 0;
  7. obj2.yspeed = 0;
  8.  
  9. var friction = .9;
  10. var speed = 1.6;
  11.  
  12. function onEnterFrame() {
  13.    
  14.     if(Key.isDown(Key.RIGHT)) {
  15.         obj1.xspeed -= speed;
  16.     }
  17.     if(Key.isDown(Key.LEFT)) {
  18.         obj1.xspeed += speed;
  19.     }
  20.     if(Key.isDown(Key.DOWN)) {
  21.         obj1.yspeed -= speed;
  22.     }
  23.     if(Key.isDown(Key.UP)) {
  24.         obj1.yspeed += speed;
  25.     }
  26.    
  27.     if(Key.isDown(65)) {
  28.         obj2.xspeed += speed;
  29.     }
  30.     if(Key.isDown(68)) {
  31.         obj2.xspeed -= speed;
  32.     }
  33.     if(Key.isDown(83)) {
  34.         obj2.yspeed -= speed;
  35.     }
  36.     if(Key.isDown(87)) {
  37.         obj2.yspeed += speed;
  38.     }
  39.    
  40.     obj1._x -= obj1.xspeed;
  41.     obj1._y -= obj1.yspeed;
  42.     obj1.xspeed *= friction;
  43.     obj1.yspeed *= friction;
  44.    
  45.     obj2._x -= obj2.xspeed;
  46.     obj2._y -= obj2.yspeed;
  47.     obj2.xspeed *= friction;
  48.     obj2.yspeed *= friction;
  49.    
  50.     if(checkCollision()) {
  51.         solveCollision(obj1, obj2);
  52.     }
  53.    
  54.     if(obj1._x <= 10) {
  55.         obj1._x = 10;
  56.         obj1.xspeed = 0;
  57.     }
  58.     if(obj1._x >= 540) {
  59.         obj1._x = 540;
  60.         obj1.xspeed = 0;
  61.     }
  62.     if(obj1._y <= 10) {
  63.         obj1._y = 10;
  64.         obj1.yspeed = 0;
  65.     }
  66.     if(obj1._y >= 390) {
  67.         obj1._y = 390;
  68.         obj1.yspeed = 0;
  69.     }
  70.    
  71.     if(obj2._x <= 10) {
  72.         obj2._x = 10;
  73.         obj2.xspeed = 0;
  74.     }
  75.     if(obj2._x >= 540) {
  76.         obj2._x = 540;
  77.         obj2.xspeed = 0;
  78.     }
  79.     if(obj2._y <= 10) {
  80.         obj2._y = 10;
  81.         obj2.yspeed = 0;
  82.     }
  83.     if(obj2._y >= 390) {
  84.         obj2._y = 390;
  85.         obj2.yspeed = 0;
  86.     }
  87.    
  88.     obj1.xspeed = Math.min(obj1.xspeed, 8);
  89.     obj1.yspeed = Math.min(obj1.yspeed, 8);
  90.     obj1.xspeed = Math.max(obj1.xspeed, -8);
  91.     obj1.yspeed = Math.max(obj1.yspeed, -8);
  92.    
  93.     obj2.xspeed = Math.min(obj2.xspeed, 8);
  94.     obj2.yspeed = Math.min(obj2.yspeed, 8);
  95.     obj2.xspeed = Math.max(obj2.xspeed, -8);
  96.     obj2.yspeed = Math.max(obj2.yspeed, -8);
  97. }
  98.  
  99. function checkCollision() {
  100.     if(Math.abs(obj1._x-obj2._x) <= (obj1._width/2 + obj2._width/2) && Math.abs(obj1._y-obj2._y) <= (obj1._width/2 + obj2._width/2)) {
  101.         return true;
  102.     }
  103. }
  104.  
  105. function solveCollision(obj1:MovieClip , obj2:MovieClip) {
  106.    
  107.     var x1:Number = obj1._x;
  108.     var y1:Number = obj1._y;
  109.     var dx:Number = obj2._x - x1;
  110.     var dy:Number = obj2._y - y1;
  111.     var dist:Number = Math.sqrt(dx * dx+dy * dy);
  112.    
  113.     var radius:Number = 10;
  114.    
  115.     var nX:Number = dx / dist;
  116.     var nY:Number = dy / dist;
  117.    
  118.     var midpointX:Number = (x1 + obj2._x)/2;
  119.     var midpointY:Number = (y1 + obj2._y)/2;
  120.    
  121.     obj1._x = midpointX-nX*radius;
  122.     obj1._y = midpointY-nY*radius;
  123.     obj2._x = midpointX+nX*radius;
  124.     obj2._y = midpointY+nY*radius;
  125.    
  126.     var distanceVector:Number = (obj1.xspeed - obj2.xspeed) * nX + (obj1.yspeed - obj2.yspeed) * nY;
  127.     var distVectorX:Number = distanceVector * nX;
  128.     var distVectorY:Number = distanceVector * nY;
  129.    
  130.     obj1.xspeed -= distVectorX;
  131.     obj1.yspeed -= distVectorY;
  132.    
  133.     obj2.xspeed += distVectorX;
  134.     obj2.yspeed += distVectorY;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement