7 Replies Latest reply on Jun 15, 2011 5:59 AM by TheScarecrow

    User Draw Line

    TheScarecrow Level 1

      I am constructing a matching game where the user will draw a line between two items.  Has anyone done this or know of an example code wise that would help me.  I am not sure how to constrain the line to just between the items or how to know if the correct two items have been connected. Thanks in advance.

        • 1. Re: User Draw Line
          kglad Adobe Community Professional & MVP

          do you know how to draw a line using actionscript?


          • 2. Re: User Draw Line
            TheScarecrow Level 1

            Yes with the lineTo.   That is what I am wanting to do .. all actionscript with the line draw.

            • 3. Re: User Draw Line
              kglad Adobe Community Professional & MVP

              if each of your to-be-connected objects is a movieclip, you can assign a property (eg, match) to each that stores its match.  then when your line draw from a starting movieclip is complete you can check for a mouse hittest with that movieclip's match property.

              • 4. Re: User Draw Line
                TheScarecrow Level 1

                Yes .... that is my thought so each line would create its own movie clip?

                • 5. Re: User Draw Line
                  kglad Adobe Community Professional & MVP

                  no.

                   

                  mc1.match=mcsomething;

                  mc2.match=mcwhatever;

                  .

                  .

                   

                  var tl:MovieClip=this;

                  for(var i:Number=0;i<mcNumber;i++){

                  tl["mc"+i].onPress=function(){

                  this.moveTo(this._x,this._y);

                  this.onEnterFrame=function(){

                  with(this){

                  lineTo(this._x,this._y);

                  moveTo(this._x,this._y);

                  }

                  }

                  tl["mc"+i].onRelease=function(){

                  delete this.onEnterFrame;

                  if(this.match.hitTest(tl._xmouse,tl._ymouse)){

                  // matched

                  } else {

                  this.clear();

                  }

                  }

                  1 person found this helpful
                  • 6. Re: User Draw Line
                    TheScarecrow Level 1

                    can you explain some of that code I am not sure how that works?

                    • 7. Re: User Draw Line
                      kglad Adobe Community Professional & MVP

                      // each mc has a match property identifying the piece to which it should be matched

                      mc0.match=mcsomething;

                      mc1.match=mcwhatever;

                      .

                      .

                       

                      // tl references the current timeline

                      var tl:MovieClip=this;

                      // loop through all movieclips mc0,mc1,..mc[mcNumber-1]

                      for(var i:Number=0;i<mcNumber;i++){

                      // define onPress for each movieclip to start line draw

                      tl["mc"+i].onPress=function(){

                      // move line start to this movieclip

                      this.moveTo(this._x,this._y);

                      // start loop to update line as mouse is moved

                      this.onEnterFrame=function(){

                      with(this){

                      lineTo(this._x,this._y);

                      moveTo(this._x,this._y);

                      }

                      }

                      // define onRelease to stop drawing line and determine if line extends to matching mc

                      tl["mc"+i].onRelease=function(){

                      // terminate enterframe loop

                      delete this.onEnterFrame;

                      // check for matching mc

                      if(this.match.hitTest(tl._xmouse,tl._ymouse)){

                      // matched - do something?

                      } else {

                      // not matched - clear the line per your design specs

                      this.clear();

                      }

                      }

                      1 person found this helpful