Copy link to clipboard
Copied
Hello everyone!
i'm working in html5 canvas projectvand i'd like to add some feature at this code that for now work fine.
In short, i have 7 movieclips with instance name button1, button2, button3, ...etc and these lines of code highlight the button that is pressed.
Now I need to add, according to the button pressed, a specific action, in my case a goToandStop() in the main timeline.
here the code:
tnx a lot
assign their names:
for (i = 1; i <= numButtons; i++) {
b = this["button" + i];
b.name="button"+i
Copy link to clipboard
Copied
with a switch-case (or a sequence of if-else) statments in your click handler.
Copy link to clipboard
Copied
Tnx for reply Kgald! but my problem is that i can't access at pressed button. how don't know how to do... i've tried a lot and lot!!!
Copy link to clipboard
Copied
use:
case "button1":
//do whatever
break;
case "button2":
//do whatever
break;
etc
}
Copy link to clipboard
Copied
mmmhh no doesn't work! 😞
i'have also created a function but nothing... no alerts displayed
stage.enableMouseOver(20);
// variables
var numButtons = 7;
var i, b, curButton;
var tgt = this["button" + i];
// initialize
for (i = 1; i <= numButtons; i++) {
b = this["button" + i];
var tgt =b
b.cursor = "pointer";
b.mouseChildren = false;
b.addEventListener("click", buttonClickHandler.bind(b));
b.addEventListener("mouseover", buttonRollOverHandler.bind(b));
b.addEventListener("mouseout", buttonRollOutHandler.bind(b));
}
// event handlers
function buttonPressed() {
switch(this.name){
case "button1":
alert("btn1");
//do whatever
break;
case "button2":
alert("btn2");
//do whatever
break;
}
}
function buttonClickHandler() {
if (curButton) {
curButton.gotoAndStop(0);
}
this.gotoAndStop(2);
curButton = this;
buttonPressed();
//alert(i+71);
}
function buttonRollOverHandler() {
if (this != curButton) {
this.gotoAndStop(1);
}
}
function buttonRollOutHandler() {
if (this != curButton) {
this.gotoAndStop(0);
}
}
Copy link to clipboard
Copied
that's not the code i suggested.
stage.enableMouseOver(20);
// variables
var numButtons = 7;
var i, b, curButton;
var tgt = this["button" + i];
// initialize
for (i = 1; i <= numButtons; i++) {
b = this["button" + i];
var tgt =b
b.cursor = "pointer";
b.mouseChildren = false;
b.addEventListener("click", buttonClickHandler.bind(b));
b.addEventListener("mouseover", buttonRollOverHandler.bind(b));
b.addEventListener("mouseout", buttonRollOutHandler.bind(b));
}
// event handlers
function buttonPressed(btn) {
switch(btn.name){
case "button1":
alert("btn1");
//do whatever
break;
case "button2":
alert("btn2");
//do whatever
break;
}
}
function buttonClickHandler() {
if (curButton) {
curButton.gotoAndStop(0);
}
this.gotoAndStop(2);
curButton = this;
buttonPressed(this);
//alert(i+71);
}
function buttonRollOverHandler() {
if (this != curButton) {
this.gotoAndStop(1);
}
}
function buttonRollOutHandler() {
if (this != curButton) {
this.gotoAndStop(0);
}
}
Copy link to clipboard
Copied
no no doesn't work:-(
if i put alerts in switch cases they are not displayed
if i put an alert here
function buttonPressed(btn) {
alert(btn.name);
switch(btn.name){
case "button1":
//do whatever
break;
it return null
i'can't display button's name in alert.
Copy link to clipboard
Copied
assign their names:
for (i = 1; i <= numButtons; i++) {
b = this["button" + i];
b.name="button"+i
Copy link to clipboard
Copied
Oh my god!!! tnx a lot!
Copy link to clipboard
Copied
you're welcome.