Forum Topic: AS3 eventlistener help

(95 views • 4 replies)

This topic is 1 page long.

<< < > >>
None

perj

Reply To Post Reply & Quote

Posted at: 10/13/09 06:50 PM

perj EVIL LEVEL 12

Sign-Up: 12/09/05

Posts: 1,552

Hello, I just simply want to create a link for a button. However, I want each eventlistener attached to a button instance to pass a different url to my gotoPage function. However, I'm just not sure how to add parameters with the event listener. Here's what I have so far, where it specifies the url within the function. I want to be able to specify the url in the eventListener:

function gotoPage(event:MouseEvent):void
{
	var target:URLRequest = new URLRequest("http://google.com");
	navigateToURL(target);
}

tour_button.addEventListener(MouseEvent.CLICK, gotoPage);

None

perj

Reply To Post Reply & Quote

Posted at: 10/13/09 08:19 PM

perj EVIL LEVEL 12

Sign-Up: 12/09/05

Posts: 1,552

hmm maybe this is more complicated than I expected it to be. I really thought this would be a simple thing - if not I feel like this is something that should be addressed. But what do I know.


None

Kirk-Cocaine

Reply To Post Reply & Quote

Posted at: 10/13/09 08:32 PM

Kirk-Cocaine EVIL LEVEL 36

Sign-Up: 08/17/03

Posts: 17,851

Right, the way I would do it would be have an array of buttons and an array of URLs. Do a for loop for each element in your button array and assign a listener to it. Then in the function, use indexOf to call the relevant element of urlArray:

var buttonArray = new Array(a,s,d);
var urlArray = new Array("a","s","d");

for (var i=0; i < buttonArray.length; i++) {
	buttonArray[i].addEventListener(MouseEvent.CLICK, clicked);
}

function clicked(e:MouseEvent):void {
	var j = buttonArray.indexOf(e.currentTarget);
	trace(urlArray[j]);
}

None

blah569

Reply To Post Reply & Quote

Posted at: 10/13/09 09:55 PM

blah569 DARK LEVEL 21

Sign-Up: 01/18/05

Posts: 2,701

At 10/13/09 08:19 PM, perj wrote: hmm maybe this is more complicated than I expected it to be. I really thought this would be a simple thing - if not I feel like this is something that should be addressed. But what do I know.

Here is another example. AS3 is really nice as it's entirely Object Orientated.

In the library, you could have a movieclip that's exported for Actionscript in the properties with a class name of "ButtonObj"

You could have an (*.as) file in the com directory in the same folder as your fla/swf.

com/MyButton.as

package com
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.net.navigateToURL;
	import flash.net.URLRequest;
	import ButtonObj;
	
	public class MyButton extends MovieClip
	{
		public var self:ButtonObj = new ButtonObj();
		//you'll need to make a reference to the stage from a document class or any other method.  Assume the name is "cur"
		
		public function MyButton(x:Number = 0, y:Number = 0, link:String)
		{
			if (x == 0 || y == 0)
			{
				self.x = cur.stageWidth / 2;
				self.y = cur.stageHeight / 2;
			}
			else
			{
				self.x = x;
				self.y = y;
			}
			cur.addChild(self);
			
			self.addEventListener(MouseEvent.MOUSE_UP, onBtnClick);
			
			function onBtnClick(evt:MouseEvent):void
			{
				navigateToURL(new URLRequest(link));
			}
		}
	}
}

then to refer to it anywhere, simply call:

var btn:MyButton = new MyButton(30, 70, "http://www.google.com");

You can create many references of MyButton without a problem. Hopefully that helped some!

PHP: Main | AS3: Main | Get Firefox | Host large files (fast and free)!
"Thank you for learning me English."

BBS Signature

None

perj

Reply To Post Reply & Quote

Posted at: 10/13/09 10:54 PM

perj EVIL LEVEL 12

Sign-Up: 12/09/05

Posts: 1,552

thanks guys


All times are Eastern Standard Time (GMT -5) | Current Time: 07:31 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!