Action Script 3. How to hide button till game ends? - actionscript-3

I'm creating game and I need to make that when game over "Try Again" button appears. I have done the button just don't know how to hide It till game ends. It is shown all the time.
This is my button:
function tryAgain(e:MouseEvent):void
{
trace("Try again");
createCards();
}

button.visible = false;
function tryAgain(e:MouseEvent):void
{
button.visible = true;
createCards();
}
visible property on AS3 Reference

Use "visibility" property to hide DisplayObject's (Button or SimpleButton are DisplayObject's).
button.visible = false;

Related

After end of MouseMove or drag event menu keeping from release menu items

I am having a problem with draggable menu including menu button items.
At the end of drag operation (when I lift my finger from the screen) a button sub item works because of its MOUSE_UP code.
I need drag end drop my menu. After drop menu button items listeners should start for release (MOUSE_UP). How can I separete them?
I read some similar messages but I couldnt solve my problem.
My code:
addEventListener(MouseEvent.MOUSE_MOVE, dragStart);
addEventListener(MouseEvent.MOUSE_UP, dragStop);
function dragStart(e:MouseEvent):void {
e.currentTarget.startDrag(false,new Rectangle(0,0,500,0));
}
function dragStop(e:MouseEvent):void {
e.currentTarget.stopDrag(false,new Rectangle(0,0,500,0));
}
Thanks..
My sample file is here
update:
I was getting it wrong. I'd suggest switching a flag in the parent clip when it is dragged and ignore MOUSE_UP events in children if it is true.
In the parent:
var dragging:Boolean = false;
function dragStart(e:MouseEvent):void{
e.currentTarget.startDrag(false,new Rectangle(0,0,500,0));
dragging = true;
}
function dragStop(e:MouseEvent):void{
e.currentTarget.stopDrag();
removeEventListener(MouseEvent.MOUSE_MOVE, dragStart);
dragging = false;
}
In subItem:
function BTN(e:MouseEvent):void{
if(!(parent as MovieClip).dragging){
trace("I'm a button: "+this+" Did you hit me when you sliding?");
}
}

Set object's `visible` to `true` when a `tap` event is performed then set it back to `false`

I have an object as MovieClip and I have a button as Button on my flash timeline.
When the button is tapped, I want to set the object.visible to true then when the button is not tapped, I want to set it back to false.
How can I do that?
I have tried this code, but it won't works as I want. I only can show the object but cannot hide it back.
button1.addEventListener(TouchEvent.TOUCH_TAP, touchTap);
function touchTap(e:TouchEvent): void {
mcObj.visible = true;
stage.addEventListener(TouchEvent.TOUCH_END, touchEnd);
}
function touchEnd(e:TouchEvent): void {
mcObj.visible = false;
stage.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
}
I think this code could work.
button1.addEventListener(TouchEvent.TOUCH_BEGIN, touchTap);
function touchTap(e:TouchEvent): void {
mcObj.visible = true;
button1.addEventListener(TouchEvent.TOUCH_END, touchEnd);
}
function touchEnd(e:TouchEvent): void {
mcObj.visible = false;
button1.removeEventListener(TouchEvent.TOUCH_END, touchEnd);
}
I changed
1: TouchEvent.TOUCH_TAP to TouchEvent.TOUCH_BEGIN
2: stage.addEventListener to button1.addEventListener
Before saying anything about your problem, let's take a look on the definitions of the TouchEvent.TOUCH_BEGIN, TouchEvent.TOUCH_END and TouchEvent.TOUCH_TAP events :
The TouchEvent.TOUCH_BEGIN is :
Dispatched when the user first contacts a touch-enabled device ...
The TouchEvent.TOUCH_END is :
Dispatched when the user removes contact with a touch-enabled device ...
The TouchEvent.TOUCH_TAP is :
Dispatched when the user lifts the point of contact over the same InteractiveObject instance on which the contact was initiated on a touch-enabled device ...
And with some tests, we can see that the TouchEvent.TOUCH_END event is, in almost cases, fired before the TouchEvent.TOUCH_TAP one (by 1 or 2 milliseconds), so we can understand the we are able to detect if the user has already removed contact with the device (TouchEvent.TOUCH_END is fired) then if that was on the same InteractiveObject object on which the contact was initiated (TouchEvent.TOUCH_TAP is fired).
And that's why your code is not working.
Now, let's see your problem : you want to show a MovieClip just when your user tap a button and hide it when he releases that button but only for a very short time (the time of a tap ~= 300 milliseconds).
In this case, I recommend you to use a TouchEvent.TOUCH_BEGIN event listener with a timeout to hide that object even if your user didn't release the button.
For that, take this example :
Multitouch.inputMode = MultitouchInputMode.TOUCH_POINT;
btn.addEventListener(TouchEvent.TOUCH_BEGIN, on_touchBegin);
function on_touchBegin(e:TouchEvent): void
{
obj.visible = true;
hide_obj();
}
function hide_obj(): void
{
// you can use a Timer object instead of setTimeout()
var timeout:int = setTimeout(function(){
clearTimeout(timeout);
obj.visible = false;
}, 300);
}
Hope that can help.

Actionscript 3.0 - Mouse_OVER, Mouse_OUT to show / hide items...?

Making a interactive tour guide where you mouse over an icon and information pops up (Images, text, etc...) I can get my test image to become visible on MOUSE_OVER but it does not become invisible when I MOUSE_OUT my icon/button.
Here is what I have so far....
sthelens1 is an image
butt1 is my icon for the mouseto mouse over and out
sthelens1.visible = false
butt1.addEventListener(MouseEvent.MOUSE_OVER,showImage);
butt1.addEventListener(MouseEvent.MOUSE_OUT,showImage);
function showImage(MouseEvent){
if(MouseEvent == "MOUSE_OUT"){
sthelens1.visible = false;
}
if(MouseEvent = "MOUSE_OVER"){
sthelens1.visible = true;
}
}
Any guidance or help would be appreciated..
use (e:MouseEvent) and then you can get more info on the e variable.
You can also use a separate listener function on MOUSE_OUT
sthelens1.visible = false
butt1.addEventListener(MouseEvent.MOUSE_OVER, showImage);
butt1.addEventListener(MouseEvent.MOUSE_OUT, hideImage);
function showImage(e:MouseEvent){
sthelens1.visible = false;
}
function hideImage(e:MouseEvent){
sthelens1.visible = true;
}

Flex topLevelApplication and SuperTabNavigator mouse event handler

I am using the flex SuperTabNavigator and want on closing the tab check if the control button was pressed. I tried:
public static var CONTROL_PRESSED:Boolean = false;
public function init():void {
var clickListener:Function = function _clickListener(event:MouseEvent):void{
trace(event.ctrlKey);
if(event.ctrlKey){
CONTROL_PRESSED = true;
}else{
CONTROL_PRESSED = false;
}
};
FlexGlobals.topLevelApplication.addEventListener(MouseEvent.CLICK, clickListener);
}
The problem with this is that the mouse click is called everywhere in the application except on the tab. I also tried the same code but addEventListener(MouseEvent.CLICK, clickListener); to add the listener to the SuperTabNavigator and it did not work at all. Is there another way to catch the mouse click?
This is because the SuperTabNavigator has a private mouse click handler function that hides the MouseEvent:
private function closeClickHandler(event:MouseEvent):void {
if(this.enabled) {
dispatchEvent(new Event(CLOSE_TAB_EVENT));
}
event.stopImmediatePropagation();
event.stopPropagation();
}
You'll need to modify the SuperTab class in the SuperTabNavigator source to dispatch some CustomEvent with the data you want instead of a plain new Event(CLOSE_TAB_EVENT).
Then change the onCloseTabClicked function in SuperTabBar to know about your CustomEvent. Then you can pass that to your application code (by adding it to the SuperTabEvent, perhaps).

Scrolling within AS3 Flash

I'm trying to scroll a movieclip within flash. The problem is I have buttons within the movieClip so everytime I try to scroll its difficult not to open the button. My code is below for the scroll im using which is fairly simple.
ChestBiceps.addEventListener(MouseEvent.MOUSE_DOWN, ClipDraggedOn);
var boundsRect:Rectangle = new Rectangle(ChestBiceps.x, -200, 0, 310);
function ClipDraggedOn(event:MouseEvent):void {
ChestBiceps.startDrag(false, boundsRect);
stage.addEventListener(MouseEvent.MOUSE_UP, ClipDraggedOff);
}
function ClipDraggedOff(event:MouseEvent):void {
ChestBiceps.stopDrag();
stage.removeEventListener(MouseEvent.MOUSE_UP, ClipDraggedOff); }
Could someone try and figure how I disable the buttons almost whilst scrolling? I still want to be able to use the buttons, just not when scrolling...Thanks in advance
To disable the buttons add the following:
function ClipDraggedOn(event:MouseEvent):void {
ChestBiceps.mouseEnabled = false;
ChestBiceps.mouseChildren = false;
[...]
To re-enable the buttons add the following:
function ClipDraggedOff(event:MouseEvent):void {
ChestBiceps.mouseEnabled = true;
ChestBiceps.mouseChildren = true;
[...]