Help for a student - debugging and animations

Student is working on Unit 3: Lesson 14 - Interactive ard, she is having issues setting her cloud to shake and have rain drops coming down. Please help. Thank you

var flower = createSprite(350, 300);
flower.setAnimation(“flower.jpg_1”);
flower.scale = 0.5;
var flower2 = createSprite(35, 300);
flower2.setAnimation(“flower.jpg_1_copy_2”);
flower2.scale = 0.5;
var cloud = createSprite(200, 60);
cloud.setAnimation(“cloud_1”);
cloud.scale = 0.5;
function draw() {
background(“skyblue”);
fill(“green”);
rect(0, 300, 400, 200);
fill(“yellow”);
textSize(25);
textFont(“sans serif”);
text(“Hello welcome to my graden”, 25, 250);
drawSprites();
//
}
if (mouseWentDown(“leftButton”)) {
cloud.rotation = randomNumber(100, 200);
}

Hello @crosado,
We would love to help you with your student’s work. We need a little more information though. We need a link to the student’s work using the Share button on his/her account so we can see the project as it is. Also, if you could include what you expected to happen and what is happening, this would help. Once we have these pieces of information, we’ll be glad to help you out.
~ Elizabeth

1 Like

Student is trying to create their own scene and would like the cloud to shake or move and possible have rain drops drop constantly when a key is pressed. Currently student is stuck as to how to get the cloud to shake or move

So there’s two things here:

  • To get the cloud to shake or move, you’ll need to set its x or y to a random number in a small range (example: cloud.x = randomNumber(195, 205) if you want it to be centered on x = 200 with the ability to move 5 pixels to either side left or right. This might be jumpy but there are other, more complex ways to do it (a logic model where x = x + 1 for certain values of x, until a certain point, and then x = x - 1 until it’s back to the start, perhaps).
  • Each raindrop should be its own sprite that gets its own y velocity toward the bottom of the screen.

As @edavis said, if you give a link to the student’s project, that will greatly help us assist you with this student’s project.

Happy coding!
–Michael K.

2 Likes

One thing I noticed just by looking at the code is that the last few lines:

if (mouseWentDown(“leftButton”)) {
cloud.rotation = randomNumber(100, 200);
}

appear to be outside of the draw loop. They need to be put in the draw loop in order for the program to continually monitor if the mouse button has been pressed. Doing that alone should fix the issue of the cloud not shaking. Then, a rotation of 100 to 200 (last line) may not quite be what your student is after … usually, it would be a rotation between -10 and 10 (or something along those lines depending on how much rotation is desired).

I don’t see that they have started coding the raindrops yet, but once they do, don’t hesitate to check back in with us if you want us to look at that!

happy coding!

Mike

1 Like