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;
}