Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var obj1 = _root.attachMovie("obj1","obj1"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:100,_y:200});
- obj1.xspeed = 0;
- obj1.yspeed = 0;
- var obj2 = _root.attachMovie("obj2","obj2"+_root.getNextHighestDepth(),_root.getNextHighestDepth(),{_x:450,_y:200});
- obj2.xspeed = 0;
- obj2.yspeed = 0;
- var friction = .9;
- var speed = 1.6;
- function onEnterFrame() {
- if(Key.isDown(Key.RIGHT)) {
- obj1.xspeed -= speed;
- }
- if(Key.isDown(Key.LEFT)) {
- obj1.xspeed += speed;
- }
- if(Key.isDown(Key.DOWN)) {
- obj1.yspeed -= speed;
- }
- if(Key.isDown(Key.UP)) {
- obj1.yspeed += speed;
- }
- if(Key.isDown(65)) {
- obj2.xspeed += speed;
- }
- if(Key.isDown(68)) {
- obj2.xspeed -= speed;
- }
- if(Key.isDown(83)) {
- obj2.yspeed -= speed;
- }
- if(Key.isDown(87)) {
- obj2.yspeed += speed;
- }
- obj1._x -= obj1.xspeed;
- obj1._y -= obj1.yspeed;
- obj1.xspeed *= friction;
- obj1.yspeed *= friction;
- obj2._x -= obj2.xspeed;
- obj2._y -= obj2.yspeed;
- obj2.xspeed *= friction;
- obj2.yspeed *= friction;
- if(checkCollision()) {
- solveCollision(obj1, obj2);
- }
- if(obj1._x <= 10) {
- obj1._x = 10;
- obj1.xspeed = 0;
- }
- if(obj1._x >= 540) {
- obj1._x = 540;
- obj1.xspeed = 0;
- }
- if(obj1._y <= 10) {
- obj1._y = 10;
- obj1.yspeed = 0;
- }
- if(obj1._y >= 390) {
- obj1._y = 390;
- obj1.yspeed = 0;
- }
- if(obj2._x <= 10) {
- obj2._x = 10;
- obj2.xspeed = 0;
- }
- if(obj2._x >= 540) {
- obj2._x = 540;
- obj2.xspeed = 0;
- }
- if(obj2._y <= 10) {
- obj2._y = 10;
- obj2.yspeed = 0;
- }
- if(obj2._y >= 390) {
- obj2._y = 390;
- obj2.yspeed = 0;
- }
- obj1.xspeed = Math.min(obj1.xspeed, 8);
- obj1.yspeed = Math.min(obj1.yspeed, 8);
- obj1.xspeed = Math.max(obj1.xspeed, -8);
- obj1.yspeed = Math.max(obj1.yspeed, -8);
- obj2.xspeed = Math.min(obj2.xspeed, 8);
- obj2.yspeed = Math.min(obj2.yspeed, 8);
- obj2.xspeed = Math.max(obj2.xspeed, -8);
- obj2.yspeed = Math.max(obj2.yspeed, -8);
- }
- function checkCollision() {
- 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)) {
- return true;
- }
- }
- function solveCollision(obj1:MovieClip , obj2:MovieClip) {
- var x1:Number = obj1._x;
- var y1:Number = obj1._y;
- var dx:Number = obj2._x - x1;
- var dy:Number = obj2._y - y1;
- var dist:Number = Math.sqrt(dx * dx+dy * dy);
- var radius:Number = 10;
- var nX:Number = dx / dist;
- var nY:Number = dy / dist;
- var midpointX:Number = (x1 + obj2._x)/2;
- var midpointY:Number = (y1 + obj2._y)/2;
- obj1._x = midpointX-nX*radius;
- obj1._y = midpointY-nY*radius;
- obj2._x = midpointX+nX*radius;
- obj2._y = midpointY+nY*radius;
- var distanceVector:Number = (obj1.xspeed - obj2.xspeed) * nX + (obj1.yspeed - obj2.yspeed) * nY;
- var distVectorX:Number = distanceVector * nX;
- var distVectorY:Number = distanceVector * nY;
- obj1.xspeed -= distVectorX;
- obj1.yspeed -= distVectorY;
- obj2.xspeed += distVectorX;
- obj2.yspeed += distVectorY;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement