how to make to object collide perfectly ??collision problem see my image
i want any body to test y code written below and test them as i shown in picture i want to pass the grey box and green box perfectly near thei border but it collide .
draw two rectangle and give instance name w1 and w2 then put my code to test it you will under stand my problem![]()
![]()
addEventListener(Event.ENTER_FRAME ,moveplayer);
var left,right,up,back:Boolean;
// both rectangles were given instancename as w1 and w2 and drawn on stage manually
stage.addEventListener(KeyboardEvent.KEY_DOWN ,keypress);
stage.addEventListener(KeyboardEvent.KEY_UP ,keyup);
//gray box movement key
function keypress(event:KeyboardEvent ):void
{
if (event.keyCode == 37)
{
left = true;
}
else if (event.keyCode==38)
{
up = true;
}
else if (event.keyCode==39)
{
right = true;
}
else if (event.keyCode==40)
{
back = true;
}
}
function keyup(event:KeyboardEvent ):void
{
if (event.keyCode == 37)
{
left = false;
}
else if (event.keyCode==38)
{
up = false;
}
else if (event.keyCode==39)
{
right = false;
}
else if (event.keyCode==40)
{
back = false;
}
}
//grey box movement
function moveplayer(event:Event ):void
{
//hit test
if (w1.hitTestObject(w2))
{
trace("hit");
//function call to repulse back
repulse();
}
else
{
// function call to move
go();
}
//rotation
if (left)
{
rotateit("left");
}
else if (right)
{
rotateit("right");
}
function rotateit( direc:String)
{
if (direc == "left")
{
w1.rotation -= 1;
}
else if (direc == "right")
{
w1.rotation += 1;
}
}
}
//move function
function go()
{
if (up)
{
var x2:Number = w1.x;
var y2:Number = w1.y;
//trace(x2,y2)
var speed:Number = 5;
w1.x += speed * Math.sin(w1.rotation * 2 * Math.PI / 360);
w1.y -= speed * Math.cos(w1.rotation * 2 * Math.PI / 360);
}
}
//repulsion function
function repulse()
{
var x1:Number = w1.x;
var y1:Number = w1.y;
trace(x1,y1);
var speed:Number = 5;
w1.x += - (speed * Math.sin(w1.rotation * 2 * Math.PI / 360));
w1.y -= - (speed * Math.cos(w1.rotation * 2 * Math.PI / 360));
}
