문제

I'm trying to access the mousePressed property in a ProcessingJS snippet but am getting undefined.

Here's what I've tried so far:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<script src="http://cloud.github.com/downloads/processing-js/processing-js/processing-1.4.1.min.js"></script>
<script>
$(document).ready(function() {  
    $('body').append($('<canvas id="preview"><p>Your browser does not support the canvas tag.</p></canvas>'));
    function onPJS(p) {
        p.setup = function(){
            console.log(p.mousePressed);//prints undefined
            try{
                console.log(p.mousePressed());
            }catch(e){
                console.log(e.name,e.message);//prints TypeError Property 'mousePressed' of object [object Object] is not a function
            }
        }
    }
    new Processing(document.getElementById("preview"), onPJS);  
});
</script>

Any clues on what I'm missing/doing wrong ?

도움이 되었습니까?

해결책 2

You can't access sketch-internal variables, only functions. You'll have to write a getting function for it, and then it'll work.

boolean getMousePressValue() { return mousePressed; }

That said: you really don't. You can simply add a click, mousedown, mouseup listener to the canvas, which will give you the same information in a pure-JS way, which you seem to be doing anyway. Just use:

pjsCanvas.addEventListener("mousedown",function(){...},false);
pjsCanvas.addEventListener("mouseup",function(){...},false);

Done. No need to reach into the sketch for that information.

다른 팁

I answered this on your other question in passing. I've replicated that here:

GoToLoop at https://github.com/processing-js/processing-js/issues/137 says that your problem with mousepressed is caused by the fact that to avoid name collisions in JS, the boolean mousepressed is called __mousePressed in JS. They recommend that you use Java syntax to code your app and have it automatically translated into JS, to avoid these gotchas.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top