Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 2 of 2

Thread: What is the best way to do this drag and drop

Hybrid View

  1. #1
    286
    posts
    Registered User

    What is the best way to do this drag and drop

    Hello,

    I am trying to create a drag/drop where when you drop the answer onto the question it senses the target and traces the name of the target. What am I doing wrong?


    Code:
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.events.Event;
    import flash.utils.Dictionary;
    var refDict:Dictionary = new Dictionary;
     /************* Set up the questions with text from the XML file. *********************/
     for(var i:int = 0; i < xmlFile.questions.question.length(); i++)
     { 
      var quesText:TextField = new TextField();
      quesText.defaultTextFormat=formatNormal;
      quesText.multiline=true;
      quesText.selectable=false;
      quesText.mouseEnabled = false;
      quesText.wordWrap=true;
      quesText.width=400;
      quesText.x = 20;
      quesText.y = 20 + (i * 60);
      quesText.autoSize=TextFieldAutoSize.LEFT;
      quesText.name = String(i); // Not sure why I did this.
      quesText.text = xmlFile.questions.question[i].content;
      stage.addChild(quesText);
     }
     
     /************* Set up the draggers with text from the XML file. *********************/
     for(var n:int = 0; n < xmlFile.draggers.answer.length(); n++)
     {
      var ansBacking:mcDragger = new mcDragger();
      ansBacking.x = 650;
      ansBacking.y = 30 + (n * 70);
      ansBacking.buttonMode = true;
      ansBacking.txtAnswer.text = xmlFile.draggers.answer[n].content;
      ansBacking.mouseChildren = false;
      ansBacking.name = xmlFile.draggers.answer[n].content;
      ansBacking.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
      stage.addChild(ansBacking);
     }
     
    
    function startDragging(evt:MouseEvent):void
    {
     evt.target.removeEventListener(MouseEvent.MOUSE_DOWN, startDragging);
     evt.target.addEventListener(Event.ENTER_FRAME, checkHitTest);
     origLocatX = evt.target.x;
     origLocatY = evt.target.y;
     evt.target.startDrag(false);
     evt.target.parent.addChild(evt.target);
     evt.target.addEventListener(MouseEvent.MOUSE_UP, onDrop);
    }
    function onDrop(evt:MouseEvent):void
    {
     
     evt.target.removeEventListener(MouseEvent.MOUSE_UP, onDrop);
     evt.target.addEventListener(MouseEvent.MOUSE_DOWN, startDragging);
     if(xmlFile.draggers.@multiUse == true)
     {
      evt.target.x = origLocatX;
      evt.target.y = origLocatY;
     }
     else
     {
      stage.removeChild(DisplayObject(evt.target));
     }
     evt.target.stopDrag();
     
     if(evt.target.dropTarget != null)
     {
      evt.target.dropTarget.text += evt.target.txtAnswer.text;
     }
     else
     {
      //stage.removeChild(evt.target.parent);
     }
    }
    function checkHitTest(evt:Event):void
    {
     /*if(evt.currentTarget.hitTestObject(quesText))
     {}*/
     if (evt.currentTarget.dropTarget != null)
     {
      trace(evt.currentTarget.dropTarget.parent);
     }
    }

  2. #2

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