A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Drag and Drop

  1. #1
    Member
    Join Date
    Sep 2001
    Posts
    37

    Drag and Drop

    Hi,

    I have 4 shapes which fit into 4 boxes and would like to modify my code so when a shape is placed in the correct box a movie clip appears e.g. if a triangle is placed into a triangle shape the movieclip for triangle appears. if the shape is placed into the wrong box a message appears saying try again. can somebody please help me with my flash file.
    Attached Files Attached Files

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Need to give your message movie clips instance names ('msgGoodsquare_mc', 'msgBadsquare_mc', msgGoodflower_mc', etc) and under properties, color effect set the alpha to zero and set the display blending to normal.

    Code:
    var startX:Number;
    var startY:Number;
    var counter:Number=0;
    
    var objs:Array=[square_mc,triangle_mc,flower_mc,pentagon_mc];
    var info:Dictionary = new Dictionary();
    var msgs:Dictionary = new Dictionary();
    
    for (var i:* in objs) {
    	objs[i].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    	objs[i].addEventListener(MouseEvent.MOUSE_UP, dropIt);
    	info[objs[i]]={t:getChildByName("target"+objs[i].name),sX:objs[i].x,sY:objs[i].y};
    	msgs[getChildByName("target"+objs[i].name)]={good:getChildByName("msgGood"+objs[i].name),bad:getChildByName("msgBad"+objs[i].name)};
    }
    
    function pickUp(event:MouseEvent):void {
    	event.target.startDrag(true);
    	reply_txt.text="";
    	event.target.parent.addChild(event.target);
    	startX=event.target.x;
    	startY=event.target.y;
    	for (var i:* in msgs) {
    		msgs[i].bad.alpha=0;
    	}
    }
    
    function dropIt(e:MouseEvent):void {
    	e.target.stopDrag();
    	if (e.target.dropTarget!=null) {
    		if (e.target.dropTarget.parent==info[e.target].t) {
    			msgs[e.target.dropTarget.parent].good.alpha=1;
    			reply_txt.text="Good Job!";
    			e.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    			e.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    			e.target.buttonMode=false;
    			e.target.x=info[e.target].t.x;
    			e.target.y=info[e.target].t.y;
    			counter++;
    		} else {
    			if (msgs[e.target.dropTarget.parent]) {
    				msgs[e.target.dropTarget.parent].bad.alpha=1;
    			}
    			reply_txt.text="Missed, Try Again!";
    			e.target.x=info[e.target].sX;
    			e.target.y=info[e.target].sY;
    		}
    	} else {
    		reply_txt.text="Try Again!";
    		e.target.x=info[e.target].sX;
    		e.target.y=info[e.target].sY;
    	}
    	if (counter==4) {
    		reply_txt.text="Congrats, you're finished!";
    	}
    }
    
    square_mc.buttonMode=true;
    flower_mc.buttonMode=true;
    triangle_mc.buttonMode=true;
    pentagon_mc.buttonMode=true;
    HTH
    Last edited by dawsonk; 09-16-2010 at 11:56 AM.

  3. #3
    Member
    Join Date
    Sep 2001
    Posts
    37
    Thank you very very much your changes made the fla work exactly how I wanted it to.
    Last edited by tupac4eva; 09-15-2010 at 07:45 AM.

  4. #4
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Place a reset button on the stage, instance name 'btnReset' and add the following code after the existing code.
    Code:
    btnReset.addEventListener(MouseEvent.CLICK, doReset, false, 0 ,true);
    
    function doReset(e:Event):void {
    	for (var i:* in msgs) {
    		msgs[i].bad.alpha = 0;
    		msgs[i].good.alpha = 0;
    	}
    	for (var j:* in objs) {
    		objs[j].x = info[objs[j]].sX;
    		objs[j].y = info[objs[j]].sY;
    		objs[j].removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    		objs[j].removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    		objs[j].addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    		objs[j].addEventListener(MouseEvent.MOUSE_UP, dropIt);
    	}
    	counter = 0;
    	reply_txt.text = "Put the shapes away!";
    }

  5. #5
    Member
    Join Date
    Sep 2001
    Posts
    37
    Hi,

    That worked perfectly. Thank you.
    Is there a way I can make the name of the shape remain under the shape after it has correctly been placed with the shape. So if the triangle is placed into the triangle box the word 'triangle' stays under the shape at al times.

    at the end when all are done she should be able to see all the names of the shapes underneath the shapes. not sure how to adjust the code to do this.

    thanks

  6. #6
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Confused, name does stay when object put into object box.
    Last edited by dawsonk; 09-20-2010 at 05:38 PM.

  7. #7
    Member
    Join Date
    Sep 2001
    Posts
    37
    Sorry yes, my mistake I am nearly there now. I have made a second and third level so it gets harder with more complex shapes and placed these on frame 2 and 3 of the timeline. If she puts all the shapes into the correct boxes I would like her to go to and stop on frame 2 where there are more shapes and a different board. I duplicated the code from frame 1 into frame 2 and I am getting;

    Scene 1, Layer 'Actions', Frame 2, Line 48 1151: A conflict exists with definition i in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 10 1151: A conflict exists with definition msgs in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 9 1151: A conflict exists with definition info in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 8 1151: A conflict exists with definition objs in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 6 1151: A conflict exists with definition filt_shadow in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 5 1151: A conflict exists with definition filt in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 4 1151: A conflict exists with definition counter in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 3 1151: A conflict exists with definition startY in namespace internal.
    Scene 1, Layer 'Actions', Frame 2, Line 2 1151: A conflict exists with definition startX in namespace internal.

    I have tried to google the answer for this but I really don't know where to start. Is this an easy fix? Thanks
    Attached Files Attached Files
    Last edited by tupac4eva; 09-17-2010 at 08:41 AM.

  8. #8
    :
    Join Date
    Dec 2002
    Posts
    3,518
    See if this will work...
    Last edited by dawsonk; 11-08-2010 at 12:07 PM.

  9. #9
    Member
    Join Date
    Sep 2001
    Posts
    37
    Hi,

    The issue I am having is with the following section of code. The dynamic text box displayed "Missed, Try Again!" perfectly every time but the movie clips displaying the try again error e.g. msgBadsquare_mc, msgBadtriangle_mc, etc, doe not seem to appear consistently when the wrong item is placed within a box.



    } else {
    if (msgs[e.target.dropTarget.parent]) {
    msgs[e.target.dropTarget.parent].bad.alpha=1;
    }
    reply_txt.text="Missed, Try Again!";
    nosound.gotoAndPlay (1);
    e.target.filters = [];
    Tweener.addTween(e.target,{x:info[e.target].sX,y:info[e.target].sY,alpha:1,time:.5,transition:"easeOutElastic"});
    Last edited by tupac4eva; 09-25-2010 at 01:21 PM.

  10. #10
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    function dropIt(e:MouseEvent):void {
    	e.currentTarget.stopDrag();
    	if (e.currentTarget.dropTarget!=null) {
    		if (e.currentTarget.hitTestObject(info[e.currentTarget].t)) {
    			msgs[info[e.currentTarget].t].good.alpha=1;
    			reply_txt.text="Good Job!";
    			e.currentTarget.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    			e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    			e.currentTarget.buttonMode=false;
    			e.currentTarget.x=info[e.currentTarget].t.x;
    			e.currentTarget.y=info[e.currentTarget].t.y;
    			counter++;
    		} else {
    			for (var j:* in info) {
    				if (e.currentTarget.hitTestObject(info[j].t)) {
    					msgs[info[j].t].bad.alpha=1;
    					break;
    				}
    			}
    			reply_txt.text="Missed, Try Again!";
    			e.currentTarget.x=info[e.currentTarget].sX;
    			e.currentTarget.y=info[e.currentTarget].sY;
    		}
    	} else {
    		reply_txt.text="Try Again!";
    		e.currentTarget.x=info[e.currentTarget].sX;
    		e.currentTarget.y=info[e.currentTarget].sY;
    	}
    	if (counter==objs.length) {
    		welldone_mc.gotoAndPlay(2);
    	}
    }

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center