Actionscript 对象数组? [英] Actionscript Arrays of Objects?

查看:18
本文介绍了Actionscript 对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 TextLink 的类.文本旨在被点击,它应该调度一个事件(我还不太关心)......目前它只是打印出一条消息.该类采用 x、y 和字符串来设置文本.死简单...但它会使浏览器崩溃.

I have a class named TextLink. The text is meant to be clicked on and it should dispatch an event (which I'm not too concerned about yet)... currently it just prints out a message. The class takes an x, y, and a string to set the text. Dead simple... But it crashes the browser.

main 中的实例调用:

Instance calls in main:

package {
    import flash.display.Sprite;

    import nav.text.TextLink;

    public class test_array_of_objects extends Sprite
    {
        public function test_array_of_objects()
        {
            var ary:Array = new Array(5);
            var i:uint;
            var ty:uint;
            var tx:uint = 30;

            for(i=0; i<ary.length; i++)
            {
                ty = i * 20 + 20;   
                var tmp:TextLink = new TextLink(tx, ty, "some text" + i.toString());
                ary.push(tmp);
            }           
        }
    }
}

类:

package nav.text
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.external.ExternalInterface;

    public class TextLink extends Sprite
    {
        public var tf:TextField = new TextField();

        public function TextLink(tx:uint, ty:uint, tft:String)
        {   
            tf.text = tft;
            tf.x = tx;
            tf.y = ty;
            tf.autoSize = TextFieldAutoSize.LEFT;

            addChild(tf);
        }

        private function rollfunc(e:Event):void
        {
            ExternalInterface.call("console.log", "got a clicky");  
        }

        /*
        protected function rollfunc(e:Event):void
        {   //dispatch a custom event 
            dispatchEvent(new Event(Event.COMPLETE));   
        }
        */
    }
}

你会注意到我已经注释掉了 rollfunc 函数,因为我稍后会添加它-我想在这里做的是为正在听课的人发送一个事件,以便我可以对点击文本的事件做一些特定的事情.该实例将由 addEventListener() 调用定义.

You'll notice that I have commented out the rollfunc function because I was going to add it later- What I would like to do here is to dispatch an event for whoever is listening to the class so that I can do something specific with the event of clicking on the text. The instance would be defined by an addEventListener() call.

谢谢

推荐答案

for(i=0; i<ary.length; i++)
{
    ...
    ary.push(tmp);
}

这是一个无限循环.ary.push() 将在每次迭代中增加 ary.length 并且 i 永远无法赶上它.

This is an infinite loop. ary.push() will increase ary.length on each iteration and i will never be able to catch up to it.

我想你想要@outis 的第二个建议;即 ary[i] = tmp

I think you want @outis's second suggestion here; i.e. ary[i] = tmp

或者只是创建一个空数组并将内容推入其中.

Or just create an empty array and push things into it.

这篇关于Actionscript 对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆