Need Help with Buttons ( AS 3.0 )

11 posts

Flag Post

Ok I’m new to AS in general so bear with me…

I am creating a map, and on this map will have 5 buttons, one for each building. Now when I constructed them I put them all on the first frame all in one layer. Heres my problem, I have 2 scenarios for the buttons, a couple of building buttons I want to load and run another .swf, and then the remaining ones I want to open a web page.

In regards to the web page issue, I have this code:
myBuilding_01.addEventListener(MouseEvent.CLICK,clickHandler);
myBuilding_02.addEventListener(MouseEvent.CLICK,clickHandler);
function clickHandler(event:MouseEvent):void{
navigateToURL(new URLRequest(“http://www.purple.com”));
}

which is cool and all, it works for whatever and whenever I click on _01 and _02 it takes me to purple.com (just a test url for now), but i want 02 to go to a different url, lets say Kongregate.com for arg sake. how can I setup the buttons to do different actions? I tried some trace code and got [object myBuilding_01], now in my past programming experiences I would use the trace() ( toString() ) to determine which button and have a switch statement, but trace throws on the " [object ] " garbage. So if my toString() idea was good, how can i get rid of the extra, because i’ll chop that string down from: myBuilding01 ==> 01 which i’ll use for the switch.

Also Im not sure what the code is to load and run a swf…

Im doing all of this work in Adobe Flash CS3 Proffesional, in the Actions-Frame area ( F9 ). Thanks for the help in advance!

 
Flag Post

One option is to use event.target.name and a switch statement, e.g.

myBuilding_01.addEventListener(MouseEvent.CLICK,clickHandler); 
myBuilding_02.addEventListener(MouseEvent.CLICK,clickHandler);

function clickHandler(event:MouseEvent):void
{ 
	switch(event.target.name){
		case "myBuilding_01":
			navigateToURL(new URLRequest("http://www.purple.com")); 
			break;
		case "myBuilding_02":
			navigateToURL(new URLRequest("http://www.kongregate.com")); 
			break;
	}
}

Of course, instead of a switch statement you could use other methods to control the result, based on event.target.name.

 
Flag Post

You could have different event listeners for different objects.

Or best of all, create a new Class, say, urlButton(targetURL:String):void which holds the path, as well as a getURL method that returns the path.
Then add the listener to that, then just do

function clickHandler(event:MouseEvent):void
{
   navigateToURL(new URLRequest(event.target.getURL()));
}

I have no idea how arcaneCoder does that cool border thingie… jeez where’s the HELP section?
Guess I do now :)

 
Flag Post

guess I didn’t look hard enough for the event.target.name, and dazzer i guess your way is better OOP standards, something to look into later if have problems, but I just didnt see it neccasar to make a whole class just for a couple of buttons

Thanks!

 
Flag Post

hey guys, I too have been having problems. I copied the stuff arcane has but i get this error:

1061: Call to a possibly undefined method addEventListener through a reference with static type Class.

what does this mean?

 
Flag Post

Did you put the import statement in?

import java.events.*;

 
Flag Post

ya heres my code, maybee you can see where im messing up

import flash.display.*;
import flash.events.*;
import flash.net.URLRequest;

var gameOverLoader:Loader = new Loader();

theBomb.addEventListener(MouseEvent.CLICK, daySaved);                   // (er1) error thrown 
theBomb.addEventListener(MouseEvent.DOUBLE_CLICK, gameOver);       // (er1) error thrown 

function daySaved ( fuse: MouseEvent) : void {
 var url:String;
        trace("you saved the day!"); // Debug purposes only
        if( fuse.getTime() > 20 ){
        url = "http://www.speedyDisarm.com";
        }else{
          url = "http://www.slowDisarm.com";
        }
        navigateToURL(new URLRequest(url));
}

function gameOver ( fuse: MouseEvent) : void {
        trace("game over"); // Debug purposes only
        var urlReq:URLRequest = new URLRequest("gameOver.swf");
	loader.load(urlReq);
	addChild(loader);
}

(er1) 1061: Call to a possibly undefined method addEventListener through a reference with static type Class.

theBomb is a SimpleButton btw.

 
Flag Post

Its most likely because its not finding your button or the button is not initialized yet.

 
Flag Post

wow i just relized i put: import java.events

i think thats some clue for me to go back to java, lol

 
Flag Post

Lol Swift. Just so you know im pretty sure handles import flash.events.*; for you even if you forget to put it in

@arcaneCoder : I created theBomb button on Flash’s stage, I clicked properties and checked “export for ActionScript” and " export on first frame." I assumed that took care of the buttons initialization.

What the error seemed to me was that it isn’t picked up the fact that SimpleButton is inheriting EventDispatcher ( still talking about 1061)

 
Flag Post

I started playing around based off of what you said Alternate and I noticed then when you create a new button on the stage and give it a name, lets say BobTheButton, and you click on Export for AS it will create a class BobTheButton w/ the base class of SimpleButton.

now your code bit


BobTheButton.addEventListener(MouseEvent.CLICK, someFucntion );

If thats the case, then what you are doing is trying to tell the class to add the event listener when there is no instance of it.
Simple Fix: go to your stage and click on the button no go down to the properties pane and you will see a grayed out box that says something like this “Instance name here”, put in Bobby. Now go back to your AS code and try this:


Bobby.addEventListener(MouseEvent.CLICK, someFucntion );

And it should now work =)