The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 6 of 6

Thread: Drag and Drop with line

  1. #1

    Drag and Drop with line

    Hello friends i make a drag and drop function by help of you...

    i want draw a line from drag to drop.

    i have done this with AS2.0

    but i couldn't did in AS3...
    can anyone help me...


    var score:Number = 0;
    var startX:Number;
    var startY:Number;
    var startLineX:Number;
    var startLineY:Number;


    circleMc.addEventListener(MouseEvent.MOUSE_DOWN, dragThis);
    circleMc.addEventListener(MouseEvent.MOUSE_UP, dropThis);

    squreMc.addEventListener(MouseEvent.MOUSE_DOWN, dragThis);
    squreMc.addEventListener(MouseEvent.MOUSE_UP, dropThis);

    errowMc.addEventListener(MouseEvent.MOUSE_DOWN, dragThis);
    errowMc.addEventListener(MouseEvent.MOUSE_UP, dropThis);

    polygonMc.addEventListener(MouseEvent.MOUSE_DOWN, dragThis);
    polygonMc.addEventListener(MouseEvent.MOUSE_UP, dropThis);

    function dragThis(event:MouseEvent):void
    {
    event.target.startDrag(true);
    startX = event.target.x;
    startY = event.target.y;
    }

    function dropThis(event:MouseEvent):void
    {
    event.target.stopDrag();
    var myTargetName:String = "target_" + event.target.name;
    var myTargetisplayObject = getChildByName(myTargetName);
    trace(myTarget);
    if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget)
    {
    resultTxt.text = "Good Job";
    event.target.removeEventListener(MouseEvent.MOUSE_ DOWN, dragThis);
    event.target.removeEventListener(MouseEvent.MOUSE_ DOWN, dropThis);
    event.target.buttonMode = false;
    event.target.x = myTarget.x;
    event.target.y = myTarget.y;
    startLineX = myTarget.x;
    startLineY = myTarget.y;
    trace("Going on....");
    score ++;

    } else
    {
    resultTxt.text = "Try Again !";
    event.target.x = startX;
    event.target.y = startY;
    }

    }


    circleMc.buttonMode = true;
    squreMc.buttonMode = true;
    errowMc.buttonMode = true;
    polygonMc.buttonMode = true;

  2. #2
    238
    posts
    Registered User
    what's the problem?

  3. #3
    A line should be start from drag point to drop piont.
    I am also sending the both file.
    Attached Files Attached Files

  4. #4
    Everything looks fine and working fine. The difference between the AS2 and AS3 file is that you have no method set to DRAW a line in the AS3 project.


    In AS2 you have:
    function onEnterFrame(hi):Void {

    clear();
    lineStyle(5,0,50);
    moveTo(spotMc1._x,spotMc1._y);
    lineTo(dragMc1._x,dragMc1._y);

    moveTo(spotMc2._x,spotMc2._y);
    lineTo(dragMc2._x,dragMc2._y);

    moveTo(spotMc3._x,spotMc3._y);
    lineTo(dragMc3._x,dragMc3._y);

    moveTo(spotMc4._x,spotMc4._y);
    lineTo(dragMc4._x,dragMc4._y);
    }

    It does not exist anywhere in your AS3 project so you should not get a line. If you revisit your AS2 file and comment out the line lineStyle(5,0,50); like this //lineStyle(5,0,50); and test your movie you will get the same results you are getting in your AS3 project.

    I am not too familiar with the methods used in AS3 but I am sure it is just as simple to implement by referencing the drawing methods in help (F1)

    I do know that you will need to add.

    stage.addEventListener(Event.ENTER_FRAME, yourHandler);

    function yourHandler(event:Event):void {

    the line drawing methods here...

    }

    You're almost there...just a little snag. If you did indeed code it in AS2 then you should know that you cannot have a line drawn without actually creating a method to do so naturally. Ha-ha, you will figure it out...I'm sure of this


    Cheers.
    Programmers: When someone asks to use your machine, tell them it has to be in "safe mode" by pressing the power button and holding it for 5 seconds. Ok it's now safe to let them use it

  5. #5
    238
    posts
    Registered User
    ok i did quite a bit of an overhaul but i got it to work how i think you were shooting for.

    Code:
    var score:Number = 0;
    var startX:Number;
    var startY:Number;
    var startLineX:Number;
    var startLineY:Number;
    
    setup(circleMc);
    setup(squreMc);
    setup(errowMc);
    setup(polygonMc);
    
    
    
    function setup(shape:MovieClip) : void{
        shape.addEventListener(MouseEvent.MOUSE_DOWN, dragThis);
        shape.addEventListener(MouseEvent.MOUSE_UP, dropThis);
        shape.addChildAt(new Sprite(), 1);
        shape.startX = shape.x;
        shape.startY = shape.y;
        shape.mouseChildren = false;
    }
    function dragThis(event:MouseEvent):void
    {
        event.target.startDrag(true);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
    }
    function onMove(m:MouseEvent) : void {
        drawLine(MovieClip(m.target));
    }
    function drawLine(item:MovieClip) : void {
        var line:Sprite = Sprite(item.getChildAt(1));
        var localStart:Point = item.globalToLocal(new Point(item.startX, item.startY));
        var localItem:Point = line.globalToLocal(new Point(item.x, item.y));
        with(line.graphics){
            clear();
            moveTo(localStart.x, localStart.y);
            lineStyle(2, 0);
            lineTo(localItem.x, localItem.y);
        }
            
    }    
    
    function dropThis(event:MouseEvent):void
    {
        event.target.stopDrag();
        stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);
        var myTargetName:String = "target_" + event.target.name;
        var myTarget:DisplayObject = getChildByName(myTargetName);
        trace(myTarget);
        if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
            resultTxt.text = "Good Job";
            event.target.removeEventListener(MouseEvent.MOUSE_DOWN, dragThis);
            event.target.removeEventListener(MouseEvent.MOUSE_DOWN, dropThis);
            event.target.buttonMode = false;
            event.target.x = myTarget.x;
            event.target.y = myTarget.y;
            trace("Going on....");
            score ++;
        
        } else{
            resultTxt.text = "Try Again !";
            event.target.x = event.target.startX;
            event.target.y = event.target.startY;
            event.target.getChildAt(1).graphics.clear();
        }
    }
    
    
    circleMc.buttonMode = true;
    squreMc.buttonMode = true;
    errowMc.buttonMode = true;
    polygonMc.buttonMode = true;

  6. #6
    Hi Sebrofm and Navee,
    Thanks a lot..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012