开发者

How to target movie clip on the stage from doc class AS3?

I am making dynamic drag and drop game. I have a class for the dragged items containing the drag drop code.

My problem is I can't call/access the movie clips I've already put on the stage in my hit test statement.

Here is my code and target1_mc & target2_mc are the existing movie clips on the stage:

package {
 import flash.display.MovieClip;
 import flash.events.MouseEvent;
 import flash.text.TextField;




 public class recipeMC extends MovieClip {



  private var startX:Number;
  private var startY:Number;
  private var counter:Number=0;




  public function recipeMC() {
   this.mouseChildren = false;
   this.buttonMode = true;
   this.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
   this.addEventListener(MouseEvent.MOUSE_UP, dropIt);
  }
  private function pickUp(event:MouseEvent):void {
   this.startDrag(true);
   this.parent.addChild(this);
   startX = this.x;
   startY = this.y;

  }
  private function dropIt(event:MouseEvent):void {
   this.stopDrag();



   *****if (this.hitTestObject(target1_mc)
   ||开发者_StackOverflow中文版this.hitTestObject(target2_mc) )***** {
    this.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
    this.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
    this.buttonMode = false;
    this.x = myTarget.x;
    this.y = myTarget.y;
    counter++;
   } else {
    //reply_txt.text = "Try Again!";
    this.x = startX;
    this.y = startY;
   }

  }
 }
}


You need to go to publish settings->flash->settings(button) and select "Automatically declare stage instances." That should do the trick.

EDIT

Try this inside your recieptMovieClip class:

MovieClip(root).target1_mc
MovieClip(root).target2_mc

Because this is annoying to type you can store MovieClip(root) in a variable. It's important to note that the root property doesn't exist unless your movieClip is on the display list. So until your class has been added wth addChild() this will not work.

An alternative method is to use a document class and store a reference to the main timeline in a static variable. I wrote a blog post about that once. Have a look

If you use the technique in that blogpost you could access the main timeline from anywhere like this:

Main.display.target1_mc

Hope that helps. I can upload an example or two if you need them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜