PDA

View Full Version : Movie clip as button problem with currentTarget



theptsman
September 10th, 2009, 04:49 PM
Hi,

Thanks to IqAndreas I have cleaned up and tried this code.

Sadly he has no more input for me I hope someone can take a look at this simplified

version and see why its not playing the MC's on click like it looks like it should.

Note : Sign1movie and Sign1button are both 2 seperate movie clips that are acting like
buttons they are only there because the signmovie's are all overlapping for effect
and the signbutton's are on a single seperate layer as transparent hit points.
So sadly they have to be there...

Below is the code I currently have in my Signs ( scene ) I have 9 other scenes with
other listners once I solve this riddle I can use this in the other 5 for now 4 are going
to be much simpler.

Section below labeled "this is where the problem lies" in red.
__________________________________________________ ______________________

import flash.display.MovieClip;
import flash.events.MouseEvent;

var currentSign:MovieClip = null;

// Sign1movie Button Listeners

sign1movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign1movie.buttonMode = true;
sign1movie.useHandCursor = true;
sign1button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign1button.buttonMode = true;
sign1button.useHandCursor = true;

// Sign2movie Button Listeners

sign2movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign2movie.buttonMode = true;
sign2movie.useHandCursor = true;
sign2button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign2button.buttonMode = true;
sign2button.useHandCursor = true;

// Sign3movie Button Listeners

sign3movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign3movie.buttonMode = true;
sign3movie.useHandCursor = true;
sign3button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign3button.buttonMode = true;
sign3button.useHandCursor = true;

// Sign4movie Button Listeners

sign4movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign4movie.buttonMode = true;
sign4movie.useHandCursor = true;
sign4button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign4button.buttonMode = true;
sign4button.useHandCursor = true;

// Sign5movie Button Listeners

sign5movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign5movie.buttonMode = true;
sign5movie.useHandCursor = true;
sign5button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign5button.buttonMode = true;
sign5button.useHandCursor = true;

// Sign6movie Button Listeners

sign6movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign6movie.buttonMode = true;
sign6movie.useHandCursor = true;
sign6button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign6button.buttonMode = true;
sign6button.useHandCursor = true;

// Sign7movie Button Listeners

sign7movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign7movie.buttonMode = true;
sign7movie.useHandCursor = true;
sign7button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign7button.buttonMode = true;
sign7button.useHandCursor = true;

// Sign8movie Button Listeners

sign8movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign8movie.buttonMode = true;
sign8movie.useHandCursor = true;
sign8button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign8button.buttonMode = true;
sign8button.useHandCursor = true;

// onClicksigns Function ( THIS IS WHERE THE PROBLEM LIES )

function onClicksigns(myEvent:MouseEvent){
var clickedSign:MovieClip = MovieClip(myEvent.currentTarget);
if (currentSign == clickedSign) {
currentSign.play();
currentSign = null;
}
else if (currentSign != null) {
currentSign.play();
}
currentSign = clickedSign;
currentSign.play();
}

// BUTTON Listeners

button1b.addEventListener(MouseEvent.CLICK,clickon 1b);
button2b.addEventListener(MouseEvent.CLICK,clickon 2b);
button3b.addEventListener(MouseEvent.CLICK,clickon 3b);
button8b.addEventListener(MouseEvent.CLICK,clickon 8b);
button9b.addEventListener(MouseEvent.CLICK,clickon 9b);
button10b.addEventListener(MouseEvent.CLICK,clicko n10b);
button11b.addEventListener(MouseEvent.CLICK,clicko n11b);

function clickon1b(myEvent:MouseEvent):void
{
selected = "main";
play();
}

function clickon2b(myEvent:MouseEvent):void
{
selected = "pricesheet";
play();
}

function clickon3b(myEvent:MouseEvent):void
{
selected = "contact";
play();
}

function clickon8b(myEvent:MouseEvent):void
{
selected = "political";
play();
}

function clickon11b(myEvent:MouseEvent):void
{
selected = "contact";
play();
}

function clickon9b(myEvent:MouseEvent):void
{
var web:URLRequest = new URLRequest("http://www.zodiacsignshome.com");
navigateToURL(web, "_blank");
}

function clickon10b(myEvent:MouseEvent):void
{
var web:URLRequest = new URLRequest("http://www.zodiacsignshome.com");
navigateToURL(web, "_blank");
}

__________________________________________________ _______________

Again any input will be greatly appreciated...

Thank you,

Patrick

IQAndreas
September 10th, 2009, 05:30 PM
Didn't forget about you, just busy. ;)

I'm still having a little hard time grasping what you mean by "sign button" and "sign movie". Do you think you could upload the FLA (or send it to me via PM if you want to keep it private)? Then I can code directly onto the timeline as well as test what I post (which I normally rarely do for projects without an FLA, the result of which has usually jumped up and bit me in the bum several times...)

Anyway, I'd be glad to help out if I could have a clearer explanation.

Scythe
September 11th, 2009, 03:48 AM
I think I see the problem here. Consider this: if the user clicks on sign1button, it would tell sign1button to play and not sign1movie. And even if the code did manage to tell sign1movie to play, it would still store sign1button in the currentSign variable so it wouldn't be able to have sign1movie go back if another sign was clicked.

The best way out of this is to only have one or the other possessing an event listener. So get rid of

sign1button.addEventListener(MouseEvent.CLICK, onClicksigns);
sign1button.buttonMode = true;
sign1button.useHandCursor = true;

and just leave

sign1movie.addEventListener(MouseEvent.CLICK, onClicksigns);
sign1movie.buttonMode = true;
sign1movie.useHandCursor = true;

But here's how to keep sign1button in effect. Nest sign1button inside of sign1movie so that sign1button is a child of sign1movie. That way, if the user clicks on sign1button it will still count as clicking on sign1movie. Just make sure that inside the sign1movie clip sign1button is on a different layer, the layer lasts through all the frames, and it isn't tweened so it just sits there like it should.

Repeat for sign2movie, sign2button, etc.

EDIT: Oh, and one more thing. The else clause in the onClickSigns function should have curly braces.

else if (currentSign != null) {
currentSign.play();
}
currentSign = clickedSign;
currentSign.play();
}

should be

else {
if (currentSign != null) {
currentSign.play();
}
currentSign = clickedSign;
currentSign.play();
}
}

You don't want those last two lines to be executed if the user clicks on the sign that's currently forward.

IQAndreas
September 11th, 2009, 06:20 PM
Fixed it. My bad. :P

function onClicksigns(myEvent:MouseEvent){
var clickedSign:MovieClip = MovieClip(myEvent.currentTarget);

if (currentSign == clickedSign) {
currentSign.play();
currentSign = null;
}
else {
if (currentSign != null) {
currentSign.play();
}

currentSign = clickedSign;
currentSign.play();
}
}

IQAndreas
September 11th, 2009, 06:35 PM
:P You got it Scythe.

I had to open up the FLA and trace the values of "currentSign" and "clickedSing" in order to find out what I was doing wrong. :sigh:

Scythe
September 11th, 2009, 11:15 PM
I actually only figured out the curly braces thing from looking at your original code: http://www.kirupa.com/forum/showthread.php?t=334788

He wrote it slightly differently, and I noticed a discrepancy.