A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: AS3 help: drag and drop game

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    3

    Unhappy AS3 help: drag and drop game

    Hello everyone

    I am making a simple drag and drop game ( dress up ) everything is going well and it works
    But now I want to exand my game with making more pages with clothing to drag.

    Here comes the problem:
    When ever I go to the second page and drag the clothing to the doll, the clothing on the doll disapears whenever I go to back to the first page.
    This is my code ( took out most clothes so its easy to see the general code )
    Actionscript Code:
    stop();
    // too page 2
       nextpage1.visible=false
       knop2.addEventListener(MouseEvent.CLICK, page2);
       function page2(e:MouseEvent):void{
       nextpage1.visible=true;
       }
       
    //too page 1
        knop1.addEventListener(MouseEvent.CLICK, hideMe);
        function hideMe(e:MouseEvent):void{
        nextpage1.visible=false;
         }
     
    //clothes page2
      //dress red//
        dress_red.visible=false
         knop2.addEventListener(MouseEvent.CLICK, showMeRed);
         function showMeRed(e:MouseEvent):void{
         dress_red.visible=true;
         }
      //hide dress
     knop1.addEventListener(MouseEvent.CLICK, hideMeRed);
        function hideMeRed(e:MouseEvent):void{
        dress_red.visible=false;
         }
    // drag codes
    dress_red.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    dress_red.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    dress_red.x = 650;
    dress_red.y = 220;
     
    top_black.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
    top_black.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
    top_black.x = 760;
    top_black.y = 400;
    function mouseDownHandler(evt:MouseEvent):void{
     var object = evt.target;
     object.startDrag();
    }

    function mouseUpHandler(evt:MouseEvent):void{
     var obj = evt.target;
     obj.stopDrag();  
    }

    I have to say, I am not very experienced with AS3
    But I really hope someone can help me with this, I have been searching for tutorials and such some time now.

  2. #2
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    The problem is that whenever you change frames, all the movie clips not present in both frames, are reset to whatever values they have in IDE. This includes "visible" property too.

    One way to avoid this would be extending same movie clip over both frames. Lets say you have movie clip "clothes" set with keyframe in frame 1 and normal frame in frame 2. In frame 1 you change parts of clothes using
    clothes.dress.visible = false

    If you leave frame 1 and want to hide clothes in frame 2, you set them all invisible
    clothes.visible = false

    All the movie clips still exist, they are simply not drawn on screen. You can now return back to frame 1 and turn them all on again
    clothes.visible = true

    and dress remains invisible like set before.


    However, this can become hassle in the end to manage all the movie clips and which frame they appear. AS3 basically has nothing to do with frames or timelines so you may as well create proper data object. The idea is that game data and visible objects are separated. Game data is persistent and is not affected by changing frames, running movie clips, tweens or whatever happens on screen.

    Create data object:
    frame1clothes = new Object();
    frame1clothes.dress_red = true;
    frame1clothes.top_black = true;

    Notice that names of data objects are same as names of movie clips.

    Now when you change visibility of movie clips, also change data object:
    dress_red.visible=false;
    frame1clothes.dress_red = dress_red.visible;

    If you change frames and return to frame 1, you want to set back the clothes same way they were when you left the frame:
    function setFrame1Clothes():void{
    for (var str in frame1clothes){
    this[str].visible = frame1clothes[str];
    }
    }

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Oh I think I understand what you mean, thank you very much!
    I do have a question, in:
    frame1clothes = new Object();

    Is " frame1clothes" a movieclip that has the clothing inside of it?

    Because when I add the code, I get an error which says: 1120: Access of undefined property Frame1clothes.

  4. #4
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    No its just a variable, try with var keyword:
    var frame1clothes = new Object();

  5. #5
    Junior Member
    Join Date
    Oct 2011
    Posts
    3
    Oh yes ofcourse var, I should have thought about that, thank you!

    I tried it, but I cant get it too work, but I think is it probably because of my lack of experience and knowledge.
    I will save de code you gave me for when I understand AS3 better.
    Thank you so much for helping me I really appreciate it!

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