命名动态加载的 MovieClip 实例
我正在尝试命名动态加载的 MovieClips 实例。
我尝试这样做:
comp = new Comp(); // 并且也尝试这样做--> var comp:MovieClip = new Comp();
comp.name = "comp"; // comp 是我希望实例
在输出窗口中使用的名称:
ReferenceError: Error #1056: Cannot create property comp on ToggleTest.
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at ToggleTest()
这是我在 ActionScript 文件中的代码: 包裹 {
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'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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已获取您的代码并尝试复制该错误,但一切正常!这就是我所做的:
我没有收到任何错误,Comp 实例已添加到舞台中,outfield 显示了一些文本
单击 Comp 后,我得到以下跟踪语句:
undefined was return 因为 this
houseArray 是一个对象,所以我更改了 跟踪语句
单击 Comp 之后的
:现在,我不明白您收到的错误。如果找不到 Comp 类,您将收到错误“调用可能未定义的方法 Comp”。
看来问题出在其他地方,尝试执行与上面相同的操作,以最小的设置启动一个新项目,您不应该能够重现错误,然后添加新元素,直到错误再次出现
I have taken your code and try to replicate the error but everything worked fine! Here's what I did:
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:
undefined was return because of this
houseArray is an object , so I changed the trace statement to this
so after clicking on Comp:
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
使用以下代码测试相同的代码:
如果有效,您可能需要导入 Comp 类
Test the same code with the following:
if it works, you may need to import the Comp class
以下是一些潜在的问题:
1) 我在任何地方都没有看到 Comp 类的 import 语句。这是 Flash 中 MovieClip 的链接名称吗?否则,您将需要导入它。
2)由于您在一个类中,因此当您为类创建属性(基本上是通过类定义声明的变量,例如您的 comp 变量)时,您需要使用 public 或 private 关键字。因此,
您可以输入:
3) DisplayObjects (comp.name) 的 name 属性是只读的。不允许您设置它。
4) 在将 ToggleTest 添加到阶段之前,您无法访问该阶段。因此,在构造函数中添加 ADDED_TO_STAGE 的侦听器,然后将 comp 添加到舞台。所以就像:
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
you'd put:
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: