# Student gets Overlap error with the following code

**URL:** <https://forum.code.org/t/student-gets-overlap-error-with-the-following-code/23781>\
**Category:** Unit and Lesson Discussion\
**Tags:** csd-unit-3, csd-unit-3-lesson-22\
**Created:** [January 4, 2019, 2:32pm UTC](https://forum.code.org/t/student-gets-overlap-error-with-the-following-code/23781 "2019-01-04T14:32:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![adam.cooper](https://avatars.discourse-cdn.com/v4/letter/a/7c8e57/32.png) [@adam.cooper](https://forum.code.org/u/adam.cooper)\
**Post date:** [January 4, 2019, 2:32pm UTC](https://forum.code.org/t/student-gets-overlap-error-with-the-following-code/23781/1 "2019-01-04T14:32:04Z")

</div>

// Create your variables here  
var score = 0;  
var chick\_1 = createSprite(20, 300);  
chick\_1.setAnimation(“chick\_1”);  
chick\_1.scale = 0.2;  
var car\_red\_1 = createSprite(156, 440);  
car\_red\_1.velocityY = -3;  
car\_red\_1.setAnimation(“car\_red\_1”);  
car\_red\_1.displace(car\_yellow\_1);  
var motorcycle\_1 = createSprite(156, 700);  
motorcycle\_1.setAnimation(“motorcycle\_1”);  
motorcycle\_1.velocityY = -3.3;  
motorcycle\_1.displace(car\_red\_1);  
var car\_green\_1 = createSprite(234, 850);  
car\_green\_1.setAnimation(“car\_green\_1”);  
car\_green\_1.velocityY = -1.9;  
var car\_yellow\_1 = createSprite(156, 960);  
car\_yellow\_1.setAnimation(“car\_yellow\_1”);  
car\_yellow\_1.velocityY = -3;  
car\_yellow\_1.displace(motorcycle\_1);

// Create your sprites here

function draw() {  
// draw background  
road();  
stripe();

// update sprites  
if (car\_red\_1.y \< -250) {  
car\_red\_1.y = 560;  
}  
if (car\_black\_1.y \< -250) {  
car\_black\_1.y = 700;  
}  
if (motorcycle\_1.y \< -250) {  
motorcycle\_1.y = 820;  
}  
if (car\_green\_1.y \< -250) {  
car\_green\_1.y = 970;  
}  
if (car\_yellow\_1.y \< -250) {  
car\_yellow\_1.y = 1080;  
}

drawSprites();  
showScore();  
}

// Create your functions here  
function road() {  
background(“green”);  
rect(125, 0, 150, 40000);  
fill(“grey”);  
}  
function stripe() {  
rect(192, 0, 12, 4000);  
fill(“yellow”);  
}  
function showScore() {  
fill(“black”);  
textSize(15);  
text("Score: ", 0, 15);  
text(score, 60, 15);  
}

It says that the error is caused here: motorcycle\_1.setAnimation(“motorcycle\_1”);

---

<div class="post-metadata">

**Author:** ![ltoal](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/ltoal/32/5449_2.png) [@ltoal](https://forum.code.org/u/ltoal)\
**Post date:** [January 4, 2019, 8:20pm UTC](https://forum.code.org/t/student-gets-overlap-error-with-the-following-code/23781/2 "2019-01-04T20:20:32Z")

</div>

Hi,

The issue has to do with the order in which the student is declaring/referencing the variables. When the code says, for example, `car_red_1.displace(car_yellow_1);`, there’s an error because `car_yellow_1` doesn’t actually exist until it’s declared a few lines later. So the code is checking for overlap (in order to do the `displace`) between a sprite and a nonexistent object.

Try creating all the variables and setting the animations first, e.g.

```auto
var car_yellow_1 = createSprite(156, 960);
car_yellow_1.setAnimation("car_yellow_1");

var car_red_1 = createSprite(156, 440);
car_red_1.setAnimation("car_red_1");

var motorcycle_1 = createSprite(156, 700);
motorcycle_1.setAnimation("motorcycle_1");

```

And _then_ doing the velocity/displace code for each sprite.

Best,  
Laurence
