ERROR: Line: 38: TypeError: rat.isTouching is not a function

Code.org This is the code.

The following link you gave just shows the template… to further debug your issue we’d need the actual source code your working with

Varrience

If you are in the activity you’re working on, select the Share button on the upper left to get the code specific to this activity. Also include what you expected to happen and what is happening instead. This will help us better support you with your debugging.

Here is the code.

//GAME SETUP
// Create the sprites
// set velocity for the obstacle and the target
var street = createSprite(200, 200);
street.setAnimation(“street”);
var pizza = createSprite(50, randomNumber(75, 339));
pizza.setAnimation(“pizza”);
pizza.velocityX = +3;
var rat = createSprite(45, 339);
rat.setAnimation(“rat”);
var alligator = createSprite(100, 339);
alligator.setAnimation(“alligator”);
alligator.velocityX = +3;

//// jumping

//create the variables
var score = 0;
var health = 10;

function draw() {

// SPRITE INTERACTIONS
// if the player touches the obstacle
// the health goes down, and the obstacle turns
if (rat.isTouching(alligator)) {
alligator.rotation = 30;
rat.velocityX = 0;
rat = health - 1;
} else {
alligator.rotation = 0;
}
// if the player touches the target
// the score goes up, the target resets

//if (rat.isTouching(pizza)) {
// pizza.x = randomNumber(50, 100);
// pizza.y = randomNumber(50, 100);
//score = score + 2;
//}
if (rat.isTouching(pizza)) {
pizza.x = randomNumber(20, 100);
pizza.y = randomNumber(50, 339);
score = score + 1;
}
//if (dolphin2.isTouching(Jellyfish)) {
//Jellyfish.x = randomNumber(420, 280);
// Jellyfish.y = randomNumber(50, 350);
// score = score + 2;
//}
// JUMPING
// if the player has reached the ground
// stop moving down
if (rat.y > 300) {
if (keyDown(“up”)) {
rat.velocityY = -4;
} else {
rat.velocityY = 0;
}
}

// if the player presses the up arrow
// start moving up

// if the player reaches the top of the jump
// start moving down
if (rat.y < 50) {
rat.velocityY = 3;
}

// LOOPING
// if the obstacle has gone off the left hand side of the screen,
// move it to the right hand side of the screen
if (pizza.x > 400) {
pizza.x = 50;
pizza.y = randomNumber(75, 339);
}
if (alligator.x > 400) {
alligator.x = 50;
}

// if the target has gone off the left hand side of the screen,
// move it to the right hand side of the screen

// DRAW SPRITES
drawSprites();

// SCOREBOARD
// add scoreboard and health meter
fill(“black”);
textSize(20);
text(“Health:”, 30, 30);
text (health, 350, 30);
text(“score”, 250, 30);
text(score, 350, 30);

// GAME OVER
// if health runs out
// show Game over
if (health < 0) {
background(“black”);
fill(“red”);
textSize(50);
text(“GAME OVER:(” , 40, 200);
} else if ((score > 50)) {
fill(“green”);
textSize(60);
text(“YOU WIN!”, 40, 200);
}
}

@bjferguson,

You may have already figured this out, but in the line of code above, you changed rat from being a sprite to being a number. This is why it couldn’t run the collision check. Rat itself shouldn’t be coerced to a number.

Mike