A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: [RESOLVED] Accessing duplicated movieClip "sister"

  1. #1
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    resolved [RESOLVED] Accessing duplicated movieClip "sister"

    Going from AC2 to AC3..
    I have made a script that creates a dot (myTag) and a text label (nameLabel) when a button is pressed. Then the user is able to drag and drop the dots. This is what my script does so far.

    I want the text label to be hidden when dragging and displayed at the place where the dot is when user stops dragging. I'm not sure what is the best way to do this.

    Here is my code:

    Code:
    var tagArray:Array = [];
    var labelArray:Array = [];
    var nameCounter:int = 0
    addLabelKnapp.addEventListener(MouseEvent.MOUSE_DOWN, addLabel);
    function addLabel(event:MouseEvent):void {
    	var startx:Number = Math.random()*100;
    	var starty:Number = Math.random()*100;
    	var myTag:MovieClip = new tag();
    	myTag.x = startx;
    	myTag.y = starty;
    	myTag.name = "tag" + nameCounter;
    	myTag.ref = nameCounter;
    	myTag.addEventListener(MouseEvent.MOUSE_DOWN, dragTag);
    	myTag.addEventListener(MouseEvent.MOUSE_UP, stopTagDrag);
    	addChild(myTag);
    	tagArray.push(myTag);
    	// Add text label:
    	var nameLabel:TextField = new TextField();
    	nameLabel.type = TextFieldType.INPUT;
    	nameLabel.border = true;
    	nameLabel.x = startx+15;
    	nameLabel.y = starty;
    	nameLabel.width = 50;
    	nameLabel.height = 16;
    	nameLabel.multiline = false;
    	nameLabel.wordWrap = false;
    	nameLabel.name = "nameLabel"+nameCounter;
    	addChild(nameLabel);
    	labelArray.push(nameLabel);
    }
    function dragTag(event:MouseEvent):void {
    	event.currentTarget.startDrag();
    }
    function stopTagDrag(event:MouseEvent):void {
    	event.currentTarget.stopDrag();
    }

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If you have not already, make a Tag class (not "tag", classes should start with a capital letter). That class should do everything you're currently doing with tag (I suspect you just have a visual asset and no code, but if you do have code, just rename "tag" to "Tag" and follow along).
    Add a label property to Tag. In Tag's constructor, create the label and initialize it much like you are doing already, except make it a child of the Tag, and give it some constant offset coordinates. Do NOT set the name property. It's just useless.

    I don't see a need for the ref property in Tag. You're probably using it to get the label for the tag, and by making the label a property of the tag, the ref is unnecessary.

    Add hideLabel and showLabel methods to Tag. Call them in dragTag and stopTagDrag.

    Code:
    public class Tag extends Sprite {
      private var label:TextField;
    
      public function Tag(){
         label = new TextField();
         label.type = TextFieldType.INPUT;
         label.border = true;
         label.x = 15;
         label.width = 50;
         label.height = 16;
         label.multiline = false;
         label.wordWrap = false;
         addChild(label);
      }
    
      public hideLabel():void{
         removeChild(label);
      }
    
      public showLabel():void{
         addChild(label);
      }
    
      public setLabelText(t:String):void{
        label.text = t;
      }
    }
    Code:
    var tagArray:Array = [];
    addLabelKnapp.addEventListener(MouseEvent.MOUSE_DOWN, addLabel);
    function addLabel(event:MouseEvent):void {
    	var startx:Number = Math.random()*100;
    	var starty:Number = Math.random()*100;
    	//type the variable as its actual type
    	var myTag:Tag = new Tag();
    	myTag.x = startx;
    	myTag.y = starty;
    	//Don't set the name and don't use the name.
    	//myTag.name = "tag" + nameCounter;
    	//probably don't need the ref either.
    	//myTag.ref = nameCounter;
    	myTag.addEventListener(MouseEvent.MOUSE_DOWN, dragTag);
    	myTag.addEventListener(MouseEvent.MOUSE_UP, stopTagDrag);
    	addChild(myTag);
    	tagArray.push(myTag);
    }
    function dragTag(event:MouseEvent):void {
    	var t:Tag = Tag(event.currentTarget);
    	t.hideLabel();
    	t.startDrag();
    }
    function stopTagDrag(event:MouseEvent):void {
    	var t:Tag = Tag(event.currentTarget);
    	t.showLabel();
    	t.stopDrag();
    }

  3. #3
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Thanks for reply, I'm not sure what you mean by visual asset and I'm not sure where to place the code as I now recive "The public attribute can only be used inside a package" error?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    By visual asset, I mean a symbol in your library associated with the Tag class.

    And you need to make a class file. Make a file called Tag.as, and put the Tag code in there. Actually, you'll need to surround it with a package statement. Copy and paste this into Tag.as:
    Code:
    package {
    
    public class Tag extends Sprite {
      private var label:TextField;
    
      public function Tag(){
         label = new TextField();
         label.type = TextFieldType.INPUT;
         label.border = true;
         label.x = 15;
         label.width = 50;
         label.height = 16;
         label.multiline = false;
         label.wordWrap = false;
         addChild(label);
      }
    
      public hideLabel():void{
         removeChild(label);
      }
    
      public showLabel():void{
         addChild(label);
      }
    
      public setLabelText(t:String):void{
        label.text = t;
      }
    }
    
    }
    And save that in the same directory as your FLA.

  5. #5
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    The dot is an visual asset and the thing I that the user should be able to initiate a drag with.

    I revice an error on the code on public class Tag extends Sprite {: "The definition of base class Sprite was not found."

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You need to import it.
    Code:
    import flash.display.Sprite;
    Let's see, you'll probably also need to import TextField and TextFieldType, so:
    Code:
    package {
    
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    
    public class Tag extends Sprite {
    etc.

  7. #7
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Only the dot should be able to initiate a dragging action, not the text field. The dot is an visual asset, how do I access this in the external Tag.ac file??

  8. #8
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Okay, found it. Then the dragging and dropping.

  9. #9
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171
    Is there a way of accessing the dot within the Tag? I have converted the dot inside the Tag to a movie clip, then named it's instance name to dot.

    Then I'm trying to access this using:
    Code:
    myTag.dot.addEventListener(MouseEvent.MOUSE_DOWN, dragTag);
    myTag.dot.addEventListener(MouseEvent.MOUSE_UP, stopTagDrag);
    Then I get this error when dragging/dropping:
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@2db520b1 to Tag.
    at makePicture_fla::MainTimeline/dragTag()
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@2db520b1 to Tag.
    at makePicture_fla::MainTimeline/stopTagDrag()

    -----------------------------------------
    The whole code looks like this:

    Tag.as:
    Code:
    package {
    
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.display.MovieClip;
    
    public class Tag extends Sprite {
      private var label:TextField;
    
      public function Tag(){
         label = new TextField();
         label.type = TextFieldType.INPUT;
         label.border = true;
         label.x = 15;
         label.width = 50;
         label.height = 16;
         label.multiline = false;
         label.wordWrap = false;
         addChild(label);
    	 //myDot:MovieClip = new dot();
    	 //myDot.x = 0;
    	 //myDot.y = 0;
    	 //addChild(myDot);
      }
      
      public function hideLabel():void{
         removeChild(label);
      }
    
      public function showLabel():void{
         addChild(label);
      }
    
      public function setLabelText(t:String):void{
        label.text = t;
      }
      }
    }
    
    }
    .fla:
    Code:
    package {
    
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.display.MovieClip;
    
    public class Tag extends Sprite {
      private var label:TextField;
    
      public function Tag(){
         label = new TextField();
         label.type = TextFieldType.INPUT;
         label.border = true;
         label.x = 15;
         label.width = 50;
         label.height = 16;
         label.multiline = false;
         label.wordWrap = false;
         addChild(label);
    	 //myDot:MovieClip = new dot();
    	 //myDot.x = 0;
    	 //myDot.y = 0;
    	 //addChild(myDot);
      }
      
      public function hideLabel():void{
         removeChild(label);
      }
    
      public function showLabel():void{
         addChild(label);
      }
    
      public function setLabelText(t:String):void{
        label.text = t;
      }
      }
    }
    Last edited by somlemeg; 02-15-2012 at 08:16 PM.

  10. #10
    Senior Member somlemeg's Avatar
    Join Date
    Aug 2000
    Posts
    171

    Fixed

    I understand what was wrong now. Sorry for the inconvenience and thanks for putting me on right track.

    Code:
    var t:Tag = Tag(event.currentTarget.parent);
    Cheers
    somlemeg

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