Collision and is touching not always working

So our class is in the middle of recreating the first level in Mario. Our code is a little messy but works for the most part. The problem we are having is that the question boxs work sometimes but not others. Box 8, 9 and 10 are not working at all and box 7 and 4 only work with big mario not small mario. Any help would be appreciated, thanks!

**Link to the project or level: Game Lab - Code.org

**What I expect to happen: bricks or question box to turn to empty box when mario jumps to hit it

**What actually happens: nothing

I’ll continue to look at this, but I’m wondering what you have tried so far to make them work.

Have you looked at the code for the boxes that work and compared it to the code for the boxes that don’t work? Sometimes comparing the buggy part of the code to similar code that works helps to see what needs to be done. That’s the first thing that I’ll do to help see where the problem is.

I think it’s great your class is taking this on. It looks amazing so far. Make sure to be aware of copyright rules.

Suggestions for coding so far. For box4 a few things I noticed - The setanimation emptybox is in the second If statement and I think you want it in the first one like the other functions. Also there is a draw sprites command that I don’t think you want in the box4 function.

The function for checking box10 (line987) isn’t called anywhere in the program. There are other functions also not called that could be part of it.

I would also check for typos, because you have so many variables with similar names. For example, make sure it’s not box6, when you want box7, or capitals and lower case letters, that kind of thing.

Once you clean that up a bit and check those things out, let me know how it goes and if there are some specific parts you need more assistance to debug.

No luck so far. The weird thing is we copied and pasted the codes for all the boxes. What we are finding is that the boxes that are higher in the sky only work when Mario is big, except for Box8 and Box9 that don’t work at all.

I think we could better help you if we had the following information for each part you’d like us to look at and help you debug.
Feel free to delete everything in brackets before posting your request for debugging help.]

What I expect to happen: [replace with a detailed description of the behavior you’re trying to create]
What actually happens: [replace with a detailed description of what actually happens when the code runs including any errors or unexpected behavior]
What I’ve tried: [replace with a detailed description of what you’ve already tried to do to solve the problem]

What I expect to happen: When Mario jumps and hits a question mark box it turns into a blank box. We have not coded all the coins yet so don’t worry about those.
What actually happens: Mario jumps and hits a question box it turns into an empty box most of the time, but for a few boxes, box4, box7, box8 (which is actually bricks not a question box) and box9(same as box 8) and bo10. Now box 4 and box7 both work when mario eats a mushroom and becomes big mario.
What I’ve tried: [most of the boxes were copied and pasted from the originally working boxes, so we went through the code to make sure there were no typos a lot of times. We also erased it completely and retyped the boxes that weren’t working. We also changed the colliders on the objects to see if that was an issue. One thing that did work was when I took off the mario.collide(box#) it worked but then Mario would just jump through the box and not be able to stand on top of them]

@nspry,

There’s a lot going on here. I don’t know that I have a definitive answer for you, but I played with Box4 for a good 30 minutes or so and I could get it to work with small Mario in certain circumstances. Usually, I had to open one of the boxes below it first and reveal the mushroom. Then, I could go up top and jump over the mushroom (without eating it) and I would sometimes get Box4 to change.

I added some console.log statements inside the open4 function to see if it was getting called at all. It is getting called, but my current theory is that for some reason, right when you go to jump it isn’t active. I don’t know if I’m explaining that very well, but when I put console.log statements inside of open4( ), and I put watchers on the q4 and Mario.y values, I could see that although the q4 and Mario.y conditions were being met, the console.log statements weren’t running, so the program was “at that very instant” not running open4.

Screen Shot 2022-01-06 at 11.02.49 AM

Screen Shot 2022-01-06 at 11.02.44 AM

When I let it sit there and when I revealed the mushroom, eventually, I could get it to trigger.

As for a solution … I haven’t tried this, but is there a way you could perhaps rethink how and when you are calling the functions? Maybe there are so many functions running that it is having a hard time… I am not an expert at this, but maybe open4 could only be called when Mario.x is above a certain number, but that requires a different conditional to check for that, so I’m not 100% sure that would work.

Overall, unfortunately, I think the best solution would involve refactoring and simplifying the code (which would be a ton of work) to eliminate as much repetitious code as possible.

Instead of having so many different box sprites, for example, create a group called box and add sprites to that group. Then there are less collision functions to account 4 as it only has to check for collisions with a group and not each sprite in the group.

I realize this involves having to redo a ton of work and also to learn how groups work. There may be another way and there are definitely smarter people than me who visit this board and who may be able to help AND the scope of this project goes beyond any I have personally attempted, so take my advice based on that, but that’s just my opinion after having played with it for 30 minutes or so.

Sorry I can’t be of more help. If I think of anything else, I’ll be sure to chime in.

Good luck as you continue to debug!

Mike

1 Like

I am also having problems when trying to go down pipe4. It works perfect for small mario but large mario it doesnt work.

I am also trying the groups thing and I don’t know how if I make a group to change individual animations. If I make the group then when I hit the first box they all change to empty boxes.

You may benefit from watching this video (and part 1 from this same youTube channel). He shows how to use a for loop to detect collision and determine which sprite in the group is the one that has collided with the other sprite. It starts around 4 minutes in.

Mike

1 Like