flash as3 first time a button pressed has no reaction - actionscript-3

I am trying to make my first game in actionscript 3 and using 3 keyframes. this is the code from my first keyframe:
gamestart.addEventListener(MouseEvent.MOUSE_DOWN, start);
function start(e:MouseEvent):void
{
gotoAndStop(2);
}
And in my second keyframe, I have a keyboard listener.
stage.addEventListener(KeyboardEvent.KEY_DOWN ,pressButton);
function pressButton(e:KeyboardEvent):void
{
trace("aaa");
}
My problem is that, after entering the second frame, the second frame doesn't seem to get "focused on", which means I still need to click it to be able to use its keyboard events. Anyway to force focus on a frame?

Setting stage focus is as simple this:
stage.focus = stage;

Related

How do I change the size of a movieclip in flash by clicking on it? (Actionscript 3)

Everything I've found is for making a separate button, but I'm trying to make it so that clicking on the movieclip itself makes it change size. The reason is that it's being animated moving across the page, so clicking it is the challenge. Can anyone help? This is the code I tried:
info_btn_mc.buttonMode = true;
info_btn_mc.addEventListener(MouseEvent.CLICK, openInfo);
stop();
function openInfo(e:MouseEvent):void {
enemy_first.play("shrink");
}
You cannot use play("shrink"), the method
play() (Moves the playhead in the timeline of the movie clip) has no arguments. use gotoAndPlay/gotoAndStop instead that received an argument called frame:Object (frame number or label (String).
enemy_first.addEventListener(MouseEvent.CLICK, openInfo);
enemy_first.stop();
function openInfo(event:MouseEvent):void
{
trace('event.currentTarget:', event.currentTarget);
event.currentTarget.gotoAndPlay("shrink");
}

Adobe Flash. Mouseover animation endless loop

I'm having some problems while making a button for a site. I created a button and an animation, then imported that animation as an .swf to the button file. I used the following code(with btn being the button and mc the movie clip):
btn.addEventListener(MouseEvent.MOUSE_OVER,btnF);
function btnF(e:MouseEvent):void
{
mc.play();
}
As you can imagine, when the mouse is over the button, the animation plays, but it doesn't stop... Any Solutions?
Details: Using Actionscript 3.0
And yes, i made the movie clip with a stop() code at the end... so I don't see why it loops.
It might be simpler to do it this way: create a button, on default state have the graphic for the button. On the over state use a movieclip with your animation. That should work without any code
The problem is that the MOUSE_OVER event will always fire if the mouse is over that "btn" display object. So the stop() code works at the end, but the play overwrites that command.
Here is a possible solution:
btn.addEventListener(MouseEvent.MOUSE_OVER, btnF);
function btnF(e:MouseEvent):void{
{
if(mc.currentFrame == 1)
{
mc.play();
}
}
//i called the instance of the button : buthead and the instance of the movie clip : head_mc
//on the first frame of the movie clip, i've put a stop function
buthead.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
head_mc.play();
});

Applying action to a button in one frame makes it apply to whole movieclip in Flash CS6

I have a movieclip with several layers and frames. Some of those contain buttons. When I click button 1 I want it to go to the next frame. When I click button 2 I want it to go to frame 23. This is the code I'm using for button 1:
this.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.nextFrame();
}
and this is the code for button 2:
this.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
MovieClip(parent).gesprekkencivcen.gotoAndStop(23);
}
What happens now is that when I click either of the buttons, or in fact anywhere inside the movieclip (even layers I haven't applied actions to) it executes both functions at the same time so I end up going to frame 24. Can somebody provide an answer to this problem?
Obviously this in both cases refers to the same object, and not to the buttons in particular. Record the names of those buttons as you've named their instances on the timeline, say button1 and button2 and write the code employing those names.
button1.addEventListener(MouseEvent.CLICK, convnext);
function convnext(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.nextFrame();
}
button2.addEventListener(MouseEvent.CLICK, convend1);
function convend1(evt:MouseEvent):void
{
parent.parent.gesprekkencivcen.gotoAndStop(23);
}
With this, however, you will need to update the link to gesprekkencivcen in both listeners, as those buttons will have this as parent, and their target apparently is not a child of this. I have tried to plainly set a call to parent.parent.gesprekkencivcen, which might not work.

currentFrame identifiers [AS3]

I'm completely new to Flash and AS3, and I'd have thought this would be a simple question, but I've been unable to find an answer online or in the documentation. I'm trying to use addEventListener (Event.ENTER_FRAME) and (.currentFrame) to disable a button for part of an animation sequence. While the animation sequence fades in, the Button Hand cursor is visible and people can select it before the sequence is completed. I want this button disabled for the first 213frames of the main timeline, which is when the button becomes visible.
Earlier, I was successfully able to disable a Rewind button for parts of a different movie scene using the code below with a few insignificant things changed.
Skip_btn.addEventListener(MouseEvent.CLICK, SkipToGoToScene);
function SkipToGoToScene(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Opening");
}
//Skip_btn functions
Skip_btn.addEventListener(Event.ENTER_FRAME, skipDisable);
function skipDisable(event:Event) {
if (this.currentFrame < 213)
{ Skip_btn.mouseEnabled = false;
} else {
Skip_btn.mouseEnabled = true;
}
if (this.currentFrame > 213) {
Skip_btn.removeEventListener(Event.ENTER_FRAME, skipDisable);
}
}
The problem is that before I could just use "this.currentFrame" as the button was on the same timeline that it controlled, whereas now it's embedded in a MovieClip that is on the main timeline. What can I swap "this" for so I can reference this main timeline? Also, could someone fill me in on what the other "identifiers" are for ".currentFrame", as I'm not too sure how it works. The documentation examples ask for Movieclips such as "MyMovie_mc.currentFrame", but what if you just want to reference a main timeline?
If the button is on main timeline you could just use
this.root.getChildByName("Skip_btn").mouseEnabled = true;
And if you start playing animation on main timeline use
MovieClip(this.root).currentFrame

Alternatives for registering Events on the stage in AS3?

I am programming a little software prototype as Flash/Actionscript3 application. Currently I registered some Events on stage - but it is cumbersome since stopPropoagation() needs to be used all the time.
As Example:
I am having a element shown via mouseclick would and a event for closing the menu on stage. Without using stopPropagation, the menu opens and closes again immediately. The hide-function is registered on some objects so just checking if target= stage would not do it, unfortunately.
Are there any good solutions to get around this?
So you have a listener for a MOUSE_CLICK on the stage thats getting fired when you click on an 'element'.
Which looks something a bit like:
addEventListener(MouseEvent.CLICK, onClick)
function onClick(e:MouseEvent)
{
trace("CLICK")
}
mc.addEventListener(MouseEvent.CLICK, onMcClick)
function onMcClick(e:MouseEvent)
{
trace("mc")
e.stopPropagation();
}
If thats the case then yes, stage will always recieve this event since it has to do with how native flash events propogate and bubble. http://www.adobe.com/devnet/actionscript/articles/event_handling_as3_03.html
Instead of listening to the stage and having to call stopPropogation you can restructure your code. You will need to remove the listener on the stage and instead add it to the actual item so:
mc2.addEventListener(MouseEvent.CLICK, onClick)
function onClick(e:MouseEvent)
{
trace("CLICK")
}
mc.addEventListener(MouseEvent.CLICK, onMcClick)
function onMcClick(e:MouseEvent)
{
trace("MC 2 CLICK")
}
Of course this might then require you to change some of your other code but since I can't see it I am not sure what that is. Just remember that events propogate and bubble. So if you had a movieclip 'c' inside a movieclip 'b' thats on stage, and both c and b have listeners for a MOUSE_CLICK then if you click on c then both b and c events will recieve this event since it bubbles up the display list. But if c was not in b but c was on the stage and b was on the stage then this would not happen since b is not on the path for the bubbling of c. Hope that helps :)
1 solution is to check stage.focus , that is if when the menu opens the focus is on it you can add a focus out event listener so when the stage is clicked and the menu loses focus it will close .