# Help with getting the X value

**URL:** <https://forum.makecode.com/t/help-with-getting-the-x-value/37518>\
**Category:** Help\
**Created:** [June 24, 2025, 5:43pm UTC](https://forum.makecode.com/t/help-with-getting-the-x-value/37518 "2025-06-24T17:43:57Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![butterflymoon](https://avatars.discourse-cdn.com/v4/letter/b/ee59a6/32.png) [@butterflymoon](https://forum.makecode.com/u/butterflymoon)\
**Post date:** [June 24, 2025, 5:43pm UTC](https://forum.makecode.com/t/help-with-getting-the-x-value/37518/1 "2025-06-24T17:43:57Z")

</div>

Here’s my game: [https://makecode.com/\_Krb8t8iLoJKE](https://makecode.com/_Krb8t8iLoJKE)

I need help because I don’t know how to get the x values that I need for each branch. My game uses a binary tree concept, where the tree is supposed to grow up and out. But I don’t know how to get the required x value for the branches each time.

What I am trying right now is saving all the branches x values to an array, and saving the amount of branches in each row to another array, then using this to find the x value of the ‘parent’ branch. Then I can plus or minus a certain amount to get the right x, but this is difficult and takes a long time and is really hard to get right.

Someone please help I’m really confused D:

---

<div class="post-metadata">

**Author:** ![a012](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/a012/32/24099_2.png) [@a012](https://forum.makecode.com/u/a012)\
**Post date:** [August 26, 2025, 2:17am UTC](https://forum.makecode.com/t/help-with-getting-the-x-value/37518/2 "2025-08-26T02:17:04Z")

</div>

It’s great that you’re looking closely at the code to understand why things might not be working as expected! Finding bugs is like solving a puzzle, and it helps you learn a lot about how games are built.

Let’s break down some of the reasons your game might not be growing the tree exactly how you want it to, focusing on the parts that control where branches appear.

* * *

### The Bugs Explained 🐛

The main issues in your code that are stopping your tree from growing “up and out” dynamically are related to how you keep track of where the tree can grow from and how you calculate new branch positions.

* * *

### 1. The Tree Only Grows From the Same Spot 📍

- **The Problem:** Look at your `doTurn` function. Inside it, you have `makeLeftBranch(84)` and `makeRightBranch(76)`. The numbers `84` and `76` are always the same. This means no matter how many times you “water” the tree, new branches will always try to come out from roughly the same two `x` positions, close to the original root.
- **Why it’s a bug:** For your tree to grow “up and out,” new branches need to sprout from the _ends_ of the existing branches (where the “knobs” are). Your current code always makes them appear near the root, not from the tips of the growing tree.

* * *

### 2. Not Tracking Where the Tree Can Grow Next 🌱

- **The Problem:** You have a `knobPositions` array, which seems like it’s meant to hold the `x` values of all the places new branches can grow from. However, in `initialiseGame`, you set `knobPositions = [80]`, and then you never add new `knob` sprites’ positions to this array when `makeKnobLeft` or `makeKnobRight` are called.
- **Why it’s a bug:** If `knobPositions` only ever contains `[80]`, then `doTurn` (if it were designed to use this array) would only ever try to grow from that single `x` coordinate. The game needs to know about _all_ the new knobs that appear on the tree so it can decide to grow from them in the next turn.

* * *

### 3. `prev_levely` Stays the Same 📏

- **The Problem:** You set `prev_levely = 105` only once in `initialiseGame`. Then, when you create branches in `makeLeftBranch` and `makeRightBranch`, you use `prev_levely - 5`. This means all new branches will always be created at the same `y` position relative to that initial `prev_levely`.
- **Why it’s a bug:** For the tree to grow “up” (get taller), the `y` value where new branches are created needs to decrease each time you make a new level of branches. If `prev_levely` never changes, the tree will just grow horizontally at the same height, instead of getting taller.

* * *

### 4. `SpriteKind.Knob` is Missing 🧩

- **The Problem:** You have `SpriteKind.Root`, `SpriteKind.Tree`, `SpriteKind.Apple`, and `SpriteKind.Leaf`. When you create a knob sprite, you assign it `SpriteKind.Tree`.
- **Why it’s a bug:** While not a “crash” bug, it makes your code harder to manage. If knobs are just `SpriteKind.Tree`, it’s hard to tell them apart from the actual branch segments. Giving knobs their own `SpriteKind.Knob` would make it much easier to find _just_ the knobs when you want to decide where to grow next or attach an apple/leaf.

* * *

### 5. Leaves Don’t Stop Growth Yet 🛑

- **The Problem:** Your `makeAttachment` function can add a `leaf` to a knob, and `cutLeaf` removes them. You also mention that “Leaves also stop that knob from growing more branches.” However, in your `doTurn` function, there isn’t any code that actually checks if a knob has a leaf before making new branches from it. It just randomly creates branches from the fixed positions.
- **Why it’s a bug:** The game isn’t following its own rule! If a leaf is supposed to block growth, the `doTurn` function needs to know which knobs have leaves and skip growing new branches from those specific ones.

* * *

### How to Fix These (The Idea) 🤔

To fix these, you’d want to:

1. **Track Knobs, not just `x` values:** When a new branch is made, its knob needs to be added to a list of “growable” points, and that list should store the actual `Sprite` objects of the knobs, not just their `x` values.
2. **Grow from _all_ growable knobs:** In `doTurn`, instead of using fixed numbers like `84` and `76`, you’d go through your list of “growable” knob sprites. For each knob, you’d calculate a _new_ `x` and `y` position for its children branches, relative to the knob’s own position.
3. **Update the `y` level:** You’d need a variable that keeps track of the current tree’s height, and each turn, you’d use that to make the `y` position for new branches a little higher up on the screen.
4. **Check for leaves:** Before growing from a knob, you’d check if a `leaf` sprite is currently overlapping or attached to that specific knob. If it is, you’d skip growing new branches from that spot.

These changes would make your tree grow beautifully, branching out and getting taller with each turn, just like you imagined! I fixed it here:Here you go, the game is here and fixed [https://arcade.makecode.com/S51788-82237-27962-47908](https://arcade.makecode.com/S51788-82237-27962-47908)
