views:

187

answers:

2

I have three Flash adverts that need to act as a direct link to the advertiser's website. I can't get it to work! I am putting in the code, directly into the advert and the cursor will now come up over the advert, but it won't click.

+1  A: 

I'm going to assume you're using ActionScript 3. If not, you could just change your project's settings to ActionScript 3, if the banners have no other code in them.

Go to the first keyframe and write the following code:

stage.addEventListener(MouseEvent.CLICK, onClick);

function onClick(evt:MouseEvent):void {
    var req:URLRequest = new URLRequest('http://www.stackoverflow.com');
    navigateToURL(req);
}

The mouse cursor won't change into a hand, though. You'd have to create a transparent movieclip on the topmost layer and then, instead of the previous code, write:

 myMC.addEventListener(MouseEvent.CLICK, onClick);
 myMC.buttonMode = true;

function onClick(evt:MouseEvent):void {
    var req:URLRequest = new URLRequest('http://www.stackoverflow.com');
    navigateToURL(req);
}

Your moviclip has to be the height and width of the stage and have a rectangle with any color and 0% opacity.

PS: obviously, replace http://www.stackoverflow.com with the website you want your banner to point to.

evilpenguin
+1  A: 

if you'd like a simple copy and paste reusable AS3 solution then paste this code into the .fla then re-publish:


// change this to the url you want to go to, and use "_self" or "_blank"
// to open the url when clicked in the same window, or in a new one
makeAllClickable("http://www.stackoverflow.com", "_blank");

var url: String;
var window: String;
function makeAllClickable(_url: String, _window: String) : void
{
    url = _url;
    _window = _window;
    var clickArea: Sprite = new Sprite();
    clickArea.graphics.beginFill(0,0);
    clickArea.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
    clickArea.buttonMode = clickArea.useHandCursor = true;
    stage.addChild(clickArea);
    clickArea.addEventListener(MouseEvent.CLICK, gotoURL);
}
function gotoURL(event: MouseEvent) :  void
{
    navigateToURL(new URLRequest(url), window);
}
Bryan Grezeszak