• Advertisement
Sign in to follow this  

Logic Issue in JS Game

This topic is 1844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement

it is kind of working, at least on one side the creeps stop at towers/walls and destroy them.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement