Форум Flasher.ru

Форум Flasher.ru (http://www.flasher.ru/forum/index.php)
-   ActionScript 3.0 (http://www.flasher.ru/forum/forumdisplay.php?f=83)
-   -   Объединение трёх функций в одну (http://www.flasher.ru/forum/showthread.php?t=210287)

Rishat1c 20.02.2015 19:21

Объединение трёх функций в одну
 
Всем привет, в JavaScript я не так давно, прошу прощение если задаю 'глуповатый' вопрос.

Перейду сразу к сабжу. Есть три функций, т.е. три кнопки. Одни абсолютно идентичны.
Хочу объединить три функций в одну, т.к. это будет более логично

Код AS3:

/* Массив */
var rishat_array:Array = new Array;
this.rishat_array[0]=[b,j,f]
this.rishat_array[1]=[0.5,1]
this.rishat_array[2]=["верно","неверно"]
this.rishat_array[3]=["sss","ss","s"]
 
/* Кнопка 1 */
function sss(event:MouseEvent):void
{
    if (this.rishat_array[0][0].alpha==this.rishat_array[1][1])
    {
        this.rishat_array[0][0].alpha=this.rishat_array[1][0];
    }
    else if(this.rishat_array[0][0].alpha==this.rishat_array[1][0])
    {
        this.rishat_array[0][0].alpha=this.rishat_array[1][1];
    }
}
 
/* Кнопка 2 */
function ss(event:MouseEvent):void
{
    if (this.rishat_array[0][1].alpha==this.rishat_array[1][1])
    {
        this.rishat_array[0][1].alpha=this.rishat_array[1][0];
    }
    else if(this.rishat_array[0][1].alpha==this.rishat_array[1][0])
    {
        this.rishat_array[0][1].alpha=this.rishat_array[1][1];
    }
}
 
/* Кнопка 3 */
function s(event:MouseEvent):void
{
    if (this.rishat_array[0][2].alpha==this.rishat_array[1][1])
    {
        this.rishat_array[0][2].alpha=this.rishat_array[1][0];
    }
    else if(this.rishat_array[0][2].alpha==this.rishat_array[1][0])
    {
        this.rishat_array[0][2].alpha=this.rishat_array[1][1];
    }
}

Думал, может такой вариант прокатит, но увы

Код AS3:

function s_(event:MouseEvent, idx):void
{
    if (this.rishat_array[0][idx].alpha==this.rishat_array[1][1])
    {
        this.rishat_array[0][idx].alpha=this.rishat_array[1][0];
    }
    else if(this.rishat_array[0][idx].alpha==this.rishat_array[1][0])
    {
        this.rishat_array[0][idx].alpha=this.rishat_array[1][1];
    }
}


dimarik 20.02.2015 19:59

Вопрос в чем, собссна?

gloomyBrain 20.02.2015 20:07

Вопрос, видимо, такой: "как передать параметр в слушатель". Он, кстати, ищется поиском по форуму и даже есть в блогах.

Ответ:
Код AS3:

function s_(event:MouseEvent):void
{
    var index:int = -1;
 
    if (event.currentTarget == button_1)
    {
        index = 1;
    }
    else if (event.currentTarget == button_2)
    {
        index = 2;
    }
    else if (event.currentTarget == button_3)
    {
        index = 1;
    }
    else
    {
        throw new Error('Кто-то не тот подписался на клик! ');
    }
 
    // Ваш код здесь
}


caseyryan 21.02.2015 08:43

Цитата:

Всем привет, в JavaScript я не так давно
Для начала неплохо хотя бы знать, что это не JavaScript


Часовой пояс GMT +4, время: 01:52.

Copyright © 1999-2008 Flasher.ru. All rights reserved.
Работает на vBulletin®. Copyright ©2000 - 2026, Jelsoft Enterprises Ltd. Перевод: zCarot
Администрация сайта не несёт ответственности за любую предоставленную посетителями информацию. Подробнее см. Правила.