views:

40

answers:

3

I'm trying to name the instances of MovieClips that I dynamically load.

I tried doing this:

comp = new Comp(); // and also tried doing this--> var comp:MovieClip = new Comp();
comp.name = "comp"; // comp is the name I want the instance to be

BUT in the OUTPUT Window:

ReferenceError: Error #1056: Cannot create property comp on ToggleTest.
 at flash.display::Sprite/constructChildren()
 at flash.display::Sprite()
 at flash.display::MovieClip()
 at ToggleTest()

This is the code that I have in my ActionScript file: package {

    import flash.display.MovieClip;
     import flash.events.MouseEvent;
     import flash.events.Event;

 public class ToggleTest extends MovieClip
 {
  var comp:MovieClip;

  public function ToggleTest()
  {

   comp = new Comp();
   //var comp:MovieClip = new Comp();
   comp.name = "comp";

   comp.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
   comp.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
   comp.addEventListener(MouseEvent.CLICK, toggleClick);
   comp.bstate = 0;
   comp.buttonMode = true;

   // Add Movie Clip "buttons" to stage
   stage.addChild(comp);

   comp.x = 120;
   comp.y = 130;


   // calls function frameloop
   stage.addEventListener(Event.ENTER_FRAME, frameloop);

  }

  // function rolloverToggle
  function rolloverToggle(e:MouseEvent) {
   if (e.currentTarget.currentFrame == 1)
    e.currentTarget.gotoAndStop(2);
   if (e.currentTarget.currentFrame == 3)
    e.currentTarget.gotoAndStop(4);
  }

  // function rolloutToggle
  function rolloutToggle(e:MouseEvent) {
   if (e.currentTarget.currentFrame == 2)
    e.currentTarget.gotoAndStop(1);
   if (e.currentTarget.currentFrame == 4)
    e.currentTarget.gotoAndStop(3);
  }

  // function toggleClick
  function toggleClick(e:MouseEvent) {


   var houseArray:Object = {lightA: 1, 
         lightB: 1, 
         lightC: 1,
         lightD: 1,
         lightE: 1,
         comp: 2,
         tv: 3,
         stove: 4,
         laundry: 5};

   var powerData:int = houseArray[e.currentTarget.name.toLowerCase()];


  trace("movieClip Instance Name = " + e.currentTarget);
  trace(powerData);
  trace(houseArray[0]);

   // how to find out which object selected

   if (e.currentTarget.currentFrame == 2)
   {
    e.currentTarget.gotoAndStop(3);
    e.currentTarget.bstate = 1;
   }

   if (e.currentTarget.currentFrame == 4)
   {
    e.currentTarget.gotoAndStop(1);
    e.currentTarget.bstate = 0;
   }
  }

  function frameloop(e:Event)
  {
   var outtext:String="";
   outtext += comp.bstate +", ";
   outfield.text = outtext;

  }


 } 
}
A: 

Test the same code with the following:

    comp = new MovieClip();

if it works, you may need to import the Comp class

PatrickS
A: 

Here are some potential problems:

1) I don't see an import statement for the Comp class anywhere. Is this a Linkage name for a MovieClip in flash? Otherwise, you will need to import it.

2) Since you're in a class, when you make properties for a class (basically variables you declare by the class's definition, like your comp variable) you need to use the public or private keyword. So instead of

var comp:MovieClip;

you'd put:

private var comp:MovieClip;

3) The name property of DisplayObjects (comp.name) is READ-ONLY. You are not allowed to set it.

4) You cant access the stage until the ToggleTest has been added to it. So add a listener for ADDED_TO_STAGE in the constructor and then add comp to the stage. So like:

public function ToggleTest()
{
    addEventListener(Event.ADDED_TO_STAGE, _added);
}

private function _added(e:Event):void
{
    removeEventListener(Event.ADDED_TO_STAGE, _added);

    comp = new Comp();

    comp.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
    comp.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
    comp.addEventListener(MouseEvent.CLICK, toggleClick);
    comp.bstate = 0;
    comp.buttonMode = true;
    comp.x = 120;
    comp.y = 130;

    // Add Movie Clip "buttons" to stage
    stage.addChild(comp);

    // calls function frameloop
    stage.addEventListener(Event.ENTER_FRAME, frameloop);
}
Fox
@Fox "The name property of DisplayObjects (comp.name) is READ-ONLY. You are not allowed to set it."This is not true , please check the docs , the name property is read-write!"you need to use the public or private keyword"false again , if you don't define an access modifier , it defaults to internal
PatrickS
in the linkage of the comp MovieClip--I exported the Comp class.
jc70
@PatrickS Try setting the name attribute yourself, it doesn't let you. Also you're right you don't have to use the public a private keyword, but its a best practice and in my mind should always be used.
Fox
@crewof1 Alright, then you should be able to use the Comp class just fine. Did you make the changes like i suggested in #3? Also, I think this older post may have a solution for you: http://stackoverflow.com/questions/1734169/flash-as3-referenceerror-error-1056-cannot-create-property
Fox
@PatrickS I created a new file using the same code, just like you did--and I received the same message in the output window: movieClip Instance Name = [object Comp]2undefined (I haven't changed the trace statement yet to be trace(houseArray[e.currentTarget.name]);) There must be something in my other file that is creating the error that I was seeing.
jc70
A: 

I have taken your code and try to replicate the error but everything worked fine! Here's what I did:

  • Created a new AS3 project in Flash CS5
  • Created a Document Class named ToggleTest.as and copied your code into it
  • Created a MovieClip named Comp and checked "Export for Actionscript" with a class name of Comp
  • Created a TextField with an instance name of outfield

I didn't get any errors , the Comp instance was added to stage and outfield displayed some text

After clicking on Comp, I got the following trace statements:

movieClip Instance Name = [object Comp]
2
undefined

undefined was return because of this

trace(houseArray[0]);

houseArray is an object , so I changed the trace statement to this

trace(houseArray[e.currentTarget.name]);

so after clicking on Comp:

movieClip Instance Name = [object Comp]
2
2

Now, I don't understand the Error you're getting. If the Comp class couldn't be found you would get the error "Call to a possibly undefined method Comp".

It seems the problem is elsewhere , try to do the same as above , start a new project with a minimal setup , you shouldn't be able to reproduce the error, then add new elements until the error comes back

PatrickS