Before i start i want to let you know today is my first day with AS3.
I wanted to know how to do an onclick function in AS3.
For example i have button 1 ( as Instance name)
and when clicked i want it to hide and show another box. this is what i found online but how can i make it on click.
this.button1.alpha = 100;
Thanks so much.
You want
button1.addEventListener(EventType, callback);
You replace EventType with a mouse event (such as MouseEvent.MOUSE_DOWN) and callback is a function that you define, which is called whenever the event occurs.
See the following example, taken from this page on the FlashEnabled Blog:
// attach the event listener to this object, if you want a global event outside
// the current class attach to stage.addEventListener([event],[callback])
this.addEventListener(MouseEvent.CLICK, onMouseClickEvent);
// then make the callback
public function onMouseClickEvent(event:Event) {
trace(event);
if(event.buttonDown)
// if primary button down, left mouse button
trace(”left button was down”);
else
trace(”left button was not down”);
}
}
The above code sample attaches a click event handler to this (whatever context this code is executed in - it could be global, or inside a class). Inside your event handler, you'd want to use the Tween class (as explained on Kirupa.com) to animate the box out and the other box in.
Since you mentioned that it's your first day, note that trace() writes to the console.
Related
For the MOUSE_MOVE event, the documentation says there is a buttonDown property to indicate whether or not the left mouse button is currently down. But how can I determine if the right button is down?
There is not, but you can do this by setting a flag inbetween Right Mouse down and Right mouse up. If you listen on capture with a high priority, it will be available in all other mouse events.
In your document class or main timeline frame 1, add the following code:
var isRightMouseDown:Boolean = false;
stage.addEventListener(MouseEvent.RIGHT_MOUSE_DOWN, globalMouseDown,true,int.MAX_VALUE)
function globalMouseDown(e:MouseEvent):void {
isRightMouseDown = true;
}
stage.addEventListener(MouseEvent.RIGHT_MOUSE_UP,globalMouseUp,true,int.MAX_VALUE)
function globalMouseUp(e:MouseEvent):void {
isRightMouseUp = false;
}
Now you have a var you can access in your mouse move listeners. If using timeline code, access it outside the main timeline by doing MovieClip(root).isRightMouseDown. If using a document class, define it as static public static var isRightMouseDown:Boolean and access it like so from anywhere in your app: MyMainClassName.isRightMouseDown. (replace MyMainClassName with whatever you've called your document class)
When you add the listeners above, putting the third parameter as true and the fourth parameter as int.MAX_VALUE will ensure this listener will be processed before any others listening for the same event in your application.
For more information about how events work and their phases, see this:
http://www.adobe.com/devnet/actionscript/articles/event_handling_as3.html
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.
Normally, forms and pop Ups don't have the focus set when they are just displayed. The obvious solution was to set the focus to the first input in the creation complete event of the component, so the keyboard short Cuts like tab and space start working.
The problem is that, creation complete is not the panacea, sometimes the element is not focus-able at that point, and i am not sure why that happens.
The render event would ensure the focus, but it dispatches too much for a very simple purpose.
In which point a component is always ready to be focus-able?
Edit: The component giving me trouble to get start up focus, is a TitleWindow, which can be poped in 2 ways, a Mouse click event and a keyboard event.
When the tite window is displayed by a click, the first input gets focus in the creation complete event, but when displayed by a keyboard event, it doesnt...
By now i got it working with the following code:
private function titlewindow_creationCompleteHandler(e:FlexEvent):void{
callLater( setTextInputFocus);
}
private function setTextInputFocus():void{
txtPregunta.setFocus();
}
But doubt the way is shown has anything to do with this... because, some other TitleWindow are displayed this way too and they're fine.
So what could it be?
The render event would ensure the focus, but it dispatches too much for a very simple purpose.
If this is true then why not try this:
private function titlewindow_creationCompleteHandler(e:FlexEvent):void{
var callback : Function = function(re : Event) : void {
titlewindow.removeEventHandler(RenderEvent.orsomething, callback);
setTextInputFocus();
};
titlewindow.addEventHandler(RenderEvent.orsomething, callback);
}
Might be kind of a hack since it should be focusable on creationComplete but it would probably work.
I have a container with many images. Instead of adding a listener for click and other mouse events on each of the images, I would like to only listen for those events on the parent of the image.
Is that possible?
container.addEventListener(MouseEvent.CLICK, clickHandler);
private function clickHandler(e:MouseEvent):void {
trace(e.currentTarget); // references container
trace(e.target); //references container's child or container itself depending on what has been clicked
}
If i am understanding your question correctly, that is totally possible. So assuming you have something like:
parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());
parent.addChild(new Child());
Then you should be able to bind the event listener to the parent thusly:
parent.addEventListener(MouseEvent.CLICK, handleClick);
and then your handler should look something like
private function handleClick(e:MouseEvent) {
// cast the target of the event as the correct class
var clickedChild:Child = Child(e.target);
// Do whatever you want to do.
}
You can also combine this with the useCapture argument of addEventListener to attach the event on the capturing side of the event rather than the bubbling side. And also use the .stopPropagation() method on the Event to stop any other event handlers from firing as well...
But its difficult to say if you need to use those without knowing more about what you are trying to do. But hopefully that will give you a push in the right direction.
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 .