Problems Scripting Multiple Buttons(nearly identical) in a single Action Script

一曲冷凌霜 提交于 2019-12-12 03:57:40

问题


I am a noob to action script 3 so please forgive this detailed Posting ! (and code posting errors)

I am making an interactive flash Project...It has has 17 Separate scenes ...

  • Intro Scene
  • "Main_ Sequence"
  • 15 Individually title song pages

I have scripted the Intro-->"main" sequence with no problems ....

Where my issue is the "main sequence" has 15 Buttons and I need to link them to the 15 separate scenes ...I have tried two different sets of code(see below) and kept getting Compiler Errors... ..I would appreciate if some one can tell me which of these two formats is best for my purpose & what exactly I am doing wrong ....

Code 1: This is a modified from what I am using to link the individual pages back to the main sequence

TD_g.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler1);
function mouseDownHandler1(event:MouseEvent):void {

gotoAndStop(1,"Tweedlee_Dee");
}
  s_g.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void {

gotoAndStop(1,"Sincerely");
}
      ats_g.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler3);
  function mouseDownHandler3(event:MouseEvent):void {

gotoAndStop(1,"Ain’_that_a_shame");

...Repeated 15 times ... the _gportion is in reference to the unique instance names I assigned to each button on the main sequence ...I changed the #in each MouseDownHandlrbecause I read somewhere that each event had to be different(?) ..Every time I test the scene I get the following ...

TypeError: Error #1009: Cannot access a property or method of a null object reference. at music_Sheet_project_14_Main_Sequence_fla::MainTimeline/frame1( ...and then the scene starts as and does its animation followed by the mouseover effects but the buttons which should return an error(?) if I click on them produce no "reaction"

...everytime I test the movie(with just that short code!) I get the following ...

14 Compiler errors

All 1021: Duplicate function definition. Source : function mouseDownHandler(event:MouseEvent):void {

..So I tried a different route ...

**Code2 **This is a different code I found style I found while trying to fix the first code

stop();
TD_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
s_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ats_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
iyk_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
hms_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tf_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
hd_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ld_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ll_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ts_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ipsy_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ysm_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
ihm_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
iss_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
tl_g.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler(event:MouseEvent):void
{
var nameOfButton:String = event.currentTarget.name;
if (nameOfButton == "TD_g")
{
 gotoAndStop(1, "Tweedlee_Dee");
}
 else if (nameOfButton=="s_g")
{
gotoAndStop(1, "Sincerely");
}
else if (nameOfButton=="ats_g")
{
gotoAndStop(1, "Ain’_that_a_shame");
}
else if (nameOfButton=="iyk_g")
{
 gotoAndStop(1, "I_hear_you_knocking");
}

...this goes on 15 more times including Line #64

{
else if (nameOfButton=="ysm_g");
{
 gotoAndStop(1, "You_shook_me");

when I attempt to run this I get the following ...

"1083: Syntax error: else is unexpected. Source else if (nameOfButton=="ysm_g");

When I look this up I get suggestions that the syntax is wrong ...but why would it pick this line and not all the other lines...I have tried retyping and re formating but I keep getting that error...

*Which of these codes is the Best to use ? (and why is it not working???) Any/All Advice & Help would greatly be appreciated ...THANKS!!! *


回答1:


The problem is that you don't have a corresponding closing brace before the else statement. Additionally you have semi-colon at the end of the else which will mess things up:

{
else if (nameOfButton=="ysm_g");
{
 gotoAndStop(1, "You_shook_me");

Should be

{
//...Whatever code goes here
}
else if (nameOfButton=="ysm_g")
{
   gotoAndStop(1, "You_shook_me");
}



回答2:


You can avoid the whole if-elseif mess if you link your objects to the name of the scene you want the to trigger. This is much easier to read and maintain, since all of the configuration is done in the init() method below. the Method setSceneByTarget() is public, so you are able to add more items at runtime.

public class Main extends MovieClip
{
    private var _scenesByTarget:Dictionary;
    public function Main()
    {
        _scenesByTarget = new Dictionary(true);
        //call init() or hook it to ADDED_TO_STAGE event if you want
    }
    public function init():void
    {
        //add your listeners here like this:

        //myItem1.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        //setSceneByTarget(myItem1, myItem1Scene);

        //myItem2.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
        //setSceneByTarget(myItem2, myItem2Scene);

        //...
    }

    public function setSceneByTarget(targetObj:Object, targetScene:String):void
    {
        _scenesByTarget[targetObj] = targetScene;
    }
    private function jumpToSceneByTarget(targetObj):void
    {
        if (_scenesByTarget[targetObj] != null) {
            gotoAndStop(1, _scenesByTarget[targetObj]);
        }
    }
    private function onMouseDown(evt:MouseEvent):void
    {
        jumpToSceneByTarget(evt.target);
    }
}


来源:https://stackoverflow.com/questions/15994739/problems-scripting-multiple-buttonsnearly-identical-in-a-single-action-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!