开发者

Using loops, arrays and IEventDispatcher with URLRequest

I have a map. On this map I have points, and when you click on these points it goes to a URL.

The code setup is as follows:

  • Arrays to define the movieclip locations and another for the urls (this one is an associative array).

    开发者_Go百科
  • I then create a function loop to make an event listener for each point. When the point is clicked it will run another function that handles the URLs.

  • The URL function is to grab the target point that has been click, use the url array type to find the key, and then throw it in to the URLRequest.

So far I have this code:

var places:Array = new Array();
places = [
 map.paulsens, map.paraburdoo, map.plutonic, map.wiluna, map.gwalia, map.wallaby,
 map.sunrise, map.moora, map.marvel, map.flyingFox, map.ernest, map.rosebery,
 map.lyell, map.renison, kalMap.kanowna, kalMap.frogsLeg, kalMap.tindals,
 kalMap.miitel, kalMap.higginsville
];

var links:Array = new Array();
links[ "paulsens" ] = "http://mywebsite.com/";
links[ "paraburdoo" ] = "http://mywebsite.com/";
links[ "plutonic" ] = "http://mywebsite.com/";
links[ "wiluna" ] = "http://mywebsite.com/";
links[ "gwalia" ] = "http://mywebsite.com/";
links[ "wallaby" ] = "http://mywebsite.com/";
links[ "sunrise" ] = "http://mywebsite.com/";
links[ "moora" ] = "http://mywebsite.com/";
links[ "marvel" ] = "http://mywebsite.com/";
links[ "flyingFox" ] = "http://mywebsite.com/";
links[ "ernest" ] = "http://mywebsite.com/";
links[ "rosebery" ] = "http://mywebsite.com/";
links[ "lyell" ] = "http://mywebsite.com/";
links[ "renision" ] = "http://mywebsite.com/";
links[ "kanowna" ] = "http://mywebsite.com/";
links[ "frogsLeg" ] = "http://mywebsite.com/";
links[ "tindals" ] = "http://mywebsite.com/";
links[ "miitel" ] = "http://mywebsite.com/";
links[ "higginsville" ] = "http://mywebsite.com/";

function listenerLoop():void {
    var i:int;
    for( i = 0; i < places.length; i++ ) {
        ( places[ i ] as IEventDispatcher ).addEventListener( MouseEvent.CLICK, openLink );
    }
}

function openLink( event:MouseEvent ):void {
    var link:URLRequest = new URLRequest( links[ event.target ] );
    navigateToURL( link, "_blank" );
}

listenerLoop();

Now all works fine until I click on the point, giving me this error:

TypeError: Error #2007: Parameter url must be non-null.
at global/flash.net::navigateToURL()

at

navigateToURL( link, "_blank" );

It's obvious to me that the openLink(); function isn't doing what I want it to. I can kind of guess because event.target isn't being turned in to a string or not pulling the array's type?


try

var link:URLRequest = new URLRequest( links[ event.target.name ] );

and since you define strings for you iterator it may be better do define your links as an object in favor of an array


Try storing the links in a flash.utils.Dictionary object, since it uses memory references as keys, and so you can access items doing links[ event.target ].

to inizialize it, you have also to use the objects as key.

links[ map.lyell ] = "http://mywebsite.com/";
links[ map.renision ] = "http://mywebsite.com/";
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜