Click here to Skip to main content
15,116,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created a collision detection between Snake and BasicEnemy. I created a for loop to make five different enemies but the collision detection doesn't get called on any of the enemies that were created from the for loop. The collision only works with the one BasicEnemy object. Why isn't collision function being called for all of the enemies inside the array? Thank you.

Sketch.js
var snake;
var food;
var basicEnemy;
var scl = 20;
var enemies = [];

function setup() {
  createCanvas(600, 500);
  snake = new Snake();
  basicEnemy = new BasicEnemy();

  //** CREATE FIVE ENEMIES **
  for (var i = 0; i < 5; i++) {
    enemies[i] = new BasicEnemy();
    } 
  }

  // **FUNCTION WHEN SNAKE HITS ENEMY**
  function collision() {
    console.log("hit!");
  }

  function draw() {
    background(51);

    //Draw snake 
    snake.update();
    snake.show();

    //Draw basicEnemy
    basicEnemy.update();
    basicEnemy.show();

    //** LOOP THROUGH ENEMIES AND UPDATE AND SHOW ** 
    for (var i = 0; i < enemies.length; i++) {
     enemies[i].show();
     enemies[i].update();

     if (enemies[i].hits(snake)) {
      collision();
     }
  }
}


BasicEnemy.js

function BasicEnemy() {
  this.x = random(700);
  this.y = random(700);
  this.velX = 15;
  this.velY = 15;
}

//** FUNCTION TO CHECK IF ENEMY AND SNAKE ARE IN THE SAME LOCATION **
this.hits = function (pos) {
  var = d = dist(this.x, this.y, pos.x, pos.y);
   if(d < 1) {
     return true;
   } else {
     return false;
   }
}

this.show = function () {
  fill(255, 0, 100);
  rect(this.x, this.y, scl, scl);
} 


snake.js
function Snake() {
   this.x = 0;
   this.y = 0;
   this.xspeed = 1;
   this.yspeed = 0;

   this.update = function() {
     this.x = this.x + this.xspeed * scl;
     this.y = this.y + this.yspeed * scl;

     this.x = constrain(this.x, 0, width - scl);
     this.y = constrain(this.y, 0, height - scl);
  }

  this.show = function() {
    fill(255);
    rect(this.x, this.y, scl, scl);
  } 


What I have tried:

I have tired changing the code many times but I can seem to make it work. I am new to js so anything will be helpful. thank you.
Posted
Updated 13-Mar-17 14:33pm
v3

1 solution

Quote:
I have tired changing the code many times but I can seem to make it work.

Changing code without knowing what is wrong is just a waste of time.
The first step is to use the debugger to check what works as expected and what don't. Once you see what don't work and how it don't work, you can do cprrections.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900