[英]Using a For loop with an array and addEventListener
I have this map I'm creating in Flash. 我有要在Flash中创建的这张地图。 You click on a state, then you can click on an icon to view a tooltip/popup of some information.
您单击状态,然后可以单击图标以查看一些信息的工具提示/弹出窗口。 What I was trying to do was instead of creating new functions and event listeners for every different icon is to use a for loop...but it's not going so well.
我试图做的是不是为每个不同的图标创建新的函数和事件侦听器,而是使用for循环...但是效果不是很好。 I haven't touched AS in a long time so bear with me :)
我已经很长时间没有接触过AS了,所以请耐心等待:)
var ToolTipMC = map.toolTip;
ToolTipMC.alpha = 0;
var places:Array = new Array();
places = [ "map.paulsens", "map.plutonic", "map.jundee", "map.wiluna", "map.darlot", "map.lawers", "map.gwaliaDeeps", "map.sunriseDam", "map.marvelLoch" ];
function enableToolTips( event:MouseEvent ):void {
ToolTipMC.x = places[ i ].x + 10;
ToolTipMC.y = places[ i ].y - ( ToolTipMC.height - 9 );
Tweener.addTween( ToolTipMC, { y: ToolTipMC.y + 5, alpha: 1, transition: "easeInOutExpo", time: 0.3 } );
ToolTipMC.toolTipTextField.text = "It worked!";
trace( "Mouse Over" );
}
function disableToolTips( event:MouseEvent ):void {
Tweener.addTween( ToolTipMC, { alpha: 0, transition: "easeInOutExpo", time: 0.3 } );
trace( "Mouse Out" );
}
for( var i:uint = 0; i < places.length; i++ ) {
places[ i ].addEventListener( MouseEvent.MOUSE_OVER, enableToolTips );
places[ i ].addEventListener( MouseEvent.MOUSE_OUT, disableToolTips );
}
The items in the array are instance names and I'm using the Tweener class(es). 数组中的项目是实例名称,我正在使用Tweener类。
The following throws an Output error of 以下引发输出错误
TypeError: Error #1006: value is not a function
TypeError:错误#1006:value不是函数
and is stopping at the 并停在
places[ i ].addEventListener( MouseEvent.MOUSE_OVER, enableToolTips );
So from this I can gather that it's having problems parsing the array values through to the event listener, but that's as far as I got :). 因此,从中我可以得出结论,将数组值解析到事件侦听器时遇到了问题,但就我所知:)。 Could anyone please help me with my dilema?
有人可以帮我解决我的困境吗?
I see a few things that might be causing the problem: 我看到了可能导致问题的一些原因:
Here's what it might look like. 这是它的外观。
function addListeners():void { for( var i:uint = 0; i < places.length; i++ ) { (places[ i ] as IEventDispatcher).addEventListener( MouseEvent.MOUSE_OVER, enableToolTips ); (places[ i ] as IEventDispatcher).addEventListener( MouseEvent.MOUSE_OUT, disableToolTips ); } }
You'd have to convert places
to an Array of IEventDispatchers, maybe map items or whatever you're doing, some DisplayObject. 您必须将
places
转换为IEventDispatchers数组,也许是地图项或您正在执行的任何DisplayObject。
Hope that helps! 希望有帮助!
Following on from viatropos's answer - I assume you want to access the "place" with your tooltip that sent the event? 遵循viatropos的答案-我假设您想使用发送事件的工具提示访问“地点”? You can do this using
event.target
: 您可以使用
event.target
来做到这一点:
function enableToolTips( event:MouseEvent ):void {
var place:DisplayObject = DisplayObject(event.target);
ToolTipMC.x = place.x + 10;
ToolTipMC.y = place.y - ( ToolTipMC.height - 9 );
//the rest of your function...
}
(I'm also guessing that your "places" are movieclips placed on the stage - hence the cast to DisplayObject
) (我还猜测您的“位置”是放置在舞台上的电影剪辑-因此投射到
DisplayObject
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.