Help with student code

I have a student working on the interactive card and we can’t figure out why the spaceship in his program gets smaller and then comes back. He is using the counter pattern with scale and it all seems right to me. Any help would be appreciated! Here is his code:

var sky = createSprite(200, 200);
sky.setAnimation(“sky”);
var star1 = createSprite(40, 40);
star1.setAnimation(“star1”);
star1.scale = 0.2;
var star2 = createSprite(320, 80);
star2.setAnimation(“star2”);
star2.scale = 0.2;
var star3 = createSprite(170, 130);
star3.setAnimation(“star3”);
star3.scale = 0.2;
var star4 = createSprite(60, 130);
star4.setAnimation(“star4”);
star4.scale = 0.15;
var star5 = createSprite(230, 80);
star5.setAnimation(“star5”);
star5.scale = 0.15;
var ufo = createSprite(200, 200);
ufo.setAnimation(“ufo”);
ufo.visible = false;
ufo.scale = 0.7;
var star_count = 0;
function draw() {
if (mousePressedOver(star1) && mouseWentDown(“leftButton”)) {
star1.visible = false;
star_count = star_count + 1;
}
if (mousePressedOver(star2) && mouseWentDown(“leftButton”)) {
star2.visible = false;
star_count = star_count + 1;
}
if (mousePressedOver(star3) && mouseWentDown(“leftButton”)) {
star3.visible = false;
star_count = star_count + 1;
}
if (mousePressedOver(star4) && mouseWentDown(“leftButton”)) {
star4.visible = false;
star_count = star_count + 1;
}
if (mousePressedOver(star5) && mouseWentDown(“leftButton”)) {
star5.visible = false;
star_count = star_count + 1;
}
if (star_count >= 5) {
ufo.visible = true;
ufo.rotation = randomNumber(-10, 10);
ufo.scale = ufo.scale - 0.1;
}
drawSprites();
textSize(30);
fill(“red”);
text(“click the stars for a suprise”, 10, 300);
}

Hi @buyskej ,

Thanks for posting the code here. Would it be possible for you to share the program itself so we get the associated sprites & animations?

Also, it would be helpful if you tell us what you’d like to have happen. We know that you said the spaceship gets smaller and comes back. Is it supposed to disappear entirely? Reappear at full size rather than growing?

Thank you!

1 Like

My initial thought without seeing the project is that the scale block is causing the ufo to “flip inside-out” once it gets smaller than zero. You would think logically that once the size gets to 0, it would disappear, however, a negative size (caused by the scale continuing to decrease) will actually cause the sprite to start growing again, but flipped.

See this previous thread for more information.

Mike

2 Likes

@mkmietowicz Thank you for the response, Michael. After seeing the response from @mwood, I think I know what to do. The sprite was supposed to get smaller and disappear, so I think the student could set up a conditional that checks if the sprite’s scale is <0 the visibility would be false.

1 Like

Thank you! I did not know that about the scale of sprites and how they flip and enlarge. I think a simple if statement checking the sprite’s scale should take care of this issue.

3 Likes