# What’s wrong with my code?

#24

@Revilo I didn’t have a chance to respond about problem # 2 yesterday, but I tried troubleshooting it as well. The problem is definitely the Hopscotch bug with bumps not working correctly.

The when Self bumps Platform should be ok since Hopscotch only has to evaluate 2 objects for the collision. The When Self bumps Anything (note that Anything also includes the Platform) fails after there’s more than something like 7 or 8 objects. I think Anything should have been Square since bumping the Platform fires both rules (that would check for collisions only with any other Square). But the Hopscotch bump bug still is a problem.

As an aside, why do you need different responses for bumping the platform vs another square? The falling square stops and the nexts starts falling in either case, right?

Here’s a potential simplification:
If (x position < 121 or x position > 909) and y position < 150
Could be just:
If y position < 150
since the blocks in the middle can’t fall to a y position of 150 without at least landing on the platform.

—————
And last but not least, I would suggest that using a math conditional to replace the Bumps is the best course of action, but the sqrt(deltaX^2 + deltaY^2) formula creates a circular contact zone. That’s not what you want here, so instead you’d want to do this:

A square at 100% is 68 x 68 pixels, so from the center to an edge is 34. The centers of two adjacent squares, when touching are 34 to the edge of each or 34 + 34. We just check the difference of both the x and y positions. And we can factor in the object size so it still works if you set the size to something other than 100%.

If Square1 X - Square2 X < 68 AND Square1 Y - Square2 Y < 68

Unfortunately this too has a problem. The problem is the square clones can’t “see” the properties of the other square clones.

This can be worked around but I’ll have to write more later. Gotta go for now…

#25

No that’s fine, I wasn’t available then either way.

I would’ve put square if I knew then that putting square wouldn’t just refer to the original object and would work with the other clones too.

I don’t know… either I was being really thick or I think there was a problem with the square overlapping the platform or something probably the former.
I agree to use the math conditional in place of the When self bumps anything but to keep the when self bumps platform as this doesn’t seem to be a problem and to also limit the problem with clones not being able to recognise other blocks ( apart from the original? )

Shouldn’t Square1 Y - Square2 Y = 68 because if lesser than then squares would overlap.

This will be replaced with the former as suggested
Edit : This actually simplified the code a lot

To sum up Problem 1 has been solved.
Leaving Problem 2 which is caused by the rule ‘ When self bumps anything’ that sets self land to 2. This rule will be replaced by a when game starts ( ? ) with a maths conditional that will if true set self to 2 however this maths conditional is only true when a clone lands on the original object and cannot recognise its other clones. ( could be solved by clone ids? )

#26

New problem/old problem (evolved?)
The code was simplified with the new When self y position < 150 but this happens now…
I’ve also done some tests. In the above project I’ve placed the square at the right end of the platform. The square goes invisible as it’s supposed to and then creates a clone however these clones act like they do in problem 2.
If you move the square back to the middle (ish) of the platform then you’ll see that any squares that appear above another and fall will go through the square and not stack as I have removed the Set self land to 2 block in the when self bumps anything rule to see what would happen.
Problem 2 still occurs despite the ‘removal’ of Set self land to 2 meaning not just the when self y position < 150 rule but also the when self bumps platform rule. Changing the clone’s set position block to x position random 300 to width - 300 further demonstrates this.
To sum up all of the rules that set self land to a number ( not just the when self bumps anything ) causes problem 2 and makes the game lag
Perhaps it’s just acting up on my end, I’ll alert you of any new developments.

#27

In the above project I’ve placed the square at the right end of the platform. The square goes invisible as it’s supposed to and then creates a clone however these clones act like they do in problem 2.

This is a different problem. In ‘Land Wave’, you just left out the Repeat Forever after Create a Clone (not a particularly efficient way to code, but it seems to get the job done) so since ‘Fall’ also has a Repeat Forever it keeps making clones.

——————

Before we get to fixing the When self bumps Anything rule, let’s look at the code within it.

Since a square bumping the platform fired both rules [When self bumps Platform] and [When self bumps Anything], you went through some gyrations with Ready and some conditionals.

If/when we change that from bumps Anything to bumps Square, you can remove everything else and just leave the [Set self Land to 2]

#28

I’ve changed it as suggested and now the only problem left is problem 2.
This is the updated link for the project.

I’ve removed the increase self land by -( 3,2, or ,1 ) as I’ve realised that it doesn’t do anything.

#29

Hi @Revilo

Fixing your “problem 2” isn’t super straight forward. Since clones can’t “see” the properties (x/y positions) of other clones, we’d have to make some significant changes to the way you’ve coded the project. For only the falling square, on every frame you’d have to not only change the Y position, but also “advertise” that clone’s x & y position by setting them to global variables. Then the other clones (to keep lag from being dramatic, the other clones have to do this) could do the calculation to determine if they are touching the clone that’s falling. Then they’d have to set another global variable that would trigger a rule or conditional for the falling clone so it would know to stop falling and make another clone to start falling.

Unfortunately, the way your project’s coded isn’t done in a way that would make this easy.

So… since we need to make some significant changes anyway, I’m going to propose an alternate approach.

As it turns out, we don’t need to use the math based approach after all. (@Petrichor, just fyi too). The Hopscotch Player’s bug with bumps is for clones bumping clones. So, if the falling square is not a clone of the others then the Bump rules will work right.

To make that work, I’ve added a new square called Falling Square. It’s always falling. When a bump is detected, a flag gets set that causes the other Square object to set it’s location to that of the Falling Square and become visible. The Falling Square then resets its position to the top and continues to fall. That gives the illusion that the Falling Square landed and a new one started falling.

One last wrinkle in how that works is that there’s always one “static” Square that’s invisible. The rest have become visible as landed squares. When a bump is detected, only the most recent of the Square clones (the invisible one) can react. We do this by checking

``````If (self) Clone Index = (Square) Total Clones
``````

Try this version & let me know if you have any questions:
https://c.gethopscotch.com/p/zme2dv8wq

#30

Wow that’s perfect!
You’ve fixed everything. I understand all the code and it’s much more simpler
Thanks so much! ‍♂️

#31

Hey @ThinBuffalo

You’re good at maths and I think my latest problem could be solved with it

So the solution you created works without the lag but now it overlaps and kind of floats. I’ve fixed the squares that land on the platform in the first project by adding subtract ‘ …FS… ’ Y position by 7. However I’m sure there’s an easier way to do this. Plus even after doing this squares above still overlap.

In the second project I’ve placed the squares at it it’s posible y positions. Each squares y position is 69 pixels apart. Squares in the former project fall by -7 until they land which may be why it doesn’t quite fit.