A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Expanding menu AS2 - nearly working?!

  1. #1
    Senior Member
    Join Date
    Oct 2004
    Location
    London
    Posts
    186

    resolved [RESOLVED] Expanding menu AS2 - nearly working?!

    Hi, I'm building a menu similar to the accordian component. I have four buttons each with subsections that appear when a button is clicked. Only one subsection appears at a time, so the menu expands and contracts. I've got the expanding part working, but I can't seem to get the buttons to contract to the right place when another button is clicked. I'm attaching the file in case you'd like to have a look! Here's the code as is.Thanks for any suggestions!
    Actionscript Code:
    var xml:XML = new XML();
    xml.ignoreWhite = true;
    submenus = new Array();
    xml.onLoad = function() {
        var nodes = this.firstChild.childNodes;
        numOfBtns = nodes.length;
        for(var i=0;i<numOfBtns;i++) {
            mainBtn = _root["btn"+i];
            mainBtn.createTextField("btn_txt",19,0,0,autoSize,autoSize);
            mainBtn.btn_txt.autoSize = true;
            mainBtn.btn_txt.antiAliasType = "advanced";
            mainBtn.btn_txt.text = nodes[i].attributes.btntext;
            mainBtn.btn_txt._x = Math.round((mainBtn._width/2) - (mainBtn.btn_txt._width/2));
            mainBtn.btn_txt._y = Math.round((mainBtn._height/2) - (mainBtn.btn_txt._height/2));
            mainBtn.pressed = false;
            mainBtn.onPress = down;
            var subnodes = this.firstChild.childNodes[i].childNodes;
            numofSubs = subnodes.length;
            submenus[i] = new Array();
            for (var j=0;j<numofSubs;j++) {
                subsub = this.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue;
                sublink = this.firstChild.childNodes[i].childNodes[j].attributes.variables;
                submenus[i].push(new Array(subsub,sublink));
            }
        }
    }

    xml.load("buttons.xml");

    var intervalId:Number;
    count = 0;

    function collapse(id):Void {
        if (count < submenus[id].length) {
            //trace ("add item: " + count);
            subBtn = _root["btn"+id].subholder.attachMovie("pointer","pointer"+count,-1*count);
            subBtn.count = count;
            currY = _root["btn"+id]._y+_root["btn"+id]._height
            subBtn._y = -25;
            subBtn.targetY = 25*count;
            subTxt = subBtn.createTextField("subtext",1,0,0,100,20);
            subTxt.autoSize = true;
            subTxt.html = true;
            subTxt.htmlText = submenus[id][count][0];
            subBtn.onEnterFrame = function() {
                if (this._y < this.targetY) {
                    this._y += (this.targetY - this._y)/4;
                } else {
                    //trace ("stop");
                    delete this.onEnterFrame;
                }
            }
            link = submenus[id][count][1];
            //trace (link);
            links.push(link);
        } else {
            clearInterval(intervalDown);
            //count = 0;
            if (count == submenus[id].length) {
                for (j=0;j<drop.length;j++) {
                    under = drop[j];
                    //under.slideY = under._y+(submenus[id].length*25);
                    under.slideY = (_root["btn"+id]._y+(submenus[id].length*25)+(50*(j+1)));
                    under.onEnterFrame = function() {
                        if (this._y < this.slideY) {
                            this._y += (this.slideY - this._y)/4;
                        } else {
                            //trace ("stop");
                            delete this.onEnterFrame;
                            //intervalUp = setInterval(_root, "retract", duration, _global.allup);
                        }
                    }
                }
            }
        }
        count++;
    }

    deleted = 0;
           
    function retract(up):Void {
        if (deleted < submenus[up].length) {
            upBtn = _root["btn"+up].subholder["pointer"+deleted];
            upBtn.deleted = deleted;
            upBtn.targetY = -25;
            upBtn.onEnterFrame = function() {
                if (this._y > this.targetY) {
                    this._y += (this.targetY - this._y)/4;
                } else {
                    //trace ("stop");
                    delete this.onEnterFrame;
                }
            }
        } else {
            clearInterval(intervalUp);
            _root["btn"+up].pressed = false;
            if (deleted == submenus[up].length) {
                var tomove:Number = moveup.length;
                //trace (tomove);
                for (f=tomove;f>=0;f--) {
                    var pre:Number = f-1;
                    upper = moveup[f];
                    upper.updis = ((numOfBtns-1)*50)-(f*50);
                    upper.onEnterFrame = function() {
                        if (this._y > this.updis) {
                            this._y += (this.updis - this._y)/4;
                        } else {
                            //trace ("stop");
                            delete this.onEnterFrame;
                            //intervalDown = setInterval(_root, "collapse", duration, _global.btndown);
                        }
                    }
                }
            }
        }
        deleted++;
    }

    var pressed:Boolean = false;
    duration = 200;
    deleted = 0;
    count = 0;
    links = new Array;

    function down() {
        id = this._name.substr(3,1);
        var tmpnm:Number = new Number(id);
        drop = [];
        moveup = [];
        if (submenus[id].length > 0) {
            for (d=0;d<numOfBtns;d++) {
                if (d==tmpnm) {
                    _global.btndown = id;
                    if (_root["btn"+d].pressed == false) {
                        count = 0;
                        //intervalDown = setInterval(_root, "collapse", duration, _global.btndown);
                    }
                    _root["btn"+d].pressed = true;
                    intervalDown = setInterval(_root, "collapse", duration, _global.btndown);
                } else {
                    if (_root["btn"+d].pressed == true) {
                        deleted = 0;
                        var movediff:Number = numOfBtns-1;
                        for (r=movediff;r>=id;r--) {
                            moveup.push(_root["btn"+r]);
                        }
                        var up:Number = new Number(d);
                        _global.allup = up;
                        intervalUp = setInterval(_root, "retract", duration, _global.allup);
                    }
                }
                if (d>tmpnm) {
                    drop.push(_root["btn"+d]);
                }
            }
        }
    }
    Attached Files Attached Files

  2. #2
    Senior Member
    Join Date
    Oct 2004
    Location
    London
    Posts
    186

    works better now...

    Actionscript Code:
    var xml:XML = new XML();
    xml.ignoreWhite = true;
    submenus = new Array();
    xml.onLoad = function() {
        var nodes = this.firstChild.childNodes;
        numOfBtns = nodes.length;
        for(var i=0;i<numOfBtns;i++) {
            mainBtn = _root["btn"+i];
            mainBtn.createTextField("btn_txt",19,0,0,autoSize,autoSize);
            mainBtn.btn_txt.autoSize = true;
            mainBtn.btn_txt.antiAliasType = "advanced";
            mainBtn.btn_txt.text = nodes[i].attributes.btntext;
            mainBtn.btn_txt._x = Math.round((mainBtn._width/2) - (mainBtn.btn_txt._width/2));
            mainBtn.btn_txt._y = Math.round((mainBtn._height/2) - (mainBtn.btn_txt._height/2));
            mainBtn.pressed = false;
            mainBtn.onPress = down;
            var subnodes = this.firstChild.childNodes[i].childNodes;
            numofSubs = subnodes.length;
            submenus[i] = new Array();
            for (var j=0;j<numofSubs;j++) {
                subsub = this.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue;
                sublink = this.firstChild.childNodes[i].childNodes[j].attributes.variables;
                submenus[i].push(new Array(subsub,sublink));
            }
        }
    }

    xml.load("buttons.xml");

    var intervalId:Number;
    count = 0;

    function collapse(id):Void {
        if (count < submenus[id].length) {
            //trace ("add item: " + count);
            subBtn = _root["btn"+id].subholder.attachMovie("pointer","pointer"+count,-1*count);
            subBtn.count = count;
            currY = _root["btn"+id]._y+_root["btn"+id]._height
            subBtn._y = -25;
            subBtn.targetY = 25*count;
            subTxt = subBtn.createTextField("subtext",1,0,0,100,20);
            subTxt.autoSize = true;
            subTxt.html = true;
            subTxt.htmlText = submenus[id][count][0];
            subBtn.onEnterFrame = function() {
                if (this._y < this.targetY) {
                    this._y += (this.targetY - this._y)/4;
                } else {
                    //trace ("stop");
                    delete this.onEnterFrame;
                }
            }
            link = submenus[id][count][1];
            //trace (link);
            links.push(link);
        } else {
            clearInterval(intervalDown);
            //count = 0;
            if (count == submenus[id].length) {
                for (j=0;j<drop.length;j++) {
                    under = drop[j];
                    //under.slideY = under._y+(submenus[id].length*25);
                    under.slideY = (_root["btn"+id]._y+(submenus[id].length*25)+(50*(j+1)));
                    under.onEnterFrame = function() {
                        if (this._y < this.slideY) {
                            this._y += (this.slideY - this._y)/4;
                        } else {
                            //trace ("stop");
                            delete this.onEnterFrame;
                            //intervalUp = setInterval(_root, "retract", duration, _global.allup);
                        }
                    }
                }
            }
        }
        count++;
    }

    deleted = 0;
           
    function retract(up):Void {
        if (deleted < submenus[up].length) {
            trace (deleted);
            upBtn = _root["btn"+up].subholder["pointer"+deleted];
            upBtn.deleted = deleted;
            upBtn.targetY = -25;
            upBtn.onEnterFrame = function() {
                if (this._y > this.targetY) {
                    this._y += (this.targetY - this._y)/4;
                } else {
                    //trace ("stop");
                    delete this.onEnterFrame;
                }
            }
        } else {
            if (deleted == submenus[up].length) {
                clearInterval(intervalUp);
                _root["btn"+up].pressed = false;
                deleted = 0;
                //trace ("deleted = "+ deleted);
            }
        }
        deleted++;
    }

    var pressed:Boolean = false;
    duration = 200;
    deleted = 0;
    count = 0;
    links = new Array;

    function down() {
        id = this._name.substr(3,1);
        var tmpnm:Number = new Number(id);
        drop = [];
        moveup = [];
        if (submenus[id].length > 0) {
            for (d=0;d<numOfBtns;d++) {
                if (d==tmpnm) {
                    //trace (_root["btn"+d] + " has been pressed");
                    _global.btndown = id;
                    if (_root["btn"+d].pressed == false || _root["btn"+d].pressed == undefined) {
                        count = 0;
                        //trace ("and isn't currently open");
                        intervalDown = setInterval(_root, "collapse", duration, _global.btndown);
                    }
                    count = 0;
                    _root["btn"+d].pressed = true;
                    //intervalDown = setInterval(_root, "collapse", duration, _global.btndown);
                } else if (d!==tmpnm) {
                    if (_root["btn"+d].pressed == true) {
                        //trace (_root["btn"+d] + " has to close");
                        var movediff:Number = numOfBtns-1;
                        for (r=movediff;r>=d;r--) {
                            moveup.push(_root["btn"+r]);
                        }
                        _global.up = d;
                        //intervalUp = setInterval(_root, "retract", duration, _global.allup);
                        var tomove:Number = moveup.length;
                        //trace (tomove);
                        for (f=tomove;f>0;f--) {
                            //trace (moveup[f] + " is going up")
                            upper = moveup[f];
                            upper.updis = ((numOfBtns-1)*50)-(f*50);
                            upper.onEnterFrame = function() {
                                if (this._y > this.updis) {
                                    this._y += (this.updis - this._y)/4;
                                } else {
                                    //trace ("stop");
                                    delete this.onEnterFrame;
                                    deleted = 0;
                                    intervalUp = setInterval(_root, "retract", duration, _global.up);
                                }
                            }
                        }
                    }
                }
                if (d>tmpnm) {
                    drop.push(_root["btn"+d]);
                }
            }
        }
    }
    now I want to figure out how to have the subsection retract if the same button is pressed twice. So press once to open, press again and it closes.

  3. #3
    Senior Member
    Join Date
    Oct 2004
    Location
    London
    Posts
    186
    Ok it seems to work, after getting rid of the second setInterval.
    Actionscript Code:
    var xml:XML = new XML();
    xml.ignoreWhite = true;
    submenus = new Array();
    xml.onLoad = function() {
        var nodes = this.firstChild.childNodes;
        numOfBtns = nodes.length;
        for(var i=0;i<numOfBtns;i++) {
            mainBtn = _root["btn"+i];
            mainBtn.createTextField("btn_txt",19,0,0,autoSize,autoSize);
            mainBtn.btn_txt.autoSize = true;
            mainBtn.btn_txt.antiAliasType = "advanced";
            mainBtn.btn_txt.text = nodes[i].attributes.btntext;
            mainBtn.btn_txt._x = Math.round((mainBtn._width/2) - (mainBtn.btn_txt._width/2));
            mainBtn.btn_txt._y = Math.round((mainBtn._height/2) - (mainBtn.btn_txt._height/2));
            mainBtn.pressed = false;
            mainBtn.onPress = down;
            var subnodes = this.firstChild.childNodes[i].childNodes;
            numofSubs = subnodes.length;
            submenus[i] = new Array();
            for (var j=0;j<numofSubs;j++) {
                subsub = this.firstChild.childNodes[i].childNodes[j].firstChild.nodeValue;
                sublink = this.firstChild.childNodes[i].childNodes[j].attributes.variables;
                submenus[i].push(new Array(subsub,sublink));
            }
        }
    }

    xml.load("buttons.xml");

    function collapse(id):Void {
        if (count < submenus[id].length) {
            //trace ("add item: " + count);
            subBtn = _root["btn"+id].subholder.attachMovie("pointer","pointer"+count,-1*count);
            subBtn.count = count;
            currY = _root["btn"+id]._y+_root["btn"+id]._height
            subBtn._y = -25;
            subBtn.targetY = 25*count;
            subTxt = subBtn.createTextField("subtext",1,0,0,100,20);
            subTxt.autoSize = true;
            subTxt.html = true;
            subTxt.htmlText = submenus[id][count][0];
            subBtn.onEnterFrame = function() {
                if (this._y < this.targetY) {
                    this._y += (this.targetY - this._y)/4;
                } else {
                    //trace ("stop");
                    delete this.onEnterFrame;
                }
            }
            link = submenus[id][count][1];
            //trace (link);
            links.push(link);
        } else {
            clearInterval(intervalDown);
            //count = 0;
            if (count == submenus[id].length) {
                for (j=0;j<drop.length;j++) {
                    under = drop[j];
                    //under.slideY = under._y+(submenus[id].length*25);
                    under.slideY = (_root["btn"+id]._y+(submenus[id].length*25)+(50*(j+1)));
                    under.onEnterFrame = function() {
                        if (this._y < this.slideY) {
                            this._y += (this.slideY - this._y)/4;
                        } else {
                            //trace ("stop");
                            delete this.onEnterFrame;
                            //intervalUp = setInterval(_root, "retract", duration, _global.allup);
                        }
                    }
                }
            }
            //trace (count);
        }
        count++;
    }
           
    function retract(up):Void {
        for (deleted=0;deleted< submenus[up].length;deleted++) {
            //trace (submenus[up].length);
            upBtn = _root["btn"+up].subholder["pointer"+deleted];
            upBtn.deleted = deleted;
            upBtn.targetY = -25;
            upBtn.onEnterFrame = function() {
                if (this._y > this.targetY) {
                    this._y += (this.targetY - this._y)/4;
                } else {
                    //trace ("stop");
                    delete this.onEnterFrame;
                    _root["btn"+d].pressed = false;
                }
            }
        }
    }

    var pressed:Boolean = false;
    duration = 200;
    deleted = 0;
    count = 0;
    links = new Array;

    function down() {
        trace (this.pressed);
        id = this._name.substr(3,1);
        var tmpnm:Number = new Number(id);
        drop = [];
        moveup = [];
        if (submenus[id].length > 0) {
            for (d=0;d<=numOfBtns;d++) {
                if (d==tmpnm) {
                    //trace (_root["btn"+d] + " has been pressed");
                    _global.btndown = id;
                    if (_root["btn"+d].pressed == false || _root["btn"+d].pressed == undefined) {
                        count = 0;
                        _root["btn"+d].pressed = true;
                        //trace ("and isn't currently open");
                        intervalDown = setInterval(_root, "collapse", duration, _global.btndown);
                    } else {
                        collapseme(d);
                        _root["btn"+d].pressed = false;
                    }
                    count = 0;
                } else if (d!==tmpnm) {
                    if (_root["btn"+d].pressed == true) {
                        collapseme(d);
                        _root["btn"+d].pressed = false;
                    }
                }
                if (d>tmpnm) {
                    drop.push(_root["btn"+d]);
                }
            }
        }
    }
       
    function collapseme(d) {
        var movediff:Number = numOfBtns-1;
        for (r=movediff;r>=d;r--) {
            moveup.push(_root["btn"+r]);
        }
        var up:Number = new Number(d);
        var tomove:Number = moveup.length;
        for (f=tomove;f>=0;f--) {
            upper = moveup[f];
            upper.updis = ((numOfBtns-1)*50)-(f*50);
            upper.onEnterFrame = function() {
                if (this._y > this.updis) {
                    this._y += (this.updis - this._y)/4;
                } else {
                    //trace ("stop");
                    deleted = 0;
                    delete this.onEnterFrame;
                    //intervalDown = setInterval(_root, "retract", duration, up);
                    retract(up);
                }
            }
        }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center