Help me help this student!

My student is trying to touch the watermelon with the giraffe and have the watermelon move after being “eaten”/touched. The watermelon just moves super fast and we can’t seem to slow it down or get it to do what he wants when the giraffe touches it. Here is the code:

// Create your variables here
var character = createSprite(200, 200);
character.setAnimation(“giraffe”);
character.scale = 0.3;
var watermelon = createSprite(200, 200);
watermelon.setAnimation(“watermelon”);
watermelon.scale = 0.2;

// Create your sprites here

function draw() {
// draw background
background(“green”);
text(“Space to jump, Arrows to move,get the watermelon”, 0, 15);
moveCharacter();
resetwatermelon();
gravity();
drawSprites();
}

function moveCharacter(){
if(keyWentDown(“space”)){
character.velocityY -= 5;
}
if(keyDown(“LEFT_ARROW”)){
character.velocityX -= 0.2;
}
if(keyDown(“RIGHT_ARROW”)){
character.velocityX += 0.2;
}
}

function resetwatermelon(){
if(character.isTouching(watermelon)){
watermelon.x = randomNumber(0,400);
watermelon.y = randomNumber(0,400);
}
}

function gravity(){
character.velocityY += 0.2;
}

Could you post the link to the project here? It’s nice to be able to go in and just make it run to see what it’s doing. Thanks!

Hatif

Jan 21, 23:37 PST

Hi,
One of my stduents is not able to proceed with her project on AppLab. Everytime, she enters the code , the screen goes blank and nothing happens. I am sharing the link to her project below. Please help her out. Thanks


Sorry, I didn’t know where exactly to enter this post.

https://studio.code.org/projects/gamelab/K8YHgyeqfBywowFiuOh3ibrohNVW4jss7lDcSJ-wr_A

Hello Mindy!
The problem this student is experiencing is in the animation tab. It appears the student attempted to scale the giraffe and watermelon sprites down using the animation tools instead of adjusting the size in the code. This caused the collider (or hitbox) for both sprites to be the size they were before they were scaled down, which was very large, which caused the sprites to constantly collide.
An easy fix for this is to remove both sprites, and create new ones without scaling them down in the animation tab, and remember that the problem is not with the code.
Hope this solves your issue!
Thanks, have a great day! :slight_smile:

1 Like

Hello Hatif! To answer your question, your app is starting on the blank screen “boy13”. To fix this you would have to set the starting screen to “Homescreen” or whichever starting screen is needed.
Hope this helps, have a great day! :smiley:

1 Like

I have a group of students using game lab and their game keeps slowing down. We’ve played with frame rate, removing frame rate, moving things into separate functions. If anyone has any thoughts let me know because I can’t think of anything else to try.

Hi!

It looks like the program is slowing down because the student is creating so many sprites inside the draw loop. Every time the draw loop runs after the user chooses an animal, the function turtle or fox2 runs. These functions create sprites themselves, and then also call multiple other functions that are also creating sprites. At a normal framerate, the program is creating hundreds of sprites per second, all of which it has to keep track of. The faster the framerate, the faster it is creating more sprites and making an even bigger problem.

The student should create the sprites the program needs at the beginning of the program, then just move them around or adjust their properties inside the draw loop. Instead of creating new sprites to replace the ones that float off the screen, just reset those sprites back to the other side of the screen and have them float by again.

Elizabeth

1 Like

Thanks Elizabeth. That makes sense.