命名动态加载的 MovieClip 实例

发布于 2024-09-15 10:42:15 字数 2555 浏览 4 评论 0原文

我正在尝试命名动态加载的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

相权↑美人 2024-09-22 10:42:15

我已获取您的代码并尝试复制该错误,但一切正常!这就是我所做的:

  • 在 Flash CS5 中创建一个新的 AS3 项目
  • 创建一个名为 ToggleTest.as 的文档类并将代码复制到其中
  • 创建一个名为 Comp 的 MovieClip 并选中类名为 Comp 的“Export for Actionscript”
  • 创建一个带有outfield 的实例名称

我没有收到任何错误,Comp 实例已添加到舞台中,outfield 显示了一些文本

单击 Comp 后,我得到以下跟踪语句:

movieClip Instance Name = [object Comp]
2
undefined

undefined was return 因为 this

trace(houseArray[0]);

houseArray 是一个对象,所以我更改了 跟踪语句

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

单击 Comp 之后的

movieClip Instance Name = [object Comp]
2
2

:现在,我不明白您收到的错误。如果找不到 Comp 类,您将收到错误“调用可能未定义的方法 Comp”。

看来问题出在其他地方,尝试执行与上面相同的操作,以最小的设置启动一个新项目,您不应该能够重现错误,然后添加新元素,直到错误再次出现

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

吻风 2024-09-22 10:42:15

使用以下代码测试相同的代码:

    comp = new MovieClip();

如果有效,您可能需要导入 Comp 类

Test the same code with the following:

    comp = new MovieClip();

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

温柔一刀 2024-09-22 10:42:15

以下是一些潜在的问题:

1) 我在任何地方都没有看到 Comp 类的 import 语句。这是 Flash 中 MovieClip 的链接名称吗?否则,您将需要导入它。

2)由于您在一个类中,因此当您为类创建属性(基本上是通过类定义声明的变量,例如您的 comp 变量)时,您需要使用 public 或 private 关键字。因此,

var comp:MovieClip;

您可以输入:

private var comp:MovieClip;

3) DisplayObjects (comp.name) 的 name 属性是只读的。不允许您设置它。

4) 在将 ToggleTest 添加到阶段之前,您无法访问该阶段。因此,在构造函数中添加 ADDED_TO_STAGE 的侦听器,然后将 comp 添加到舞台。所以就像:

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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文