开发者

Dragging an object in as3

开发者 https://www.devze.com 2023-02-07 05:11 出处:网络
I have an object that i want开发者_StackOverflow社区 to drag in as3 but because its an object and not a movieClip or Sprite i cant drag it. Does anyone know why this is and how i can solve it? I tried

I have an object that i want开发者_StackOverflow社区 to drag in as3 but because its an object and not a movieClip or Sprite i cant drag it. Does anyone know why this is and how i can solve it? I tried to cast it as a Sprite but it doesnt work.

var order:Sprite = e.currentTarget as Sprite;
order.startDrag();

So e.currentTarget gives me [object Order] and i cast it as a Sprite.

Anyone help how i could solve it?


You have given the answer to your own question. To use startDrag , your object must at least be a Sprite. If the Order class doesn't extend Sprite , casting the event currentTarget as Sprite won't achieve much.

You could implement your own solution, this way may be easier to debug...

    package
    {
         import flash.display.MovieClip;
         import flash.events.*;

         public class Order extends MovieClip
         {
             public function Order()
             {
                 this.addEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler );

                 if( stage != null )
                    stage.addEventListener( MouseEvent.MOUSE_DOWN , mouseDownHandler );
             }

             private function mouseDownHandler ( event:MouseEvent ):void
             {
                 if( stage != null )
                    stage.addEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
             }

             private function mouseMoveHandler ( event:MouseEvent ):void
             {
                  this.x = mouseX;
                  this.y = mouseY;
             }

             private function mouseUpHandler ( event:MouseEvent ):void
             {
                 stage.removeEventListener( MouseEvent.MOUSE_MOVE , mouseMoveHandler );
             }
         }
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消