Davy Says
Davy Says

Reputation: 85

Position of buttons as3

I need some help with Positions of buttons in AS3. Same as before Topic, i made a IRC client with AS3, but when a user join a channel, there will be a button added, but if there is already a button and the user join new channel on IRC, there will be added new button also, this for every channel that a user does join, but It should to be positioned after the last button (side by side). Same for if a user join more channels. How can i do this on best way with x and y positions?

This is my code for now:

function onChannelButton(channel)
{
var test:MovieClip = new MovieClip();
var btn:Button;

for(var j:int = 0; j < btn.length; j++) {
    btn = new Button();
    btn.name = "btn";
    btn.label = channel;
    btn.width = 90;
    btn.x = 120;
    btn.y = 487.25;
    test.addChild(btn);
    }
    addChild(test);
}

Hopefully, u can help me! Thanks!

Upvotes: 0

Views: 294

Answers (2)

Scott Thiessen
Scott Thiessen

Reputation: 875

I typically do something like the following, using your iterator j to determine the x and y position.

I've created four variables to hopefully make the code pretty self explanatory. In this case, the y position does not change per btn, which is why the btnSpacingY is set to 0.

var btnStartX:Number = 120;
var btnStartY:Number = 487.25;
var btnSpacingX:Number = 120;
var btnSpacingY:Number = 0;

function onChannelButton(channel) {
    var test:MovieClip = new MovieClip();
    var btn:Button;

    for (var j:int = 0; j < 10; j++) {
        btn = new Button();
        btn.name = "btn";
        btn.label = channel;
        btn.width = 90;
        btn.x = btnStartX + (j * btnSpacingX);
        btn.y = btnStartY + (j * btnSpacingY);
        test.addChild(btn);
    }
    addChild(test);
}

Upvotes: 0

gabriel
gabriel

Reputation: 2359

Try this:

const PADDING:uint = 10;
const NUM_BUTTONS:uint = 10;

onChannelButton();

function onChannelButton():void {
    var test:Sprite = new Sprite();
    var btn:Button;

    //NUM_BUTTONS or btn.length ? (Don't know about your Button class
    for (var i:int = 0; i < NUM_BUTTONS; i++) 
    {
            btn = new Button();
            btn.name = "btn";
            btn.width = 90;
            btn.x = (btn.width + PADDING) * i;
            test.addChild(btn);
    }

    addChild(test);
}

Upvotes: 2

Related Questions