Javascript RPG Collsion Detection

January 27, 2011 at 12:34:00
Specs: Windows XP
What is wrong with this collision detection?

<title>Javascript RPG</title>
<script language="JavaScript">

function moveObj(Char, Xpix, Ypix) 
   	obj = document.getElementById(Char);
	var px = parseInt( + Xpix;		
	var py = parseInt( + Ypix;
	var COL1 = parseInt( = 32Xpix;
	var COL2 = parseInt( = 32ypix; = px; = py;
	if( = COL1 && = COL2)
	alert('Collision alert!')

function ProcessKeypress(e)
	var Char = "Character";
	var moveBy = 32;

	if (e.keyCode) keycode=e.keyCode;

	if(ch=='a')		moveObj(Char, -moveBy, 0);
	else if(ch=='d') 	moveObj(Char, moveBy, 0);
	else if(ch=='w') 	moveObj(Char, 0, -moveBy);
	else if(ch=='s') 	moveObj(Char, 0, moveBy);

<body onKeyPress="ProcessKeypress(event);">


<img id="Level" style="left: 0px; top: 0px; position: absolute;" 

<img id="Character" style="left: 32px; top: 32px; position: absolute;" 


Everything else is working. :)

See More: Javascript RPG Collsion Detection

Report •

January 27, 2011 at 18:10:27
Hmm, well this doesn't fix it, but this code isn't valid -

var COL1 = parseInt( = 32Xpix;
var COL2 = parseInt( = 32ypix;

You're using two assignment operators - the second equals sign is invalid - should be an operator.

Also what is 32Xpix and 32ypix suppose to be? They're not defined...

if( = COL1 && = COL2)

Obj should be lower case

Also, you're only assigning the value of to COL1 (it should be ==, not just =), this conditional statement will always return true (unless maybe you couldn't allocate memory to copy COL1 into ... )


keycode is not defined - I guess you ment e.keyCode ... ?

And after all still doesn't work. The bounds checking method doesn't identify the edges properly (at least in my test).

I would use a library. GameQuery has bounds checking & lets you concentrate more on the actual game development than low level things like collision detection. Check out the demos:

Report •
Related Solutions

Ask Question