如何从闪存中的对象(关联数组)中获取值,并将该值重新分配给新变量?

发布于 2024-09-14 17:42:40 字数 694 浏览 5 评论 0原文

我的闪光灯舞台上有多个切换按钮。这些切换按钮是影片剪辑。如果用户单击其中一个 MovieClip,我希望能够找到一种方法来遍历下面的对象 - 根据用户单击的内容获取适当的值 - 并将该值存储在名为 var powerData 的新值中然后将其传递给函数来执行计算。

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

例如: 如果用户单击“Comp”MovieClip 那么——> var 电源数据 = 2; (因为在 houseArray:Object--> comp: "2" 中)

I have multiple toggle buttons on my flash stage. These toggle buttons are MovieClips. If a user clicks on one of the MovieClips, I want to be able to find a way to go through the Object below--get the appropriate value based on what the User clicked--and store that value in a new value called var powerData which will then be passed to a function to perform calculations.

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

For example:
If the user clicked on "Comp" MovieClip
then--> var powerData = 2; (because in houseArray:Object--> comp: "2")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

陌伤浅笑 2024-09-21 17:42:40

您需要根据 houseArray 对象的属性来命名您的 MovieClips 实例。在您的示例中, Comp 实例应命名为 "comp" ,电视应命名为 "tv" 等等...

  //Could also be , private var comp:Comp;
  private var comp:MovieClip;

  private function init():void
  {
      comp = new Comp();
      comp.name = "comp";
   }

  private function mouseClickHandler(event:MouseEvent):void
  {
      var buttonName:String = event.currentTarget.name;

      //since the houseArray property value is a String, the result needs 
      //to be typed as int( or Number, depending on context )
      var powerData:int = int( houseArray[buttonName] );

      performCalculations(powerData);
  }

  private function performCalculations(value:int):void
  {
      //your code here
  }

我认为您有充分的理由将 houseArray 中的属性命名为字符串...

You will need to name your MovieClips instances after the properties of the houseArray object. In your example, the Comp instance should be named "comp" , the TV "tv" and so on...

  //Could also be , private var comp:Comp;
  private var comp:MovieClip;

  private function init():void
  {
      comp = new Comp();
      comp.name = "comp";
   }

  private function mouseClickHandler(event:MouseEvent):void
  {
      var buttonName:String = event.currentTarget.name;

      //since the houseArray property value is a String, the result needs 
      //to be typed as int( or Number, depending on context )
      var powerData:int = int( houseArray[buttonName] );

      performCalculations(powerData);
  }

  private function performCalculations(value:int):void
  {
      //your code here
  }

I assume you have good reasons to have the properties in houseArray as Strings...

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