Форум 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=187497)

imediasun 20.11.2012 14:53

Можно ли отследить что какая кнопка была нажата
 
Можно ли отследить какая кнопка была нажата последней и в зависимости от этого перейти к определенному кадру. И как одно из условий, если никакая кнопка не была нажата.
Код AS3:

//ButtonContainer.as
package  {
 
        import flash.display.MovieClip;
        import flash.display.SimpleButton;
        import flash.events.MouseEvent;
        import flash.events.Event;
 
 
        public class ButtonContainer extends MovieClip {
                public var button1:SimpleButton;
                public var button2:SimpleButton;
                public var button3:SimpleButton;
                public var button4:SimpleButton;
                private var _index:int = 1;
                public function ButtonContainer() {
                        // constructor code
                        button1.addEventListener(MouseEvent.CLICK, buttonsHandler);
                        button2.addEventListener(MouseEvent.CLICK, buttonsHandler);
                        button3.addEventListener(MouseEvent.CLICK, buttonsHandler);
                        button4.addEventListener(MouseEvent.CLICK, buttonsHandler);
                }
 
                private function buttonsHandler(event:MouseEvent):void{
                        switch(event.target.name){
                                case "button1":
                                        this.setChildIndex(this["button1"], this.numChildren-1);
                                        this.setChildIndex(this["button2"], this.numChildren-2);
                                        this.setChildIndex(this["button3"], this.numChildren-3);
                                        this.setChildIndex(this["button4"], this.numChildren-4);
                                        _index = 1;
                                        break;
                                case "button2":
                                        this.setChildIndex(this["button2"], this.numChildren-1);
                                        this.setChildIndex(this["button1"], this.numChildren-2);
                                        this.setChildIndex(this["button3"], this.numChildren-3);
                                        this.setChildIndex(this["button4"], this.numChildren-4);
                                        _index = 2;
                                        break;
                                case "button3":
                                        this.setChildIndex(this["button3"], this.numChildren-1);
                                        this.setChildIndex(this["button2"], this.numChildren-2);
                                        this.setChildIndex(this["button1"], this.numChildren-3);
                                        this.setChildIndex(this["button4"], this.numChildren-3);
                                        _index = 3;
                                        break;
                                        case "button4":
                                        this.setChildIndex(this["button4"], this.numChildren-1);
                                        this.setChildIndex(this["button2"], this.numChildren-2);
                                        this.setChildIndex(this["button1"], this.numChildren-3);
                                        this.setChildIndex(this["button3"], this.numChildren-3);
                                        _index = 4;
                                        break;
                        }
                        this.dispatchEvent(new Event(Event.CHANGE));
                }
 
                public function get index():int{
                        return _index;
                }
        }
 
}

Код AS3:

//MenuItem.as
package  {
 
        import flash.display.*;
        import flash.events.*;
        import flash.external.ExternalInterface;
 
        public class MenuItem extends MovieClip {
                public var buttonContainer:ButtonContainer;
                public var slogan:MovieClip;
 
                public function MenuItem() {
                        // constructor code
                        this.addFrameScript(0, addedFrameScript);
                        this.addFrameScript(24, addedFrameScript);
                        this.addFrameScript(50, addedFrameScript);
                        this.addFrameScript(76, addedFrameScript);
                        this.addFrameScript(102, addedFrameScript);
                        buttonContainer.addEventListener(Event.CHANGE, buttonContainerHandler);
                        gotoAndPlay(2);
                        // Загрузка в контейнер по умолчанию
                        slogan.slogan.text = "Мы работаем, чтобы радоваться за Ваш бизнес";
                        ExternalInterface.available ? ExternalInterface.call("loadHtmlContenet", {catid:1, id:163, Itemid:3}) : null;
                }
 
                private function buttonContainerHandler(event:Event):void{
                        if(event.target is ButtonContainer){
                                var container:ButtonContainer = event.target as ButtonContainer;
                                var avalable:Boolean = ExternalInterface.available;
                                switch(container.index){
                                        case 1:
                                                gotoAndPlay(2);
                                                slogan.slogan.text = "Мы работаем, чтобы радоваться за Ваш бизнес";
                                                avalable ? ExternalInterface.call("loadHtmlContenet", {catid:1, id:163, Itemid:3}) : null;
                                                break;
                                        case 2:
                                                gotoAndPlay(26);
                                                slogan.slogan.text = "Мы работаем, чтобы радоваться за Ваш бизнес";
                                                avalable ? ExternalInterface.call("loadHtmlContenet", {catid:1, id:164, Itemid:3}) : null;
                                                break;
                                        case 3:
                                                gotoAndPlay(52);
                                                slogan.slogan.text = "Мы работаем, чтобы радоваться за Ваш бизнес";
                                                avalable ? ExternalInterface.call("loadHtmlContenet", {catid:1, id:165, Itemid:3}) : null;
                                                break;
                                        case 4:
                                                gotoAndPlay(78);
                                                slogan.slogan.text = "Мы работаем, чтобы радоваться за Ваш бизнес";
                                                avalable ? ExternalInterface.call("loadHtmlContenet", {catid:1, id:166, Itemid:3}) : null;
                                                break;
                                }
                        }
                }
 
                private function addedFrameScript():void{
                        stop();
                }
        }
 
}


AlexLucas 20.11.2012 15:05

Создаёте класс кнопки (например ButtonClass), задаёте ей аттрибут frame (к примеру), при обработке нажатия gotoAndPlay(ButtonClass(e.target).frame).
Можете также прицепить к ней параметры вызова ExternalInterface, чтобы не было этого switch в buttonContainerHandler(), а ещё я б вам посоветовал в buttonsHandler() пройтись циклом по всем кнопкам, и те что не нажаты (e.target.name != "button1" , если нажата button1) уже обработать, ведь если кнопок будет много (а всё может быть, заказчик - он такой), то у вас будет ещё больше избыточного кода.

imediasun 20.11.2012 15:31

Могли бы вы отобразить это в коде, что то не могу пока понять, низкий уровень AS3
Или объясните подробнее с примерами кода, не могу понять


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

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