as3 I try to make player reflect from wall by opposite degrees

67 Views Asked by At

Now my player move opposite way from the block wall except from the right side. I had trace the value of x/y modify and find that x is always positive. that make the object alway move to left. I don't understand why because it work with y. Is this bug or my code wrong very thank you.

//------------------------0
var wallAngle:Number;
wall.addEventListener(Event.ENTER_FRAME, reflect);
function reflect(e:Event):void
{
    if (mcPlayer.hitTestObject(wall)){
        wallAngle = getAngle(mcPlayer.x, mcPlayer.y, wall.x, wall.y)        
        mcPlayer.y -= 5*(Math.sin(wallAngle*(Math.PI/180)));
        mcPlayer.x -= 5*(Math.cos(wallAngle*(Math.PI/180)));
        trace("sin "+ String(Math.sin(wallAngle*(Math.PI/180))));
        trace("cos "+ String(Math.cos(wallAngle*(Math.PI/180))));
    }
}
function getAngle (x1:Number, y1:Number, x2:Number, y2:Number):Number
{
    var dx:Number = x2 - x1;
    var dy:Number = y2 - y1;
    return (Math.atan2(dy,dx));//return Redians
}
0

There are 0 best solutions below