Naming Instances of MovieClips loaded dynamically
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 beBUT 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;
}
}
}
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
Test the same code with the following:
comp = new MovieClip();
if it works, you may need to import the Comp class
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);
}
精彩评论