I want to create a branching tree game such as the one in the attached gif. I want to create the tree structure dynamically, but Im not sure how. I would imagine it would be done unsing multi-dimensional arrays, which I have some experience of. But I cant work out how.
Can anyone suggest a starting point or a basic structure? I just need to be turned in the right direction and Ill be able to work out the rest myself!
Thanks in advance
thesquirrel
Last edited by thesquirrel; 01-22-2004 at 08:20 AM.
One way would be to, though some very fancy math, draw shapes starting from the last X,Y and then record the ending shapes X,Y and loop though. Remember to scale down the width as you go. Leaves would be random MC added in.
OR
Draw a generic branch and add it in based on the last X,Y. Rotate random deg. Record ending X,Y of added branch and loop. Maybe if you had 4 or 5 branches, maybe a branchMC with 5 frames. Randomly pick a frame so the brances aren't all the same.
Thanks for your reply. I probably didnt explain myself properly.
I dont want to recreate the physical shape of the tree and its leaves. I want to represent the structure of the questions and answers in some kind of data set, so I can build the textual parts of the tree dynamically.
I could then show the user the first question "Does it have more than 4 sides" and then dynamically build the "tree" from the information in the data set, depending on what their answers were to successive questions.
By using a data set, I could adapt the application to represent a different set of questions with a different structure.
What Im struggling with is creating a data set (im presuming a multi-dimensional array) that represents the structure of the information in the tree.
ok, I have been doing some stuff like this, and the solution I have used is to create a really complicated array. I have unfortunately had to to do this by hand, so to speak, but it does work.
The best way of solving this problem is to use some kind of program that allows you to draw your tree (some xml editors will allow this) then export that as an xml file which you can then read in flash, to create your 'tree'
Alternatively, excel files are apparently convertable to xml, I couldn't get this to work tho.
If I understand your question correctly, then you're wanting to create a standard tree data structure. An array is a BAD way to go for this. Instead, use the prototyping properties of Flash to construct your tree on the fly.
For instance, create a MC called a treeNode. Put one instance on screen, and call it "treeRoot". Create a question field for treeRoot like this:
Code:
treeRoot.question = "Does it have four sides";
Now, if you trace treeRoot.question, you'll get back "Does it have four sides".
This is the start of your tree, the root.
Now, create a recursive function called buildTree(node, x) where node is a treeNode and x is a depth. So, buildTree(treeRoot, 1) would build a tree with two children branching off of treeRoot.
If this is remotely close to what your wanting, let me know, and I'll flesh it out more if you need it.
FrankenMoro, I'm going to expand on your idea, as I don't think that thesquirrel has working knowledge of tree data structures.
What you want, thesquirrel, is called a binary tree. It consists of Nodes. Each node is basically an object that holds information, such as data, a pointer to it's left child, and a pointer to it's right child.
Flip that drawing of yours upside down. That's what binary trees look like.
A node for your tree would have the following fields
question, yesChild and noChild
What will happen is, your root (top) node will have the question "Does it have 4 sides?". The yesChild will be a node with the question "Does it have an even number of sides?" and the noChild will be a node with the question "Does it have straight sides?" and so on.
root = new Node("Does it have 4 sides?");
root.yesChild = new Node("Does it have an even number of sides?");
root.noChild = new Node("Does it have straight sides?");
...
...
You need to use something to check when you hit an answer. Maybe add two fields, isAnswer which is either true or false, and answer which is a string containing the answer.
So, you start with a variable called currentQuestion which is initially the root. then after each answer, you traverse the tree, checking for an answer.
FrankenMoro,
Yes I am interested in a further description of what you are talking about.
Essentially my array creates an explorer style tree menu system, and it does work, but the array is punishingly difficult, and the slightest error kills my swf.
Now as the interface is essentially a shell that loads in content, I gather that what I could do is have a php script that looks thru my content folder, and could generates my array. This has 2 problems, one I'm running from cd, not inter/intra net (this may not be a problem but I know nothing about php), two I don't have a clue about php.
Your method looks like it could offer a clear solution.
Thanks for your replies. This got put to one side for a week or so and I have only just returned to it.
yasunobu13, I have created the binary tree according to your advice, but Im unsure how to then turn the data into a physical representation of the tree. More specifically, Im unsure how to move through the data tree. Im presuming I will be able to loop through it somehow to access the data in each node. But I can't work out how.