Unit 3 Lesson 22 debugging

My student is having trouble with his code. Please help. Below is what he wrote:

“Here is my code that doesn’t work. The coin(a ketchup packet) will not reappear at the end of the screen, even though it has the same code as the platform, which does reappear.” -J

// Create your variables here
var time = 0;
var score = 0;

// Create your sprites here
var player = createSprite(200, 50);
player.setAnimation(“burger”);
player.scale = 0.2;

var skull = createSprite(200, 350);
skull.setAnimation(“skull”);
skull.velocityX = 2;
skull.scale = 0.5;

var platform1 = createSprite(410, 200);
platform1.setAnimation(“platform1”);
platform1.velocityX = -7;

var coin = createSprite(410, 150);
coin.setAnimation(“coin”);
coin.scale = 0.1;
coin.velocityX = -8;

player.debug = false;
platform1.debug = false;
function draw() {
// draw background
if (time > 20) {
background2();
} else {
background1();
}
showVar();

// update sprites
spriteRotation();
playerControl();
gravity();

drawSprites();
}

// Create your functions here
function background1() {
background(“red”);
fill(“yellow”);
ellipse(100, 100, 10, 10);
ellipse(300, 300, 10, 10);
ellipse(200, 200, 10, 10);
ellipse(200, 100, 10, 10);
fill(“Orange”);
rect(0, 300, 400, 200);
fill(“red”);
rect(75, 325, 100, 5);
rect(250, 375, 100, 5);
}
function background2() {
background(“blue”);
fill(“red”);
ellipse(100, 100, 10, 10);
ellipse(300, 300, 10, 10);
ellipse(200, 200, 10, 10);
ellipse(200, 100, 10, 10);
fill(“Orange”);
rect(0, 300, 400, 200);
fill(“red”);
rect(75, 325, 100, 5);
rect(250, 375, 100, 5);
}
function showVar() {
time = time + 1;
score = score + 1;
fill(“White”);
textSize(20);
textFont(“Arial”);
text(time, 280, 25);
text("Time: ", 220, 25);
text(score, 120, 25);
text("Score: ", 50, 25);
}
function spriteRotation() {
if (skull.x == 410) {
skull.x = -10;
skull.y = 350;
}
if (platform1.x == -10) {
platform1.x = 410;
platform1.y = randomNumber(100, 300);
}
if (coin.x == -5) {
coin.x = 410;
coin.y = randomNumber(1, 300);
}
}
function playerControl() {
if (keyWentDown(“up”)) {
player.velocityY = player.velocityY - 10;
}
if (keyDown(“left”)) {
player.x = player.x - 5;
}
if (keyDown(“right”)) {
player.x = player.x + 5;
}
player.collide(platform1);
}
function gravity() {
player.velocityY = player.velocityY + 0.2;
}

I can look at this a little later, but is there any chance you can have them “share” .the project and send us the link? It is easier to debug if we have access to the actual program including the animations they are using.

thanks!

Mike

Sorry about that. This should be the link: https://studio.code.org/projects/gamelab/dd_v08gi0_dq0SH8qoUOr4j6Dpx1eCAyF_WLNb5JjiU

Fun take on the platform game! I do like it. So, I have located the problem. If I were his teacher, I would ask for him to explain to me exactly what causes the platform to reappear. In this case, the answer is in line 83. At some point, the platform x value is EXACTLY -10.

In the case of the coin, he is wanting it to reappear if it is EXACTLY -5. I think he got lucky with the platform because his velocity of -7 (moving 7 pixels to the left each time through the draw loop) does get him to -10. However, his coin velocity of -8 doesn’t achieve the same result.

So, I would have him rethink the operators he is using on lines 83 & 87 and maybe not require it to be such a precise x value. Maybe have him check to see if the x value is at or further to the left rather than right on the number?

Let us know how it goes!

Mike

Amazing what time will do. He actually figured out how to fix it on his own! Thank you though!

1 Like