How to call a built-in Snap block from JS?

Hi, I read some of the other similar posts on here about this, but none of the posts had a simple example.

function moveCallback(e) {
  // turn 15 degrees
}
document.addEventListener("mousemove", moveCallback, false);
document.body.requestPointerLock();

I want to call the Turn () Degrees block where I wrote the comment, but I can't figure out what to write.

Any help is appreciated.

EDIT: I see @Jens' post below and wanted to answer some questions. A group of my students wanted to make a 3D game like Doom. They found a Snap project on the forums that used the Pen blocks very creatively and it worked. They decided to use that code as a base for what they were creating, but that code used keyboard movement and they wanted mouse movement.

I couldn't find a way for Snap to lock the cursor in the middle of the screen or prevent the mouse from going off the edge of the screen. I was aware that other games on the internet (written with javascript) were able to accomplish this, and it involved hiding the cursor and giving the user the ability to press escape to get their mouse control back. I eventually found the JavaScript requestPointerLock() function on the Mozilla website, tested it in a Snap JS function, and it worked.

This introduced a new problem, however. after the pointer was locked, mouse movement blocks in Snap no longer worked. For example, the mouse position reporters were stuck reporting the same value, where the mouse was when the pointer lock was enabled. This forced me to look into JavaScript functions again. I tested using the onmousemove listener, and it still worked. Despite the pointer lock, it still recognized that the mouse was being moved.

Finally, it was just a matter of tying mouse movement back to Snap code, so the students could work with it in their game. This is when I realized I had no idea what to type, nor if this was even possible. I tried logging a few this in the console but I didn't recognize the names of anything. I recognized that my strategy of guessing and checking the logs of different things in the console was going to take far too long. I went to the forums in search of an answer, and found a variety of topics similar to the issue, but not a straightforward answer. I found someone suggesting to download the Snap source code, which I did, and I searched through a few files. I quickly realized that I didn't know the name of the exactly what to look for. The function could have been named anything, and which of the files was it going to be stored in? After I found the function, how would I reference my sprite to make the call? Once again, I recognized this search for knowledge was going to take far too long, and someone on the forums probably already knows how to reference sprites and what the name of the function is.

During this search, I had also found some of your (Jens) posts that discouraged JavaScript discussion because of fear of creating the wrong culture, amongst other things. This led me to the advanced help forum which seemed like a safe enough place to ask. I recognized that I wasn't truly asking for JavaScript help. What I needed was the names of Snap objects and Snap functions. Using this logic, I believed this was a safe question to ask. I then tried to word my question as simple as possible without using too many words. I made the post and left work for home; I had to go pick up my kids from daycare.

The first response confused me. I was a little familiar with making JavaScript objects and how they could use prototype, but I didn't know you could call functions via their prototype. From this response I also learned that "this" was referencing the current sprite, awesome. That made things easy. I tested the suggested code and it worked. I learned some things about Snap and JavaScript.

Finally, I saw your (Jens) post. I took a day to think about what to say, so as not to offend. To my surprise, others came to my defense before I did. Now I see that the topic is locked, so this really spun out of control fast.

Here is my response to your message:

Another largely pointless JavaScript discussion.

Harsh, and not true. It had a point for me and my students, and I learned things along the way.

is: this.turn(15);

Of course, if the turn function is defined in the prototype, then it can also be directly referenced. Thanks for pointing this out.

This is exactly why I hate discussing JavaScript in these forum.

I recognize now that I still failed to avoid triggering your hatred, despite my efforts.

Nothing is even remotely advanced or computationally interesting about any of this

I went to the advanced forum because I hoped to keep the normal help forum pure from JavaScript discussion, as I saw from your desires. Nothing about this project was ever intended to be "computationally interesting", I didn't see a requirement like that to post in this forum.

and rather than learn something usable you're even turning a straightforward function call into a baroque monstrosity of indirectness.

I suppose this critique is more directed at @helicoptur, rather than me, since I didn't write that code, although I did thank him for it. I certainly learned usable things from this. I learned that object functions can be called via their prototype, which I recognize is strange. I learned that in Snap, "This" references the current sprite. I then realized from your post that I could just call the function via the this reference like normal. I went on to log more specific things to console, such as parents and prototypes and discovered many more functions that could be called, and I also learned the parent child structure for the IDE, stage, and sprites during this time.

Why are you asking this question, what are you trying to accomplish? How does this answer help you accomplish anything?

I think these questions were answered in the first part of my edit.

I think it's rude to keep asking for JavaScript advice in this forum

This is my first post on the forums, so I feel like this is directed at others? Going forward, perhaps pointer lock could be implemented as an official Snap block? Then there will be no need for JS in this instance.

This isn't about getting things to "just work"

It was. I just needed one small piece of info in order to allow the students to continue their work.

This is what the final code now looks like:

document.onmousemove = (e) => { this.turn(e.movementX); };
document.onclick = (e) => { this.parent.parent.broadcast("test"); }
document.body.requestPointerLock();

This was a long response, so apologies if I mistyped anywhere in this edit.

function moveCallback(e) {
  SpriteMorph.prototype.turn.call(this, 15);
// ...

This script will make the current sprite turn 15 degrees, assuming the function moveCallback is run from a sprite. (Replace 15 with whatever you want, of course.)
P.S. What are you doing with JavaScript here?

Pretty sure they're trying to implement pointerlock

I personally would just create a pointerlock library that has blocks to turn pointerlock on, report the mouse movements, and test if pointerlock is even enabled. Of course, I would also make it so that when you press the esc key, it disables pointerlock.

Now that I think about it, I might try to create this library.

Thanks, SpriteMorph.prototype.turn.call(this, 15); works perfectly.

Cheers.

sigh. Another largely pointless JavaScript discussion.

is: this.turn(15);

This is exactly why I hate discussing JavaScript in these forum. Nothing is even remotely advanced or computationally interesting about any of this, and rather than learn something usable you're even turning a straightforward function call into a baroque monstrosity of indirectness.

Why are you asking this question, what are you trying to accomplish? How does this answer help you accomplish anything?

Then don’t. :smirk:

Exactly. Jens, you don't have to post on these topics. In programming, not everything has to be "computationally interesting"; sometimes you just have to make things work. Barging into every JS-related topic only to tell us how JavaScript is the devil is pretty rude, especially when it's correctly categorized. (Speaking of categories, you can mute them if you don't like the kind of things that get posted in them!)

Not necessarily

Edit: Wait, hold on.

I think there might be a bit of a consistancy issue here.

I think it's rude to keep asking for JavaScript advice in this forum and to keep propagating the impression that in order to be part of the inner circle of the Snap community JavaScript is helpful. This isn't about getting things to "just work", it's fostering an elitist, exclusive hacker culture that's pretty much the exact opposite of what I'm working on with Snap! I'm closing this. And I will do so with other threads that pop up. See, with my own kids I'm willing to explain my reasoning time and again. But with the folks here my patience has all but worn off.