[Browser MMORPG] isInRange function

Place to place any code snippets, completed games, or even uncompleted games for IR users to use.
Post Reply
toby
Posts: 3
Joined: Wed May 04, 2011 2:24 pm

[Browser MMORPG] isInRange function

Post by toby »

Description:
This function can be used to tell if one div is in range of another div... both horizontally and vertically!
Took 3 days to figure out, please don't be harsh lmao.

Usage:
isInRange("div232","div235",4);
This would return true.
isInRange("div230","div235",4);
This would return false.

Code:
(paste into netmap.js)
function isInRange(tag1,tag2,sqs) {

/*
If you go up one row in the map, the div tag is += 30 automatically
Likewise, if you go down, the div tag is -= 30, therefore,
the offset is 30
*/

var offset = 30;

//Getting the number on its own; changes 'div213' to '213'
var tag_1 = parseInt(tag1.replace("div", ""));
var tag_2 = parseInt(tag2.replace("div", ""));
var totalOffset = offset * sqs; //The total offset (furthest away)
var currentX = tag_1 - totalOffset; //Furthest north directly above tag1 (default)

//How many loops to go through, e.g sqs=2 .. 2 rows above , 2 below and the 1 middle row
var loops2 = sqs * 2; //
var total_loops = loops2 += 1; //Add on the row tag1 is on (mid row)

//Creating some default variables which are used inside the loop
var minX = 0;
var maxX = 0;
var i=0;
for (i=0;i<=total_loops;i++)
{
minX = currentX - sqs; //The smallest in range for this row
maxX = currentX + sqs; //The largest in range for this row

if(tag_2 >= minX && tag_2 <= maxX)
{
return true; //Houston, we are in range
}

currentX = currentX + offset; //Go up one row for the next loop (30 divs)
}

return false; //Houston, we have a problem
}
I will be releasing a lot more of my code from now on.

I was going to release keyboard movement, but it was already done by somebody.

I also promise to release my combat, chat and possibly trading systems when I finish them.

Toby
Mustardy
Posts: 15
Joined: Thu May 12, 2011 1:53 am

Re: [Browser MMORPG] isInRange function

Post by Mustardy »

so what does this do lol?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Browser MMORPG] isInRange function

Post by Jackolantern »

What do you mean by "the div is in range of another div"?
The indelible lord of tl;dr
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: [Browser MMORPG] isInRange function

Post by Torniquet »

by the looks of it, its for a real time rpg. it detects if div a (the player) is in range of div b (the enemy) by 4 squares (30px by 30px?)

if true then action, if false no action.

well atleast thats what i took from it lol.
New Site Coming Soon! Stay tuned :D
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: [Browser MMORPG] isInRange function

Post by 62896dude »

yeah, that sounds right Torniquet, could actually be pretty useful!
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: [Browser MMORPG] isInRange function

Post by Jackolantern »

Ahh, for a map system lol. I was thinking of divs in the design of the page hehe
The indelible lord of tl;dr
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: [Browser MMORPG] isInRange function

Post by 62896dude »

Hahaha nice one Jack :p
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Post Reply

Return to “Code Sharing”