CSD Lesson 12 Bubble 3 Challenge (Changing Sprites - giraffe)

Hi - we are having a hard time understanding why this code won’t work for the challenge - if we use only one of the IF/THEN loops it does work , The link is above - here is the code:

var sprite = createSprite(200, 200);
sprite.setAnimation(“giraffe”);

function draw() {
background(“white”);
if(keyDown(“h”)) {
sprite.setAnimation(“hippo”);
}
else{
sprite.setAnimation(“giraffe”);}
if(keyDown(“p”)) {
sprite.setAnimation(“pig”);
}
else{
sprite.setAnimation(“giraffe”);}

if(keyDown(“r”)) {
sprite.setAnimation(“rabbit”);
}
else {
sprite.setAnimation(“giraffe”);}
drawSprites();
}

Hi @andrea.burgess,

If I hold down the “r” key, I can get it to turn into a rabbit, but as soon as I pick up my finger, it turns back into a giraffe. Hmm.

Remember that your code executes from top to bottom, so if anything on line n goes against anything on line (n+1), line (n+1) takes priority.

Have your student look at lines 18-22 in their code, and have them explain what this chunk is saying before the drawSprites(); call in line 23.

Hope that helps! If not, comment back here & we can keep working with you.
–Michael K.

I THIHNK I’ve got it…
J

Would I be accurate to tell my student that the animation is changing to the pig and the hippo when the associated keys are pressed, but that, due to the speed of the execution
we are only seeing the results of the 3rd conditional?

Hi @andrea.burgess,

You’re close. The second “if / else” in line 18 cancels out the one in 6-16 entirely, so even if the P button is pressed, it’ll get canceled out by the rabbit or giraffe. I’d have them check out using “if / else if / else if / else” statements, to sort of chain them all together to give multiple animation options.

Hope that helps,
–Michael K.