Skip to main content
Participating Frequently
November 9, 2017
Question

Creating an action on a button in action script 3 adobe flash CS6

  • November 9, 2017
  • 2 replies
  • 11039 views

HI,

I am a newbie to flash, I have however learnt a lot and achieved a lot, including using coding in action script within the past week. However, I have problems with getting a button to link to another scene, it sounds so simple and there are loads of tutorials out there, BUT, i get to the same point every time that doesn't work for me. I have a main page which is the solar system which is coded to be looped every 20seconds - I managed to create invisible motion tweened buttons to follow the video file of the planets, they are clickable and are working but when i try to open the actions panel for the button to actually function, I get the messaged - "in action script 3 code cannot be placed directly on objects" I have tried it all different ways but can't seem to figure it out ! I have tried double clicking on the button and adding a layer within the button for coding, but the same message appears - it also adds another button layer - not timeline layer. I have tried going back to the main scene and adding a layer - but the codes that I've found that everyone is using for scene navigation doesn't work - because how will it know which layer/element I want to divert from.

Basically - I have 10 planets, I want to link the 10 planets to 10 separate scenes, I have 10 buttons which have been motion tweeted and filtered to match the visual of the planets (which i made and imported from after effects) the planets and planet buttons are all on separate layers.

any advice greatly welcome !

Jasmine

[Moved from the non-technical Lounge to the specific Program forum... Mod]

[Here is the list of all Adobe forums... https://forums.adobe.com/welcome]

This topic has been closed for replies.

2 replies

JoãoCésar17023019
Community Expert
Community Expert
November 10, 2017

Hi. I hadn't seen your FLA while I was doing this little sample: https://goo.gl/WYWFgF

I hope it can help you.

Never thought it would be so challenging to code using scenes.

Anyway... Try avoiding scenes when using AS3. It's better to rely on different frames and/or Movie Clips.

Participating Frequently
November 10, 2017

Hi JoaoCesar,

thanks for the input, I have a 600frame fla because i need it to loop, so I can't click to a further blank frame outside of that. But thats a good trick for next time ! thanks !!

Jasmine

JoãoCésar17023019
Community Expert
Community Expert
November 10, 2017

You're welcome!

As I said before, I did my example relying only on your first comment. If I had seen your FLA before doing the sample, surely I would have come out with a different solution for your specifc case.

Please let me know if Colin can't help you.

Colin Holgate
Inspiring
November 9, 2017

In ActionScript 2 you could attach a script to a button, but in ActionScript 3 you create a listener for the click, and then do something with the information.

You can end up with fairly short functions, if you carefully name your buttons. For example, if each button had a name that was the same as the scene you want to go to (eg, button "earth" needs to take you to scene "earth scene"), you could have this script in the first frame of the menu scene:

stage.addEventListener(MouseEvent.CLICK,checkplanet);

function checkplanet(e:MouseEvent){

    if(e.target.name==null || e.target.name=="") return;

     stage.removeEventListener(MouseEvent.CLICK,checkplanet);

    gotoAndPlay(1, e.target.name+" scene");

}

I'm on a bus as I type this without testing, so watch out for errors! But it should work. If you click not on a button, or on a button that doesn't have a name, nothing should happen. If it's one of the named planet buttons it will combine the button name with " scene" to work out the name of the scene to go to.

Participating Frequently
November 9, 2017

Thank You so much for replying !

I tried what you said, it didn't work, I'm not confident I understand you correctly though....

do I just copy and paste the code you provided (as is) onto frame 1 of the scene i want the button to go too....

I am starting with the Sun planet -

so i have 2 layers named -   Sun    (which is the fla video file) and i have    Sun Button (which is the motion tweened button). I have called scene 2 SunScene. I inserted your code but nothings happening ... how does it know I am talking about Sun Button and Sun Scene ...do i change the word Name in your code to SunButton ? so

stage.addEventListener(MouseEvent.CLICK,checkplanet);

function checkplanet(e:MouseEvent){

    if(e.target.SunButton==null || e.target.SunButton=="") return;

     stage.removeEventListener(MouseEvent.CLICK,checkplanet);

    gotoAndPlay(1, e.target.SunButton+" scene2");

}

thanks again Colin ! you star !

Jasmine

Colin Holgate
Inspiring
November 10, 2017

This part:

gotoAndPlay(1, e.target.SunButton+" scene2");

would need to be like I wrote it:

gotoAndPlay(1, e.target.name+" scene");

It would then use the name that you had given the button on the stage. That is, you select the first frame where the button starts and look at the Properties panel, and give it a name of "sun", and you would have a scene you have named "sun scene" (you can change the names of scenes, they don't have to remain as Scene 1, Scene , etc).

If you want to post the FLA somewhere I can look at it, I can fix whatever you got wrong, so you can see what I meant.