Need Help with a Reset Button in Flash CS4 w/ AS3
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Need Help with a Reset Button in Flash CS4 w/ AS3

  1. #1
    Junior Member
    Join Date
    Aug 2003
    Posts
    4

    Need Help with a Reset Button in Flash CS4 w/ AS3

    I am making a drag-and-drop matching activity for an instructor at our Technical College, and so far, I have nine photos on the left side of my stage, and I have it set up to drag those photos to boxes on the left side of the stage. The photos lock in place if the answer is correct, or they return to their place on the left if the answer is not correct. So far, my AS3 code looks like this (on Frame #1):

    Code:
    var objectoriginalX:Number;
    var objectoriginalY:Number;
    
    huygens_mc.buttonMode = true;
    huygens_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    huygens_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    clerk_mc.buttonMode = true;
    clerk_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    clerk_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    wankle_mc.buttonMode = true;
    wankle_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    wankle_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    hero_mc.buttonMode = true;
    hero_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    hero_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    watt_mc.buttonMode = true;
    watt_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    watt_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    lenoir_mc.buttonMode = true;
    lenoir_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    lenoir_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    otto_mc.buttonMode = true;
    otto_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    otto_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    stirling_mc.buttonMode = true;
    stirling_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    stirling_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    diesel_mc.buttonMode = true;
    diesel_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    diesel_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
    
    function pickupObject(event:MouseEvent):void {
    	event.target.startDrag(true);
    	event.target.parent.addChild(event.target);
    	objectoriginalX = event.target.x;
    	objectoriginalY = event.target.y;
    }
    
    function dropObject(event:MouseEvent):void {
    	event.target.stopDrag();
    	var matchingTargetName:String = "target" + event.target.name;
    	var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
    	if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget){
    		event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
    		event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
    		event.target.buttonMode = false;
    		event.target.x = matchingTarget.x;
    		event.target.y = matchingTarget.y;
    	} else {
    		event.target.x = objectoriginalX;
    		event.target.y = objectoriginalY;
    	}
    }
    The final thing I want to do is add a "Reset" button so that when I click on it, the photos reset to the left side of the stage. I've been looking all around the Internet, and I can't find anything that is working for me. I have a button instance on my stage, and I gave it an instance name of "reset_btn". What code do I need, and where do I insert it above?

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,517
    Replace your code with this...
    Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    
    var dragArray:Array = [huygens_mc,clerk_mc,wankle_mc,hero_mc,watt_mc,lenoir_mc,otto_mc,stirling_mc,diesel_mc];
    
    for each (var mc:MovieClip in dragArray) {
            mc.buttonMode = true;
            mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
            mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
            mc.baseX = mc.x;
            mc.baseY = mc.y;
    }
    
    reset_btn.addEventListener(MouseEvent.CLICK, doReset);
    
    function doReset(event:MouseEvent=null):void {
            for each (var mc:MovieClip in dragArray) {
                    mc.buttonMode = true;
                    mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
                    mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
                    //mc.x = mc.baseX;
                    //mc.y = mc.baseY;
                    new Tween(mc, "x", Strong.easeOut, mc.x, mc.baseX, 0.5, true);
                    new Tween(mc, "y", Strong.easeOut, mc.y, mc.baseY, 0.5, true);
            }
    }
    function pickupObject(event:MouseEvent):void {
            event.target.startDrag(true);
            event.target.parent.addChild(event.target);
    }
    
    function dropObject(event:MouseEvent):void {
            event.target.stopDrag();
            var matchingTargetName:String = "target" + event.target.name;
            var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
            if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget) {
                    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
                    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
                    event.target.buttonMode = false;
                    event.target.x = matchingTarget.x;
                    event.target.y = matchingTarget.y;
            } else {
                    event.target.x = event.target.baseX;
                    event.target.y = event.target.baseY;
            }
    }

  3. #3
    Junior Member
    Join Date
    Aug 2003
    Posts
    4
    Thanks dawsonk! I will try this out soon!

  4. #4
    Junior Member
    Join Date
    Aug 2003
    Posts
    4
    Quote Originally Posted by dawsonk View Post
    Replace your code with this...
    Code:
    import fl.transitions.Tween;
    import fl.transitions.easing.*;
    
    var dragArray:Array = [huygens_mc,clerk_mc,wankle_mc,hero_mc,watt_mc,lenoir_mc,otto_mc,stirling_mc,diesel_mc];
    
    for each (var mc:MovieClip in dragArray) {
            mc.buttonMode = true;
            mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
            mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
            mc.baseX = mc.x;
            mc.baseY = mc.y;
    }
    
    reset_btn.addEventListener(MouseEvent.CLICK, doReset);
    
    function doReset(event:MouseEvent=null):void {
            for each (var mc:MovieClip in dragArray) {
                    mc.buttonMode = true;
                    mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
                    mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);
                    //mc.x = mc.baseX;
                    //mc.y = mc.baseY;
                    new Tween(mc, "x", Strong.easeOut, mc.x, mc.baseX, 0.5, true);
                    new Tween(mc, "y", Strong.easeOut, mc.y, mc.baseY, 0.5, true);
            }
    }
    function pickupObject(event:MouseEvent):void {
            event.target.startDrag(true);
            event.target.parent.addChild(event.target);
    }
    
    function dropObject(event:MouseEvent):void {
            event.target.stopDrag();
            var matchingTargetName:String = "target" + event.target.name;
            var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
            if (event.target.dropTarget != null && event.target.dropTarget.parent == matchingTarget) {
                    event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
                    event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
                    event.target.buttonMode = false;
                    event.target.x = matchingTarget.x;
                    event.target.y = matchingTarget.y;
            } else {
                    event.target.x = event.target.baseX;
                    event.target.y = event.target.baseY;
            }
    }
    Thanks again, dawsonk! This code works perfectly for what I need. I'm not real great with code, and I had gotten some of it from my original post from tutorials. So, what was I doing wrong? Looks like the code you gave me is quite a bit "cleaner" than what I had.

  5. #5
    :
    Join Date
    Dec 2002
    Posts
    3,517
    In order to be able to put them back, needed to save all the starting positions, not just the one that was currently being used.

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

Poll by Flashkit.com