0

2つの長方形と指定されたインスタンス名w1w2そしてどのようにして2つの長方形を互いに非常に接近させて通過させることができますか?

http://forums.adobe.com/thread/1139552

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));
}
4

0 に答える 0