0

So essentially I'm making a page where as an object move to the left as pressing right arrow key, different images would show up when the object is at different location. This is what related functions look like(I'm testing not with the image itself but by drawing an object):

function rightArrowPressed() {
var element = document.getElementById("heart");
var pos = element.style.marginLeft = parseInt(element.style.marginLeft) + 5 + 'px';
element.style.width = parseInt(element.style.width) + 1 + 'px';
console.log(pos);
}

function imageshow(){
        if (pos>30 && pos<=150){ 
        stroke(0);
        strokeWeight(7);
        fill(170);
        rect(windowWidth/2-30, -80, 62, windowHeight+100);

        console.log("yes!"); 
}
}

the variable pos increases fine, I can see it in javascript console, but the console in imageshow function never works.

Thank you very much!

  • There is no `pos` variable in `imageshow`. That code will be throwing a reference error. – Quentin Nov 20 '16 at 22:32
  • The pos variable can only be accessed inside the scope of the function rightArrowPressed(). Therefore imageShow does not know anything about pos. It exists only within rightArrowPressed() scope! – Force444 Nov 20 '16 at 22:36
  • but even after I made pos as a global variable (put it at the beginning of javascript var pos;), it still doesn't work and no console is showing up for that part – 1122334 Nov 20 '16 at 22:40
  • and I tried to put what's in imageShow in the function of rightArrowPressed, it doesn't work either :( – 1122334 Nov 20 '16 at 22:41
  • @DSF but even after I made pos as a global variable (put it at the beginning of javascript var pos;), it still doesn't work and no console is showing up for that part. And I tried to put what's in imageShow in the function of rightArrowPressed, it doesn't work either – 1122334 Nov 20 '16 at 23:02

0 Answers0