物体的动作阵列? [英] Actionscript Arrays of Objects?

查看:132
本文介绍了物体的动作阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为文本链接的类。文本,就是要点击,它应该派遣一个事件(这我不是太在意过)......现在它只是打印出一条消息。 这个类需要一个X,Y,和一个字符串来设置文本。死的简单... 但它崩溃的浏览器。

在主实例调用:

 包{
    进口flash.display.Sprite;

    进口nav.text.TextLink;

    公共类test_array_of_objects扩展Sprite
    {
    公共职能test_array_of_objects()
    {
    VAR元:阵列=新阵列(5);
    变种我:UINT;
    变种TY:UINT;
    VAR TX:UINT = 30;

    对于(i = 0; I< ary.length;我++)
    {
    TY = I * 20 + 20;
    VAR TMP:文本链接=新的文本链接(TX,TY,一些文本+ i.toString());
    ary.push(TMP);
    }
    }
    }
}
 

类:

 包nav.text
{
    进口flash.display.Sprite;
    进口对象类型:flash.events.Event;
    进口flash.events.MouseEvent;
    进口API元素flash.text.TextField;
    进口flash.text.TextFieldAutoSize;
    进口的flash.external.ExternalInterface;

    公共类文本链接扩展Sprite
    {
    公共变种TF:文本字段=新的TextField();

    公共职能文本链接(TX:UINT,TY:UINT,TFT:字符串)
    {
    tf.text = TFT;
    tf.x = TX;
    tf.y = TY;
    tf.autoSize = TextFieldAutoSize.LEFT;

    的addChild(TF);
    }

    私有函数rollfunc(五:事件):无效
    {
    ExternalInterface.call(执行console.log,得到了clicky);
    }

    / *
    保护功能rollfunc(五:事件):无效
    {//调度自定义事件
    则dispatchEvent(新的事件(Event.COMPLETE));
    }
    * /
    }
}
 

您会发现,我已注释掉rollfunc功能,因为我打算把它添加later- 我想在这里做的是调度的事件谁是听类的,这样我可以做具体的使用点击文本的情况下的东西。该实例会通过的addEventListener()的调用来定义。

感谢

解决方案

 为(i = 0; I< ary.length;我++)
{
    ...
    ary.push(TMP);
}
 

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

我想你想@这里outis的第二个建议;即元[I] = tmp目录

或者只是创建一个空的数组,推动东西进去。

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.

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

Class:

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

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.

Thanks

解决方案

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

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.

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.

这篇关于物体的动作阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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