Score question/problem


#1

Here is a copy of the code. Everything on the game seems to be working except it will not add the score when the apples touch the dog. We cannot figure out what is wrong.

// Create your variables here
var cactus = createSprite(320, 320);
cactus.setAnimation(“cactus_1”);
cactus.scale = 0.8;
var dog = createSprite(55, 350);
dog.setAnimation(“dog”);
dog.scale = 0.2;
var apple = createSprite(0, 200);
apple.setAnimation(“apple_1”);
apple.scale = 0.4;
apple.velocityY = 5;
var score = 0;

// Create your sprites here

function draw() {
myFunction();
collision();
showapple();
if (score < 11) {
background1();
} else {
background2();
}
drawSprites();
if (keyDown(“right”)) {
dog.x = dog.x + 5;
}
if (keyDown(“left”)) {
dog.x = dog.x - 5;
}
showscore();

}
function showscore() {
fill(“black”);
textSize(20);
text(“score:”, 5, 5, 20, 20);
text(score, 70, 5, 20, 20);
}
function myFunction() {
if (dog.isTouching(apple)) {
apple.x = randomNumber(0, 400);
apple.y = 0;
}
}
function background1() {
background(“lightblue”);
fill(“green”);
rect(0, 370, 400, 30);

}
function background2() {
background(“red”);
fill(“green”);
rect(0, 370, 400, 30);

}
function showapple() {
if (apple.y > 410) {
apple.x = randomNumber(0, 400);
apple.y = 0;
}
}
function collision() {
if (apple.isTouching(dog)) {
score = score + 1;
}
}


#2

They may want to try something like this:
function playerTouchesObject(){
if(player.isTouching(snakeToken)){
setSnake();
score = score + 4;

The full code used for a game that increases the score when two sprites touch is found on lesson 22 bubble 11 under the example solution tab in the teacher panel.