Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 code help with drag and drop quiz

New Here ,
Jul 27, 2009 Jul 27, 2009

I have a flash drag and drop quiz that i would like to add different images to correspond to the correct veribage when the users have dropped the folder.

I have the text working ok when the user drops the folder (i.e. "yes, that is correct!", I'm sorry, that's not correct.", "Congrats, you're finished!")

I don't know where to start and I was wondering if it was possible to add an image from the library to the stage when the corresponding text is displayed on the stage?

I have the AS3 code below that I'm using.

Thanks for help in advance!

1.gif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var startX:Number;

var startY:Number;

var counter:Number = 0;

folder_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);

folder_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

folder2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);

folder2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

folder3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);

folder3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void {

            event.target.startDrag(true);

            reply_txt.text = "";

            reply_txt2.text = "";

            event.target.parent.addChild(event.target);

            startX = event.target.x;

            startY = event.target.y;

}

function dropIt(event:MouseEvent):void {

            event.target.stopDrag();

            var myTargetName:String = "target" + event.target.name;

            var myTarget:DisplayObject = getChildByName(myTargetName);

            if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){

                        reply_txt.text = "Yes, that's correct!";

                        reply_txt2.text = "Donec sed fermentum lorem. Suspendisse potenti. Pellentesque hendrerit tempor nunc, vitae semper risus gravida vel. Nam eu lorem sed nulla lacinia commodo vitae vel ante. Cras ac nibh dolor.";

                        event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);

                        event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);

                        event.target.buttonMode = false;

                        event.target.x = myTarget.x;

                        event.target.y = myTarget.y;

                        counter++;

            } else {

                        reply_txt.text = "I'm sorry, that's not correct.";  

                        reply_txt2.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce a massa tellus, ut euismod urna. Phasellus bibendum pretium porta. Praesent sed elit vel odio ultrices dignissim in in tortor."

                        reply_txt3.text = "Try Again";

                        event.target.x = startX;

                        event.target.y = startY;

            }

            if(counter == 3

               ){

        reply_txt.text = "Congrats, you're finished!";

                        reply_txt2.text = "";

    }

}

folder_mc.buttonMode = true;

folder2_mc.buttonMode = true;

folder3_mc.buttonMode = true;

TOPICS
ActionScript
3.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jul 27, 2009 Jul 27, 2009

yes, to add a library object to the stage using actionscript assign the object a class, say ImageClass.  you can then use the new constructor to create an instance and then add it to the display list:

var image:ImageClass=new ImageClass();

addChild(image);

Translate
Community Expert ,
Jul 27, 2009 Jul 27, 2009

yes, to add a library object to the stage using actionscript assign the object a class, say ImageClass.  you can then use the new constructor to create an instance and then add it to the display list:

var image:ImageClass=new ImageClass();

addChild(image);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 27, 2009 Jul 27, 2009

Thanks a lot for the help!

Another question... how do i get the image to remove when a different image needs to be displayed

Example: The user drags the wrong folder to the circle... the sad face image appears, but when they drag the correct folder to the cirlce, they sad face disappear then the happy face appears.

Thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 27, 2009 Jul 27, 2009

use:

removeChild(image);

to remove image from the display list.  if it's no longer needed you should also null it so it can be gc'd:

image=null;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2009 Jul 28, 2009

Thank you so much for your help,

Last question, I promise, how do you place that image at a certain location on the stage?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2009 Jul 28, 2009

assign its x,y properties:

image.x=22;

image.y=44;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 28, 2009 Jul 28, 2009

Thank you so much!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 28, 2009 Jul 28, 2009

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jul 29, 2009 Jul 29, 2009

Now I'm getting the following error and I dont know why???

TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at interaction_drag_n_drop_fla::MainTimeline/dropIt()

maybe I'm placing the removeChild(image) and the image = null; in the wrong place?

var startX:Number;
var startY:Number;
var counter:Number = 0;
var currAnswerContinue:DisplayObject;
var currAnswerGreat:DisplayObject;
var currAnswerTry:DisplayObject;

folder_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
folder_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
folder2_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
folder2_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
folder3_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
folder3_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);

function pickUp(event:MouseEvent):void {
event.target.startDrag(true);
//reply_txt.text = "";
//reply_txt2.text = "";
event.target.parent.addChild(event.target);
startX = event.target.x;
startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
 
  //reply_txt.text = "Yes, that's correct!";
  //reply_txt2.text = "Donec sed fermentum lorem. Suspendisse potenti. Pellentesque hendrerit tempor nunc, vitae semper risus gravida vel. Nam eu lorem sed nulla lacinia commodo vitae vel ante. Cras ac nibh dolor.";
     
  event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
  event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
  event.target.buttonMode = false;
  event.target.x = myTarget.x;
  event.target.y = myTarget.y;
  counter++;
 
  var currAnswerData1:AnswerImageDataContinue = new AnswerImageDataContinue(0,0);
  currAnswerContinue = new Bitmap(currAnswerData1);
  addChild(currAnswerContinue);
  currAnswerContinue.x = 20;
  currAnswerContinue.y = 365;
 
  //currAnswerContinue = null;
      
 
} else {
  //reply_txt.text = "I'm sorry, that's not correct.";
  //reply_txt2.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce a massa tellus, ut euismod urna. Phasellus bibendum pretium porta. Praesent sed elit vel odio ultrices dignissim in in tortor."
  //reply_txt3.text = "Try Again";
 
  event.target.x = startX;
  event.target.y = startY;
 
  var currAnswerData2:AnswerImageDataTry = new AnswerImageDataTry(0,0);
  currAnswerTry = new Bitmap(currAnswerData2);
  addChild(currAnswerTry);
  currAnswerTry.x = 20;
  currAnswerTry.y = 402;
 
}
if(counter == 3
    ){
  
     removeChild(currAnswerContinue);
  removeChild(currAnswerTry);

  //reply_txt.text = "Congrats, you're finished!";
  //reply_txt2.text = "";
 
  var currAnswerData3:AnswerImageDataGreat = new AnswerImageDataGreat(0,0);
  currAnswerGreat = new Bitmap(currAnswerData3);
  addChild(currAnswerGreat);
  currAnswerGreat.x = 20;
  currAnswerGreat.y = 365;
    }
}

folder_mc.buttonMode = true;
folder2_mc.buttonMode = true;
folder3_mc.buttonMode = true;

//removeChild(currAnswerContinue);
//removeChild(currAnswerTry);
//removeChild(currAnswerGreat);

//currAnswerContinue = null;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 29, 2009 Jul 29, 2009
LATEST

you're trying to reference an object that doesn't exist when that removeChild() code executes.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines