Jump to content

Issue with JS no one understands the cause


echofool
 Share

Recommended Posts

Hey I've asked every where with this problem and not one person seems to know the answer. I'm trying to make drag-able map in canvas. I have two current solutions to make it work but both are plagued with some kind of strange problem. My first method is:

function mousePos(e){		mousex = e.pageX;		mousey = e.pageY;canvas.addEventListener("mousemove", movePos, false);} function movePos(e){ 		offset_x = e.pageX-mousex; // used in draw function		offset_y = e.pageY-mousey; // used in draw function}

How ever this won't work because if i try to drag a second time, it resets back to 0 offset because its not adding it on to previous offset values. So i then changed it to:

		offset_x += e.pageX-mousex; // used in draw function		offset_y += e.pageY-mousey; // used in draw function

This solved the reset issue but now when i click and drag it goes so fast off the screen its pretty much useless to use. It also continues in the same direction regardless of mouse movement direction too. any ideas how i solve it - i know its a long shot cos no one has found the solution yet and i've asked literally all forums i can think of.

Edited by echofool
Link to comment
Share on other sites

First, you may want to increase your intervals. You're setting things at an interval of 1ms and clobbering the CPU as it's constantly running the gameUpdate function, which calls the draw function to draw all of the tiles again (1000 times a second). The movePos function sets variables called dx and dy which the comments indicate are supposed to be used in the draw function, but they aren't used anywhere.

This solved the reset issue but now when i click and drag it goes so fast off the screen its pretty much useless to use.
This is because you're adding to the x and y every time the draw function is called, regardless of whether or not you are dragging. If you're not dragging the draw function probably doesn't need to do anything.
Link to comment
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
 Share

×
×
  • Create New...