Why isn't this click event being fired? - actionscript-3

Why isn't the myMouseClick event being fired?
myMC:TestMC = new TestMC();
myMC.addEventListener(MouseEvent.CLICK, myMouseClick);
addChild(myMC);
function myMouseClick(e:MouseEvent):void {
trace("clicked");
}
As far as I can tell from the tutorials I've seen, that should work. For a moment, I thought that since I was adding the event listener to myMC, I needed to have the event function inside the myMC class, but that didn't work. Just gave an error about accessing an undefined property.
If it helps any, TestMC is a seperate .as file that extends movie clip.
I'm just trying to make it so when the movie clip itself is clicked, it does something. The movie clip itself will be following the mouse.

The object I was trying to click was made up on vertical lines. Apparently the whole movie clip isn't a collider... just the pixels in it, so when I was clicking, I wouldn't hitting enough of it. Changing it to a box worked. I could probably nest it if I wanted the same design, but that's fine.

Related

How can I make a button inside a Movieclip send me to a frame in the main timeline?

I made a movie clip with an animation of a pull-down menu in which 3 buttons appear. The thing is, I'd like for those buttons to send me to an exact frame within the main timeline, and I haven't quite understood how it can be made.
The movieclip is located within a frame inside the main timeline, and its structure is kind of like this one.
The frame "Sucesos" is not within the timeline of the MovieClip, but in the Main Timeline, whereas the Button "IrSucesos1" is located inside a frame within the movie clip. The code I use is this one.
function LinkSucesos1 (event:MouseEvent):void
{ gotoAndStop("Sucesos1");
}
IrSucesos1.addEventListener(MouseEvent.CLICK, LinkSucesos1);
I don't know whether I should put the code inside the timeline of the movieclip or on the main timeline in the frame the movie clip is located, or if the code is the right one.
How do I make the buttons work by sending me to the frame that I want to get to in the main timeline and then have it stop there? What is the code, and where should I put it?
If I've understood your structure the way I think I have, the following should work. On your main timeline, add a new layer called Actions and on the frame that has the movieclip containing your buttons add a keyframe to this layer. Go to the movieclip that contains the buttons. Open the actions panel and type Stop(). Create a new layer called Button actions, click on the (blank) kayframe on this layer and add the following code:
import flash.events.MouseEvent;
my_button.addEventListener(MouseEvent.CLICK,on_click)
function on_click(e:MouseEvent)
{
MovieClip(root).gotoAndStop(5)
}
You'll need to change my_button to the button instance you'd like to assign the event listener to and 5 to the frame you want to go to on the main timeline. Test your movie - hopefully this should work for one button. For the others to work, simply give them each their own event listener and function to do what you want them to (ask if you get lost!).
stage is available anywhere where the displayObject is linked to the stage.
You can give a reference object to that control button. Your timeline object won't change so its fine.

Targeting Root from a Movieclip

Im having trouble targeting back to the main timeline from within a movie clip AS3.
I have tried the code below but still does nothing? I have labelled the frame in which I would like it to go to on the main time and added this code to the actions within in the movie clip, but when I click the button it does nothing? I have no errors come up?
Any ideas?
map_UK.addEventListener(MouseEvent.CLICK, MAIN);
function MAIN(e:MouseEvent):void
{
MovieClip(root).gotoAndPlay("MAIN");
}

As3: Button only working in certain locations?

I really don't know how to explain this problem. I'm really stumped as to what is causing it.
Here is the code:
var abutton:AButton = new AButton; //Where AButton is a button defined in my library
addChildAt(abutton, numChildren);
abutton.addEventListener(MouseEvent.CLICK, attack);
It doesn't want to work when certain movie clips are underneath it, but I don't want to make it more complicated by switching to another screen. Is it possible to make the button work with movieclips underneath?
What do you mean by "certain movieclips"? What do you mean by not working? The CLICK event isn't firing? Normally if a click isn't working on a button, it means that something else it trapping the mouse click above your button. This can be another Sprite, MovieClip or TextField.
Add a click listener to the stage, and have it print out target and currentTarget. Then, when you button doesn't work, the stage listener will still fire and you'll be able to see the object that's blocking your button.

Play movie clip instance inside of button instance

I placed a movie clip instance inside a button, and I want this movie clip to play when the button is released. I'm using this code on the frame containing the button:
function playMovie(event:MouseEvent)
{
this.theButton.theMC.gotoAndPlay(3);
}
theButton.addEventListener(MouseEvent.MOUSE_UP, playMovie);
When I try to test the flash movie, I get this message:
1119: Access of possibly undefined property theMC through a reference with static type flash.display:SimpleButton.
I can somewhat understand why it doesn't like it, but I do not know how to resolve the issue.
if you are inside of theButton already then you won't need to call "this.theButton" because "theButton" is "this"
try
this.theMC.gotoAndPlay(3);
if you are still unsure of the object parent child relation and you are using the flash IDE, in your actions panel, click the blue target at the top of the actions panel and find the MC you are trying to reference and let the flash IDE figure out the relationships for you.
Give the movieclip in your button an instance name of "theMC". Then use the following code:
function playMovie(e:MouseEvent)
{
this.theButton.getChildByName("theMC").gotoAndPlay(3);
}// end function
theButton.addEventListener(MouseEvent.MOUSE_UP, playMovie);

flash as3: can children not run their own actionscript?

I thought I was being slick by having movieclips that I export for actionscript and then addChild later. I've made this one movieclip that loads html text through as, and it works fine when I drag it to the stage; but if I do
var trackListingBox:trackListingScreen = new trackListingScreen();
addChild(trackListingBox);
it either doesn't run the actionscript, or it's somehow broken. Can children not run their own action script?
Maybe try adding some code to your MovieClip which will fire when the movie clip is added to the stage. Something like this:
this.addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
function onAddedToStage(e:Event):void {
functionWhichLoadsHTML();
}
They can "run their own actionscript" fine. There's probably a bug in your code in the child clip, but I can't give any advice on it without actually seeing the code.
the problem was that the actionscript was loading before the items it was referring to, thereby giving me errors that items were not found.