JavaScript Collision Detection doesn't seem to be working?

tanishalfelven

So I'm trying to set-up Collision Detection in my rendition of Alien Invaders. But it hardly works. And by hardly I mean I've never gotten a valid hit, or true return. I've even tried a bunch of different methods, but for whatever reason I haven't been able to get a correct answer.

The Collision Detection is for the Players bullet firing to the Alien ships, just for clarification, if it matters :D.

Here is the Collision Detection code: (I tried to make it error prone, in hope for a hit, although no method seems to work)

var isCollidingWithAlien = function(shot){
    var alienImg = document.getElementById('alien');
    for(var i = 0; i < aliens.length; i++){
        return shot.x               < aliens[i].x + alienImg.width  && 
        shot.x + shot.img.width     > aliens[i].x                   &&
        shot.y                      < aliens[i].y + alienImg.height && 
        shot.y + shot.img.height    > aliens[i].y;
    }
};

It's not that the code isn't being called, or some strange technical error. It just never returns true.

Also if you want to view all code, here is a JSFiddle

@Update: Been playing with the code, and it seems to register correctly for the first Alien created, the one on the top left if you look at the JSFiddle. Not sure why though

JohnnyAW

the problem is, you return the collision ONLY for the first alien in the list. you should return true if you get true for some alien, or return false, after all aliens doesn't collide:

var isCollidingWithAlien = function(shot){
    var alienImg = document.getElementById('alien');
    for(var i = 0; i < aliens.length; i++){
        var result = shot.x         < aliens[i].x + alienImg.width  && 
        shot.x + shot.img.width     > aliens[i].x                   &&
        shot.y                      < aliens[i].y + alienImg.height && 
        shot.y + shot.img.height    > aliens[i].y;
        if(result)
            return true;
    }
    return false;
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Collision Detection Javascript Not Working(rather Im not working)

From Dev

RedirectToAction doesn't seem to be working

From Dev

substring doesn't seem to be working

From Dev

Flock doesn't seem to be working

From Dev

RewriteRule doesn't seem to be working

From Dev

AlarmManager doesn't seem to be working

From Java

Collision detection in pygame not working

From Dev

Collision detection not working?

From Dev

if statement in JavaScript game not working as desired to allow for collision detection

From Dev

Why isn't my collision detection working properly in pygame?

From Dev

Why isn't my collision detection working in Phaser?

From Dev

Optimization of JavaScript collision detection

From Dev

Collision detection in javascript game

From Dev

Teamcity SetParameter doesn't seem to be working

From Dev

Equatable implementation doesn't seem working with generics

From Dev

reverse video doesn't seem to be working

From Dev

Java Multithreading doesn't seem to be correctly working

From Dev

ng-repeat doesn't seem to be working

From Dev

Python string comparison doesn't seem to be working

From Dev

array_unique doesn't seem to be working

From Dev

z-index doesn't seem to be working

From Dev

Objectify Delete doesn't seem to be working

From Dev

Method mousePressed() in Java doesn't seem to be working

From Dev

My onload listener doesn't seem to be working

From Dev

Simple PHP form doesn't seem to be working

From Dev

Nftables DNAT doesn't seem to be working

From Dev

totalSizeCap in logback doesn't seem to be working as expected

From Dev

Modal window doesn’t seem to be working

From Dev

useradd -g option doesn't seem to be working

Related Related

HotTag

Archive