Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-12-2011, 02:27 AM   PM User | #1
Vincent0
New to the CF scene

 
Join Date: Jul 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Vincent0 is an unknown quantity at this point
Problem with variable scoping

So I just began learning HTML 5, and I'm having some issues with variables not being accessed properly.

PHP Code:
var mouseX 0;
var 
mouseY 0;

function 
mousemove(event) {
    var 
canvas document.getElementById('apples'),
    
mouseX event.pageX canvas.offsetLeft;
    
mouseY event.pageY canvas.offsetTop;
}
function 
mousedown(event) {
    if(
judge != playerNumber && started && mouseY >= 500-800/7*1.54){
        var 
Math.floor(mouseX/800*7);
        
alert(mouseX);
    }

All of this is in the <head>.

I define the listener here:
PHP Code:
    var canvas document.getElementById("apples");
    
canvas.addEventListener('mousemove'mousemovefalse);
    
canvas.addEventListener('mousedown'mousedownfalse); 
For some odd reason, mouseX stays at zero outside of mousemove. Inside, it updates properly. It appears to be acting as a local variable. However, mouseY acts properly in both mousemove and mousedown. It retains its value. I've been testing this in Chrome v12.0.742.100, on Windows 7. It does the exact same thing in Firefox. Those are the only parts of the code that reference mouseX, mouseY or the two event listeners.

Any ideas as to how to fix this?
Vincent0 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:46 PM.


Advertisement
Log in to turn off these ads.