View Full Version : selected button in AS 3
nitras
November 5th, 2008, 10:08 AM
greetings all
hopefully a short question
i have a batch of buttons.
now in the past (as 2) i had this very simple check to see if the button was clicked, meanwhile the other buttons got back to its original state, via this simple code
var selectedM:MovieClip;
BUTTON.onRelease = function() {
if (selectedM != this) {
selectedM.enabled = true;
}
//if an item is selected we make sure the item can't be triggered again.
selectedM = this;
selectedM.enabled = false;
now in AS 3. it seems to be a different story
i tried:
var selectedM:MovieClip;
function clicker(e:Event){
if (selectedM != e.target)
{
selectedM.enabled = true;
}
selectedM = e.target;
e.target.enabled = false;
}
//for loop has BUTTON.addEventListener(MouseEvent.CLICK,clicker)
or where e.target is i tried to replace it with "this" but all to no avail
i am probally looking over something but if anyone could give me a go, i'd be very thankfull
cheers
Magik5
November 5th, 2008, 10:31 AM
function clicker(e:Event) should be function clicker(e:MouseEvent)
as thats what the listener is sending it =]
nitras
November 5th, 2008, 10:57 AM
function clicker(e:Event) should be function clicker(e:MouseEvent)
as thats what the listener is sending it =]
hmm i have to resign my first reply. that doesn't seem to solve anything.
the main purpose of this is to disable a button when its clicked and enable it again when another button has been clicked
thanks for the time so far
cheers!
creatify
November 5th, 2008, 11:50 AM
use .mouseEnabled instead of .enabled
nitras
November 5th, 2008, 12:44 PM
use .mouseEnabled instead of .enabled
tried that aswell, but i am afraid we are looking aside the subject
the issue is that "this" doesn't get recognized, so i tried e.target but that gives me errors
here is the full code:
var selectedM:MovieClip;
function CLICKED (e:MouseEvent)
{
if (selectedM != this)
{
selectedM.mouseEnabled = true;
}
selectedM = this;
this.mouseEnabled = false;
}
function doMENU()
{
for (var i:uint = 0; i <3; i++)
{
BUTTON = new CLIP();
BUTTON .x = (stage.stageWidth/2)+(spacing + i * (btn.width + spacing))-(btn.width-spacing);
BUTTON .y = (stage.stageHeight/2);
BUTTON .buttonMode = true;
BUTTON .addEventListener (MouseEvent.CLICK, CLICKED);
BUTTON.ID = i;
addChild (BUTTON);
}
}
doMENU();
so i tried
var selectedM:MovieClip;
function CLICKED (e:MouseEvent)
{
if (selectedM != e.target)
{
selectedM.mouseEnabled = true;
}
selectedM = e.target;
e.target.mouseEnabled = false;
}
function doMENU()
{
for (var i:uint = 0; i <3; i++)
{
BUTTON = new CLIP();
BUTTON .x = (stage.stageWidth/2)+(spacing + i * (btn.width + spacing))-(btn.width-spacing);
BUTTON .y = (stage.stageHeight/2);
BUTTON .buttonMode = true;
BUTTON .addEventListener (MouseEvent.CLICK, CLICKED);
BUTTON.ID = i;
addChild (BUTTON);
}
}
doMENU();
and also this doesn't work
any ideas please?
wyktor
November 5th, 2008, 02:14 PM
You probably get an "Can't convert" type of error, right?
you have to tell Flash what type of object you are working with.
try this:
function CLICKED (e:MouseEvent)
{
var clicked:MovieClip = MovieClip(e.currentTarget);
if (selectedM != clicked)
{
selectedM.mouseEnabled = true;
}
selectedM = clicked;
clicked.mouseEnabled = false;
}
nitras
November 5th, 2008, 04:23 PM
You probably get an "Can't convert" type of error, right?
you have to tell Flash what type of object you are working with.
try this:
function CLICKED (e:MouseEvent)
{
var clicked:MovieClip = MovieClip(e.currentTarget);
if (selectedM != clicked)
{
selectedM.mouseEnabled = true;
}
selectedM = clicked;
clicked.mouseEnabled = false;
}
thanks but even with this i get errors
now its a TypeError: Error #1009: can't add a method to the object
creatify
November 5th, 2008, 05:15 PM
see attached
nitras
November 5th, 2008, 05:29 PM
see attached
brilliant,
i didn't thought of making the selectedM as a null
thanks! learning again ;):doh:
FlashPlaya
November 5th, 2008, 06:26 PM
Hey Creatify,
Thanks for that lil piece of code!! :thumb2: How hard would it be to add a rollOver, rollOut animation in there? I bet a bunch of peeps would like to see how it would be done.
Thanks again!
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.