0 Replies Latest reply on Jul 21, 2006 4:09 PM by Newsgroup_User

    loop to detect which mc an image was dragged into and snap image   to it

    Level 7
      Hi All,

      I have an 'onMouseUp' code that will snap a dragged image into an mc target.
      If I hardcode the target it works fine for that one mc target, but if I
      try to put it into some kind of loop to add the code for all the mc
      targets, it won't work.

      If I hardcode it like it is, i get the trace "hello" perfectly, if I put
      it in a for loop, I get no "hello".

      I could use a 'case' and hardcode all the eventualities, but I was
      wondering how I could do it more dynamically using a loop to catch all
      the mc targets.

      Any help much apreciated,

      Jeff C.

      My code:

      // constants
      var NPICS:Number = 5; // number of pictures to load
      var THUMBW:Number = 82; // width of each thumbnail
      var THUMBH:Number = 82; // height
      var MARGIN:Number = 10; // margin between thumbnails
      _global.dragpic = ""; //name of pic being dragged

      // index into pictures array, used for loading
      var ipic:Number;
      var thispic:String;

      // set up loader, an instance of MovieClipLoader
      var loader:MovieClipLoader = new MovieClipLoader();

      // use the main timeline to listen to and respond to loader's broadcast

      var myMCL = new MovieClipLoader();//create an instance of MovieClipLoader
      function onLoadInit(_mc:MovieClip) {// this gets done when the jpg is
      completely loaded:
      _mc._width = THUMBW;
      _mc._height = THUMBH;

      _mc.onMouseDown = function(){
      if (this.hitTest(_xmouse, _ymouse,true)) {
      var nm:String = this._name
      var nextDepth:Number = getDepth();
      _global.dragpic = this._name;

      }//end of mousedown

      _mc.onMouseUp = function(_mc){
      this.stopDrag ();
      if(this.hitTest(tgt1)){// what target ** tgt1 is
      hardcoded - want it dynamic!
      this._y = tgt1._y-(this._height/2);
      this._x = tgt1._x-(this._width/2);

      }//end of mouseup

      function init() {
      //create the targets for drop
      for (var a:Number=1; a<=4; a++ ){ //loop to load 3 pictures
      //attach targetmc and give each one the name tgt1, tgt2, etc
      var mctg:MovieClip = this.attachMovie("targetmc", "tgt" + a,
      //position each one in a row -
      //(border+(half width)) + (no of image) X (image width+border)
      mctg._x=(10+(mctg._width*.5)) + ((a-1)*(10 + mctg._width));

      //****end of targets code ****

      // create holder for pictures

      // make empty movieclips in thumbs_mc for each pic to go into
      for (var i:Number = 0; i < NPICS;) {
      i++ //important to leave this out of the for()

      var mc:MovieClip = this.createEmptyMovieClip("mc"+i, i+1);
      mc._x = MARGIN + (i-1)*(MARGIN + THUMBW);
      mc._y = MARGIN;
      if (i>3){ //start a new row after 3rd mc
      mc._x = MARGIN + (i-4)*(MARGIN + THUMBW);
      mc._y = (MARGIN*2) + THUMBH;

      // set the pointer to the first jpg
      thispic = "pic"+i+".jpg";

      ipic = 0; //changes the x postion of pic
      // start loading jpgs
      loader.loadClip(thispic, "mc"+i);