Array of Buttons?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Array of Buttons?

Post by ChessGredon » Thu May 22, 2014 4:12 pm

How do I create a multi-dimensional array which consists buttons? This could very helpful for board games like chess or go. For example in C# I would write

Code: Select all

Button[,] square = new Button[10, 10];
for (i = 1; i <= 8; i++)
            {
                for (j = 1; j <= 8; j++)
                {
                    square[i, j] = new Button();
                    square[i, j].Size = new Size(70, 70);
                    square[i, j].Anchor = AnchorStyles.Right;
                    square[i, j].Location = new Point(12 + (i - 1) * 68, 600 - 68 * (j - 1));
                    square[i, j].BackgroundImage = empty;
                    square[i, j].Visible = true;
                    this.Controls.Add(square[i, j]);
                    square[i, j].Click += new System.EventHandler(ClickButton);
                }
            }
and then I could write a common EventHandler for all buttons (in Livecode I think it's only Handler) and then change a button only by knowing his place in the array. I fail to find information, I googled "Livecode array of Buttons" "Livecode array of objects" and so on but I couldn't find how. Would appreciate some help :)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Array of Buttons?

Post by dunbarx » Thu May 22, 2014 4:56 pm

Hi.

If I understand what you are trying to do, know that an array is a type of variable, and can only hold data. This data can reference any characteristics of an "array" (group?) of buttons, including their properties. And then you can write handlers to populate or extract data as needed...

Or if I do not understand, might you be talking about using a dataGrid in its "form" style? This sort of gadget can "contain' buttons, and the whole thing can be accessed similarly to accessing an array.

Craig Newman

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: Array of Buttons?

Post by ChessGredon » Thu May 22, 2014 5:01 pm

The problem is that if I use group I think I can't have two dimensions. For example, if I want to change the image of the square 1, 1, I can't do that.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9785
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Array of Buttons?

Post by dunbarx » Thu May 22, 2014 5:30 pm

The array variable can hold data ordered as you see fit.

answer buttonArray[buttonName]["loc]["rect"]

would give you the rect of button "buttonName"

Craig

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4036
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Array of Buttons?

Post by bn » Thu May 22, 2014 6:29 pm

Hi,

I don't want to spoil the party but here is a little sample stack that shows one way how to do that.

@Craig, I started this stack before your post and since chessGredon knows C# he just wants to know how to do this in LC. Anyways he either will build on it or not.


Kind regards

Bernd
Attachments
chessButtonArray.livecode.zip
(3.73 KiB) Downloaded 236 times

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: Array of Buttons?

Post by ChessGredon » Fri May 23, 2014 9:01 pm

Thanks for spending time to answer me!
I think I know how to continue. I will make handler in one of the buttons with parameters i, j and then call this handler from all the others. Awesome, thanks again, bn!

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4036
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Array of Buttons?

Post by bn » Fri May 23, 2014 9:28 pm

Hi ChessGredon,

if you get stuck feel free to ask here in the forum.

What is difficult for many new users of Livecode how messages are passed. The message hierarchy.

Here is a short introduction
http://www.fourthworld.com/embassy/arti ... _path.html

and here a bit about coding style in general but also LiveCode
It pays off big time to spend some time to dig into the specifics of LiveCode.

All this because
I will make handler in one of the buttons with parameters i, j and then call this handler from all the others
It may be wiser to put the handlers into the card script and handle your code there. For this you might want to check out the "Target" in the dictionary. In short it indicates which control received a message and since it did not handle the message, the messages goes up the message path. The card would be the next higher instance of all messages from buttons on a card.

This is a collection of stacks that give a very good introduction to Livecode, or Revolution as it was called at that time. They cover all the basics that still apply.
http://www.hyperactivesw.com/revscriptc ... ences.html

Kind regards
Bernd




http://www.fourthworld.com/embassy/arti ... style.html

ChessGredon
Posts: 24
Joined: Thu May 15, 2014 4:40 pm

Re: Array of Buttons?

Post by ChessGredon » Sat May 24, 2014 1:22 pm

Thanks, I will check these thing out.

I made a similar to chess game with another idea (my creation). I use the automatical numbers of buttons - button 1 button 2 button 3 and so on. Let's think that the board is 8x8 (button at the bottom left is button 1, button at the top right is button 64).
Then when some of them is clicked, for example button 10 (which would usually be 1; 2) I find it's coordinates by dividing to 8 (x coordinate) and take it's remainder (x coordinate). For example button 19 (2; 3):
19:8 = 2 (remains 3)

Now I can rewrite my game (similar to chess) in Livecode, and play it with friends (on tablets). You can see the game (and some other games made by me in C# and AngelScript) here: http://www.youtube.com/watch?v=ux4bxc9L6L4 2:04. Most labels in the video is in my native language (Bulgarian) but still I think it's enough to understand what I am trying to achieve in Livecode. Thanks everyone for the help on this topic!

P.S LiveCode rules.

Aji
Posts: 18
Joined: Mon Sep 09, 2013 3:43 pm

Re: Array of Buttons?

Post by Aji » Thu Feb 25, 2016 11:33 pm

Hi,

Have there been any improvements or new ideas on how to build "an array of buttons" in newer versions of LiveCode?

Thank you much
..Aji

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9867
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Array of Buttons?

Post by FourthWorld » Thu Feb 25, 2016 11:46 pm

Aji wrote:Have there been any improvements or new ideas on how to build "an array of buttons" in newer versions of LiveCode?
Bernd's chessButtonArray example above seems pretty complete. What are you looking to build, and how might we help?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”