A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Initial X and Y Help ^^

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    Question Initial X and Y Help ^^

    Well, basically I have a problem regarding X and Y of multiple objects. Repetition isn't good.


    I have this on First Frame:
    Actionscript Code:
    dict[box1_mc] = flower_mc;
    dict[box2_mc] = cater_mc;
    dict[box3_mc] = bird_mc;

    dropTargets.push(box1_mc, box2_mc, box3_mc);

    frame = 4;
    maxCount = 3;

    button_btn.visible = false;
    reset_btn.visible = false;

    reset_btn.addEventListener(MouseEvent.CLICK, Reset);

    dict[box1_mc].initial_x = dict[box1_mc].x;
    dict[box1_mc].initial_y = dict[box1_mc].y;
    dict[box2_mc].initial_x = dict[box2_mc].x;
    dict[box2_mc].initial_y = dict[box2_mc].y;
    dict[box3_mc].initial_x = dict[box3_mc].x;
    dict[box3_mc].initial_y = dict[box3_mc].y;


    function Reset(evt:MouseEvent):void {
        dict[box1_mc].x = dict[box1_mc].initial_x;
        dict[box1_mc].y = dict[box1_mc].initial_y;
        dict[box2_mc].x = dict[box2_mc].initial_x;
        dict[box2_mc].y = dict[box2_mc].initial_y;
        dict[box3_mc].x = dict[box3_mc].initial_x;
        dict[box3_mc].y = dict[box3_mc].initial_y;
       
        droppedTargets.splice(0);
       
        flower_mc.alpha = 1;
        cater_mc.alpha = 1;
        bird_mc.alpha = 1;
       
        for each (var item in dict)
        item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

        max = 0;
        hits = 0;
       
        resetCounter = resetCounter + 1;
       
        reset_btn.visible = false;
        button_btn.visible = false;
       
        if (resetCounter == 3) {resetChances = false;} else {resetChances = true;}
       
       
    }

    Second Frame:

    Actionscript Code:
    dict[box1_mc] = nuts_mc;
    dict[box2_mc] = squirrel_mc;
    dict[box3_mc] = snake_mc;
    dict[box4_mc] = eagle_mc;

    maxCount = 4;
    dropTargets.push(box1_mc, box2_mc, box3_mc, box4_mc);

    button_btn.visible = false;
    reset_btn.visible = false;

    bird_mc.visible = false;
    cater_mc.visible = false;
    flower_mc.visible = false;

    reset_btn.removeEventListener(MouseEvent.CLICK, Reset);
    reset_btn.addEventListener(MouseEvent.CLICK, Reset2);

    dict[box1_mc].initial_x = dict[box1_mc].x;
    dict[box1_mc].initial_y = dict[box1_mc].y;
    dict[box2_mc].initial_x = dict[box2_mc].x;
    dict[box2_mc].initial_y = dict[box2_mc].y;
    dict[box3_mc].initial_x = dict[box3_mc].x;
    dict[box3_mc].initial_y = dict[box3_mc].y;
    dict[box4_mc].initial_x = dict[box4_mc].x;
    dict[box4_mc].initial_y = dict[box4_mc].y;

    function Reset2(evt:MouseEvent):void {
        dict[box1_mc].x = dict[box1_mc].initial_x;
        dict[box1_mc].y = dict[box1_mc].initial_y;
        dict[box2_mc].x = dict[box2_mc].initial_x;
        dict[box2_mc].y = dict[box2_mc].initial_y;
        dict[box3_mc].x = dict[box3_mc].initial_x;
        dict[box3_mc].y = dict[box3_mc].initial_y;
        dict[box4_mc].x = dict[box4_mc].initial_x;
        dict[box4_mc].y = dict[box4_mc].initial_y;

        droppedTargets.splice(0)
       
        eagle_mc.alpha = 1;
        nuts_mc.alpha = 1;
        squirrel_mc.alpha = 1;
        snake_mc.alpha = 1;
       
        for each (var item in dict)
        item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

        max = 0;
        hits = 0;
       
        resetCounter = resetCounter + 1;
       
        reset_btn.visible = false;
        button_btn.visible = false;
       
    if (resetCounter == 3) {resetChances = false;} else {resetChances = true;}
    }



    as You can See. That Dict[box] is giving me a problem on setting up X and Y. as i always put it on every frame instead of External AS. I wonder if someone can help me.

    I don't know if Array can be helped cause I tried but I failed, or I just Don't Know how to do it. will someone Please help me? thanks ^^

  2. #2
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    blah... frames---

    My advice is, never use them unless you really need to.


    hmm, quick question- are flower_mc, cater_mc, bird_mc, nuts_mc, squirrel_mc, snake_mc,eagle_mc, all just movieclips (what class are they)?

    If I were you, (and all of the above are just mc's), then I would just create a new class that extends MovieClip. That new class would have the following methods:

    Actionscript Code:
    /** This variable holds the Starting/Saved .x value */
    public var X:int = 0;
    /** This variable holds the Starting/Saved .y value */
    public var Y:int = 0;
    /** Use to set the Starting Location of this MovieClip. Use 'resetLocation' to reset back to this starting location.
    @param x int Starting x Locaiton of this MovieClip
    @param y int Starting y Location of this MovieClip
    */

    public function setStartingXY(x:int,y:int):void{
        this.X = x;
        this.x = x;
        this.Y = y;
        this.y = y;
    }

    /** Resets this movieclip back to its original location */
    public function resetLocation():void{
        this.x = this.X;
        this.y = this.Y;
    }

    Then you just use it and extend that like your normal movieclip class. Now you will have the added bonus that you can use the following:

    Actionscript Code:
    flower_mc.setStartingXY(box1_mc.x,box1_mc.y);

    //Then later when you want to reset its location:
    flower_mc.resetLocation();

    Thats one idea at least.

    Hope that is helpful! if you need any more help (or my ramblings dont make any sense), give a holler and I can work you up a working example.

    Best of luck!
    ~gk. >^_^<
    Last edited by guardiankitty; 12-30-2011 at 07:41 AM.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Hmmm, I have bunch of frames almost 20 frames that has different draggable objects that need this code. I have an External Class. that has public functions.
    and yes they are all MovieClips

    dict[box1_mc] = flower_mc;

    basically this is a new Dictionary. the box1_mc is the correct box that the flower_mc that should be dragged to. If not, it's a mistake.

    Hmm, It will still give me a lot of repetitive Codes. I appreciate your help.

    But I wonder if such thing as "Each Item in array could be extracted to set each XandY" or.. a variable that will produce a result like this:

    Actionscript Code:
    for each (var item in dict)
    item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);

    I already tried using this:

    Actionscript Code:
    for each (var startingXY in dict)
    startingXY.initialX = startingXY.x
    startingXY.initialY = startingXY.y


    function resetButton(evt:MouseEvent):void {
       for each (var startingXY in dict)
       startingXY.x = startingXY.initialX
       startingXY.y = startingXY.initialY
    }

    the Only MovieClip that is being returned to its initial XandY is the Last MovieClip in that Frame.

    I can't move that Reset FUnction on the External AS because every frame has different objects/MovieClip to be reset. Here's my Public External AS:

    Actionscript Code:
    package Scripts
    {
        import flash.events.MouseEvent;
        import flash.utils.Dictionary;
        import flash.display.MovieClip;
        import flash.display.Stage;
        import flash.display.*;
        import flash.events.*;
        import fl.transitions.Tween;
        import fl.transitions.easing.*;
       
       
       
        dynamic public class PublicClass extends MovieClip {
            public var dict:Dictionary = new Dictionary();
            public var correct = 0;
            public var error = 0;
            public var failed_attempt = 0;
            public var hits:int = 0;
            public var max:int = 0;
            public var maxCount:int = 0;
            public var frame:int = 0;
            public var droppedTargets:Array = new Array();
            public var resetCounter = 0;
            public var dropTargets:Array = new Array();
            public var stageSelect:String = "StageNumber";
            var resetChances = true;

            public function PublicClass()
            {

            }


    //Listeners **************
           
            public function mouseDownHandler(evt:MouseEvent):void {
                var object = evt.target;
               
                parent.addChild( object );
                object.startDrag();
                stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
            }

            public function mouseUpHandler(evt:MouseEvent):void {
                var obj = evt.target;
                var targetTrap = false;
               
                obj.stopDrag();
                trace (max, "= max", maxCount, " = maxcount ");
               
                for (var i:int = 0; i < dropTargets.length; i++) {
                   
                    var target:MovieClip = dropTargets[i] as MovieClip;

                    if (target.hitTestObject(obj) == true) {
                       
                        if (alreadyBeenDraggedCheck(target) == false) {
                           
                            droppedTargets.push(target);
                            targetTrap = true;
                            test_match(target,obj);
                           
                        }
                    }
               
                    stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
               
                }
               
                if (targetTrap == false) {
           
                obj.x = obj.initial_x;
                obj.y = obj.initial_y;
           
                }
            }
           
           
           

           
            function test_match(target,obj) {
                if (dict[target] == obj)
                {
                    hits = hits+1;
                    max = max+1;
           
                    trace("Yes ! You got one !");
           
                    obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                    obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
                   
                    centerMe (obj, target);
           
                } else {
                    max = max+1;
                    failed_attempt = failed_attempt + 1;
                    trace("Missed :(");
           
                    obj.removeEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
                    obj.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);

                    centerMe (obj, target);
                }
               
                if(max == maxCount)
                {
                    button_btn.visible = true;
                   
                }

                if(max > 0)
                {
                    if(resetChances == true){
                    reset_btn.visible = true;
                    }
                }
       
            }

            function centerMe(dragged:MovieClip,droppedOn:MovieClip):void {
       
                var startX = dragged.x;
                var startY = dragged.y;
       
                var destX = dragged.x = droppedOn.x + ((droppedOn.width - dragged.width)/2);
                var destY = dragged.y = droppedOn.y + ((droppedOn.height - dragged.height)/2);
               
                if (stageSelect == "Stage3")
                {
                    dragged.x = droppedOn.x
                    dragged.y = droppedOn.y
                } else {
                var myXtween:Tween = new Tween(dragged, "x", Elastic.easeOut, startX, destX, 1, true);
                var myYtween:Tween = new Tween(dragged, "y", Elastic.easeOut, startY, destY, 1, true);
                dragged.alpha = 0.5;
                }
            }
           
            function alreadyBeenDraggedCheck(dropCandidate:MovieClip):Boolean {

                var alreadyDraggedOn = false;
       
                for (var i=0;i<droppedTargets.length;i++) {
           
                    if (droppedTargets[i] == dropCandidate) {

                        alreadyDraggedOn = true;
                    }
                }
                return alreadyDraggedOn;
            }

    // Public Buttons **************

            function Next(evt:MouseEvent):void {
                if (hits == maxCount)
                {
                        correct = correct + 1;
                        trace("Made it !!");
                    }
                    else
                    {
                        trace("You Made an Error!!");
                        error = error + 1;
                }
           
                gotoAndStop(frame = frame + 1);
                   
                dropTargets.splice(0,max);
                   
                max = 0;
                hits = 0;
                   
                droppedTargets.splice(0);
               
                for each (var item in dict)
                    item.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
            }
           
            function done(evt:MouseEvent):void {
                if (hits == maxCount)
                {
                        correct = correct + 1;
                        trace("Made it !!");
                    }
                    else
                    {
                        trace("You Made an Error!!");
                        error = error + 1;
                }
               
                dropTargets.splice(0);
                droppedTargets.splice(0);
               
                max = 0;
                hits = 0;
               
                if(stageSelect == "Stage1") {
                    plankton2_mc.visible = false;
                    smallfish4_mc.visible = false;
                    tuna2_mc.visible = false;
                    calm_mc.visible = false;
                    human_mc.visible = false;
                } else if(stageSelect == "Stage2") {
                    humanBaby_mc.visible = false;
                    humanChild_mc.visible = false;
                    adolescent_mc.visible = false;
                    humanMen_mc.visible = false;
                    humanWomen_mc.visible = false;
                    humanOld_mc.visible = false;
                }
                gotoAndStop(29);

            }

        }

    }

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    Thanks but I already got my Self Solve it. THanks for the help sir ^^

    Here's what i've Done, instead of using for each(var item in dict) i use this:

    Actionscript Code:
    for each(var box in dropTargets) { // I used a variable box to establish each elements in dropTargets Array
      dict[box].initial_x = dict[box].x; //instead of repetitive dict things, I came up to use this
      dict[box].initial_x = dict[box].y;
    }



    function Reset(evt:MouseEvent):void {
       for each(var box in dropTargets) {
          dict[box].x = dict[box].initial_x;
          dict[box].y = dict[box].initial_y;
       }
    }


    There you go. Thanks again sir ^^

  5. #5
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    I am glad to hear that you got it, Thanks for sharing the solution, too!

    Happy Coding!

Tags for this Thread

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