Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to simplify this function ?

New Here ,
Sep 12, 2014 Sep 12, 2014

Hello, I am trying to add fish of different colors on the canvas by triggering a function on a button click, each color has its own movieclip (in this case, fishred and fishblue)).

The thing is that the function are all the same but only the var for color changes, and I have to make multiple colors.

Is there a way to change it into a single function that will choose which movieclip will be added when clicking on a specific button ?

var red:fishred = new fishred();

var blue:fishblue = new fishblue();

function addRed(e:Event) {

    if (red.stage) {

        red.parent.removeChild(red);

    } else {

        red.x = Math.random() * 550;

        red.y = Math.random() * 400;

        red.dx = Math.round(Math.random() * 550);

        red.dy = Math.round(Math.random() * 400);

        addChild(red);

    }

function addBlue(e:Event) {

    if (blue.stage) {

        blue.parent.removeChild(red);

    } else {

        blue.x = Math.random() * 550;

        blue.y = Math.random() * 400;

        blue.dx = Math.round(Math.random() * 550);

        blue.dy = Math.round(Math.random() * 400);

        addChild(blue);

    }

}

TOPICS
ActionScript
215
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Sep 12, 2014 Sep 12, 2014

If you assign a property to each button that is the same as the name of its corresponding fish color then you could just use the e.currentTarget property to identify which button was clicked and use that button's property to target the fish...

function addFish(e:Event) {

    var fish:String = e.currentTarget.color_name;

    if (this[fish].stage) {

       this[fish].parent.removeChild(red);

    } else {

        this[fish].x = Math.random() * 550;

        this[fish].y = Math.random() * 400;

        this[f

...
Translate
LEGEND ,
Sep 12, 2014 Sep 12, 2014

If you assign a property to each button that is the same as the name of its corresponding fish color then you could just use the e.currentTarget property to identify which button was clicked and use that button's property to target the fish...

function addFish(e:Event) {

    var fish:String = e.currentTarget.color_name;

    if (this[fish].stage) {

       this[fish].parent.removeChild(red);

    } else {

        this[fish].x = Math.random() * 550;

        this[fish].y = Math.random() * 400;

        this[fish].dx = Math.round(Math.random() * 550);

        this[fish].dy = Math.round(Math.random() * 400);

        addChild(this[fish]);

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 13, 2014 Sep 13, 2014

Took me time to understand what to change but it works !! Thank you !

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 13, 2014 Sep 13, 2014
LATEST

You're welcome

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines