Sign in to follow this  
Scourge80

Logic Issue in JS Game

Recommended Posts

I was working on a basic algorithm for detecting collision between a creep and game entities (enemy towers, enemy walls, etc.). Now, this works perfect when tested offline, but when I uploaded it to my site, it seems to have issues; creeps continue to move even while colliding. This is a simple Tower Defense-type game.

Collision update:

function CpuUpdate()
{
	//Update creeps
	for(var j = 0; j < CpuCreeps.length; j++)
	{
		//Check if we collide with an enemy tower
		if(CollidesWithTarget(CpuCreeps[j], P1Towers))
		{
			//Move toward the target
			CpuCreeps[j].MoveToTarget(CpuCreeps[j].Target);
			//Attack the target if we are in melee range
			if(CpuCreeps[j].CollidesWith(CpuCreeps[j].Target))
				CpuCreeps[j].Attack(CpuCreeps[j].Target);
		}
		//Check if we collide with a wall
		else if(CollidesWithTarget(CpuCreeps[j], P1Walls))
		{
			//Move toward the target
			CpuCreeps[j].MoveToTarget(CpuCreeps[j].Target);
			//Attack the target if we are in melee range
			if(CpuCreeps[j].CollidesWith(CpuCreeps[j].Target))
				CpuCreeps[j].Attack(CpuCreeps[j].Target);
		}
		//Move Creeps if we aren't colliding with enemies
		else
		{
			CpuCreeps[j].X--;
		}
	}
}

 

CollidesWithTarget(creep, targetGroup) method:

function CollidesWithTarget(creep, targetGroup)
{
	for(var k = 0; k < targetGroup.length; k++)
	{
		if(creep.CollidesWith(targetGroup[k].View))
		{
			//Set our target
			creep.Target = targetGroup[k];
			//Return true and stop checking for other towers
			return true;
			break;
		}
	}
	return false;
}

 

?

?

 To see the error:  http://dikaioti.byethost7.com/TowerDefense/

 

thank you for any feedback

(By the way, new to GameDev, if I made a mistake concerning rules, let me know so I can fix it)

Edited by Scourge80

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this