Say I have two buttons, green and red. How do I make it so that when the green button is clicked once, it stays red until it is clicked again? I want to make a button to mute and unmute music. I am using SoundChannel to play my music i.e.
public var intro:IntroSound = new IntroSound();
public var soundControl:SoundChannel = new SoundChannel();
soundControl = intro.play(0, 100);
Thanks.
Put them in a container an toggle visibility of the top (green) Button!
Im assuming u have the buttons instantiatet, im calling them greenButton and redButton.
var container:Sprite = new Sprite();
container.addChild(redButton);
container.addChild(greenButton);
container.addEventListener(MouseEvent.CLICK, onClick);
function onClick(event:MouseEvent):void
{
greenButton.visible = !greenButton.visible; // toggle visibility of green button
//and do whaterver u need on click
}
Related
I'm trying to create a mute and unmute button. I don't want any play/pause button just mute and unmute. So far all I've created is the mute button, the problem is I have no clue as to make an unmute button. I'd also like the mute icon to change its appearance to unmute icon once it's clicked. Any help is greatly appreciated, thanks everyone.(some side comments or explanations would be nice too)
Hope I'm not asking for too much. Here's my code so far
var soundReq:URLRequest = new URLRequest("assets/for_flash.mp3");
var sound:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
sound.load(soundReq);
sound.addEventListener(Event.COMPLETE, onComplete);
function onComplete (e:Event):void
{
sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteSound);
function muteSound(e:MouseEvent):void
{
SoundMixer.stopAll();
}
Let's assume in your mute_btn instance, you have a child icon with an instance name of muteIcon and the presence of that icon is a visual indicator on whether your sound is presently unmuted or not (Like a circle with a slash through it type icon) and underneath that is a speaker icon.
This would be code then:
//First, you need to assign the result of the sound.play function to the sound channel
function onComplete (e:Event):void{
channel = sound.play();
}
mute_btn.addEventListener(MouseEvent.CLICK, muteBtnClick);
function muteBtnClick(e:MouseEvent):void
{
//check to see if the sound channel exists yet (in case you click the button before the sound loads, otherwise you'll get an error
if(!channel) return;
//toggle the icon's visibility
mute_btn.muteIcon.visible = !mute_btn.muteIcon.visible;
//now check and see if the icon is visible or not
if(mute_btn.muteIcon.visible){
//it is visible, so we should unmute the sound
channel.soundTransform = new SoundTransform(1); //1 is the volume level from 0 - 1
}else{
//it is not visible, so we should mute the sound
channel.soundTransform = new SoundTransform(0); //0 is no volume, 1 is full volumn
}
}
Also, for efficiency, you can change this:
var channel:SoundChannel = new SoundChannel();
To this:
var channel:SoundChannel;
Because creating a new object in that var is pointless as a whole new object gets assigned when you do channel = sound.play();
I am attempting to build a drag and drop game where a user can build something using the images I provide. I will have images in a menu that the user can click and drag to the building area. The user will be able to add however many instances of that image as they want.
I was able to get part of it working. So far, I can click the image and drag it around, and create as many instances as I want. However, I cannot click and drag the image once I have placed it.
When I do a trace to see what the name is, it says that all the new instances are named hillChild1. I tried to make them named hillChild1, hillChild2, etc., but that doesn't seem to work either... not sure that is an issue, though.
Here's my code:
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
var myImage:Sprite = Sprite(new Hill_mc());
var i:Number=0; i++;
function onDown(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.name = "hillChild"+i;
addChild(myImage);
myImage.x = mouseX;
myImage.y = mouseY;
myImage.startDrag();
myImage.buttonMode = true;
}
function onUp(e:MouseEvent):void {
var myImage:Sprite = Sprite(new Hill_mc());
myImage.stopDrag();
myImage.name = "hillChild";
}
stage.addEventListener(MouseEvent.CLICK, traceName);
function traceName(event:MouseEvent):void { trace(event.target.name); }
myImage.getChild(myImage).addEventListener("mouseDown", mouseDownHandler);
stage.addEventListener("mouseUp", mouseUpHandler);
function mouseDownHandler (e:MouseEvent):void{
myImage.startDrag();
}
function mouseUpHandler (e:MouseEvent):void{
myImage.stopDrag();
}
I am new to StackOverflow and also Actionscript 3, if it isn't apparent.
Your issue is likely that you are creating a new instance on mouse up (when what you want is a reference to instance that was already created on mouse down). Also, you never add a click listener to you new objects. Add the mouse up listener to stage only after the mouse down (then remove the listener in the mouse up).
thesubmenu1.hill.addEventListener(MouseEvent.MOUSE_DOWN, createCopy);
var i:int=0;
var tmpImage:Sprite; //to store which image is being dragged currently
function createCopy(e:MouseEvent):void {
tmpImage = new Hill_mc();
tmpImage.name = "hillChild"+(i++); //increment every copy
addChild(tmpImage);
tmpImage.x = mouseX;
tmpImage.y = mouseY;
tmpImage.startDrag();
tmpImage.buttonMode = true;
tmpImage.addEventListener(MouseEvent.MOUSE_DOWN, onDown); //add the mouse down to this new object
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //since the mouse is currently down, we need to listen for mouse up to tell the current copy to stop dragging
}
//this will be called when click a copy
function onDown(e:MouseEvent):void {
tmpImage = Sprite(e.currentTarget); //get a reference to the one that was clicked, so we know which object to stop dragging on the global mouse up.
stage.addEventListener(MouseEvent.MOUSE_UP, onUp); //listen for the mouse up
tmpImage.startDrag();
}
function onUp(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP,onUp); //now that the mouse is released, stop listening for mouse up
tmpImage.stopDrag(); //stop dragging the one that was clicked
}
Lets say I have 30 objects created in for loop, added to a container.
Objects stop on frame 1. I have added event listeners to the objects as you can see below, and when I click any object inside container, it goes to frame 2 and play.
for (var i:int=0; i < 30; i++)
{
var object = new Object1();
object.gotoAndStop(1);
object.addEventListener(MouseEvent.CLICK, myFunction);
container.addChild(object);
}
private function myFunction(e:MouseEvent):void
{
e.currentTarget.gotoAndPlay(2);
}
So I have to click each object to send it on frame 2,
I also tried ROLL_OVER, everything is same but CLICK is changed to ROLL_OVER inside for loop.
What I want is to click, and then mouse over object so they go to frame 2 and play.
The problem is that I need to use MOUSE_DOWN event, I have tried to set MOUSE_DOWN instead of CLICK or ROLL_OVER, but it does not work. If I want to send objects to frame 2 (using MOUSE_DOWN), I need to click each of them, there is no difference between MOUSE_DOWN and CLICK in this case.
As someone who does not know much about mouse events, I'm wondering why roll over and click works, but mouse_down does not?
I think I see what you're trying to do... you want to press the mouse to start drawing over a bunch of sprites, each one goes to frame two when you mouse over it, but only if the mouse button is pressed, right?
try something like this
container.addEventListener(MouseEvent.MOUSE_DOWN, setMouseDown);
container.addEventListener(MouseEvent.MOUSE_UP, setMouseUp);
private var mouseIsDown:Boolean = false;
private var currentSprite:Sprite;
for (var i:int=0; i < 30; i++)
{
var object = new Object1();
object.gotoAndStop(1);
object.addEventListener(MouseEvent.MOUSE_OVER, myFunction);
object.mouseChildren = false;
container.addChild(object);
}
private function setMouseDown(e:MouseEvent){
mouseIsDown = true;
setActive(currentSprite);
}
private function setMouseUp(e:MouseEvent){
mouseIsDown = false;
}
private function myFunction(e:MouseEvent){
currentSprite = e.target;
if(mouseIsDown){
setActive(currentSprite);
}
}
private function setActive(target:Sprite){
target.gotoAndPlay(2);
}
I'm relatively new to flash and as3. Basically I'm trying to load a movieclip to the stage and then click a button within that movieclip to remove the child again from the stage. I have the following movieclip:
var myResultBox:ResultBox = new ResultBox();
addChild(myResultBox);
I have a button placed within the movieclip called closeButton1. I am trying to click the close button which in turn removes the movieclip.
the code within the MovieClip is -
//Event Listener for Close button within my results box
closeButton1.addEventListener(MouseEvent.CLICK, closeBMI);
function closeBMI(evt:MouseEvent):void
{
removeChild(myResultBox);
}
Following error
code: 1120: Access of undefined property closeButton1.
Any help would be most appreciated
Below is a simple program which i think has the functionality of what you are asking for. As far as I understand, you have button A on the stage. When you click button A, movie clip B is added to the stage. Movie clip B has button B in it. When you click button B, Movie clip B and button B are removed.
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
// This creates a movie clip that contains a button.
// This button will remove the movie clip that contains
// it when it is clicked.
var MovieClipB = new MovieClip();
MovieClipB.graphics.lineStyle(1,0);
MovieClipB.graphics.beginFill(0x0000FF,1);
MovieClipB.graphics.drawRect(0,0,50,50);
var ButtonB:MovieClip = new MovieClip();
ButtonB.buttonMode = true;
ButtonB.graphics.lineStyle(1,0);
ButtonB.graphics.beginFill(0xFFFFFF,1)
ButtonB.graphics.drawCircle(0,0,10);
ButtonB.x = ButtonB.y = 25;
MovieClipB.addChild(ButtonB);
ButtonB.addEventListener(MouseEvent.CLICK, removeButtonClickHandler, false, 0, true);
function removeButtonClickHandler(event:MouseEvent):void
{
var button = MovieClip(event.currentTarget);
var container = button.parent;
container.parent.removeChild(container);
}
// This creates a button that starts on the stage.
// When clicked, it adds the movie clip defined above to the stage
var ButtonA:MovieClip = new MovieClip();
ButtonA.buttonMode = true;
ButtonA.graphics.lineStyle(1,0);
ButtonA.graphics.beginFill(0xFF0000,1)
ButtonA.graphics.drawRect(0,0,50,50);
addChild(ButtonA);
ButtonA.x = ButtonA.y = 20;
ButtonA.addEventListener(MouseEvent.CLICK, addButtonClickHandler, false, 0, true);
function addButtonClickHandler(event:MouseEvent) : void
{
addChild(MovieClipB);
MovieClipB.x = 200;
MovieClipB.y = 20;
}
Within the button? But you can't reference button in such way. You should put your code within movieclip that holds button, where you add result addChild(myResultBox); So your event handler will be able reference myResultBox
For code within a button:
this.addEventListener(MouseEvent.CLICK, closeBMI);
function closeBMI(evt:MouseEvent):void
{
//removeChild(myResultBox);
//Sadly, you don't have reference on myResultBox within a button...
}
I have a banner with a ClickTag and a hover function.
My problem is that that the user can't click on the button because of the hover function.
My code is for the ClickTag:
knap1.addEventListener(MouseEvent.CLICK, ADFclicked);
function ADFclicked(event:MouseEvent) { AdfURLNavigator.navigateToUrl( AdfFlashVarsUtil.getParameter("clickTAG"), AdfFlashVarsUtil.getParameter("landingPageTarget")); }
And for the hover function:
var holder:MovieClip = new MovieClip();
btn.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
btn.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
btn.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
function mouseOverHandler(e:MouseEvent):void{
//creating a new tooltip instance
var tooltip:Tooltip = new Tooltip();
//we tell the holder to hold our tooltip
holder = tooltip;
//positioning the tooltip on the stage
holder.x = 190;
holder.y = 280;
//adding the tooltip to the stage
addChild(tooltip);
}
function mouseOutHandler(e:MouseEvent):void{
//we remove the holder when the cursor is outside our button
removeChild(holder);
}
function mouseMoveHandler(e:MouseEvent):void{
holder.x = 190;
holder.y = 280;
}
Can anybody help?
I would assume, not seeing the entire code, that btn object is covering knap1 object so you cannot click on anything that is beneath btn.
If you want to have hover function over whole banner try using MOUSE_LEAVE event to detect mouse leaving and them MOUSE_MOVE to track if mouse is back on flash object after leaving it. As for MOUSE_MOVE event you can add listener to stage to detect mouse movement without any additional containers.
Hey you are using mousevent for hover & click at one time. So better remove addeventlistener & write the mouseover in button as inline.