Jump to content

Need help setting limits (X and Y)


joel2k8

Recommended Posts

Alright well Hello,

 

I currently have a table that shows a background image and I have 1 TD with 100% width and height and in that TD I have a DIV that moves by margin-left and margin-top via JavaScript and the height is approx 20px by 20px, so basically I have a map with a character that moves when keys are pressed.

 

My problem is I'm having trouble setting limits (where the character cannot go inside the TD). etc if it reachs a X and Y then it would stop, but it's not that easy, I currently have this in a IF function when the keys are pressed:

 

var x;
var y;

var exclude = {
x: [50, 50, 50, 50, 50],
y: [50, 60, 70, 80, 90]
};
if (exclude.x.indexOf(x) !== -1 && exclude.y.indexOf(y) !== -1){
// dismiss
}
else
{
//run main code..
}

 

But that code doesn' work correctly because I want the numbers in exclude.x to match the oppisite of exclude.y so 50 and 50, 50 and 60 but instead its like this 50 and 50 or 60 or 70 or 80 or 90.

 

I just want it to be set so it matches the number...

 

x: [1, 2, 3, 4, 5],
y: [1, 2, 3, 4, 5]

 

Hope you can help I've been having trouble for awhile now, thanks.

Link to comment
Share on other sites

I'm really not following your explanation well, but I think I know what you are wanting.

 

if (exclude.x.indexOf(x) !== -1 && exclude.y.indexOf(y)===exclude.x.indexOf(x)){

 

That will chek if the X value is in the excluded list. If so, it then checks if the index of the Y value is the same as the index of the X value. So, the //dismiss branch will only be run if the X & Y values are corresponding values in the two arrays.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.