Discuss Scratch

gdavid04
Scratcher
16 posts

Pointer lock, an awesome extension for 3D projects

Have you created a 3D engine/game in Scratch with mouse controls but the control is not the best and it's hard to rotate?
Then Pointer lock is for you!
Pointer lock adds 5 new blocks:
Lock mouse::extension //Locks the mouse

Unlock mouse::extension //Unlocks the mouse

(Mouse movement X::extension) //Mouse movement on the X axis

(Mouse movement Y::extension) //Mouse movement on the Y axis (Y increases down)

<Can lock mouse?::extension> //Returns true if the user's browser supports mouse locking
Known issues
  • The
    <mouse down?>
    sensing block doesn't works when the mouse is locked - there will be a
    <Mouse down?::extension>
    block in the extension to solve thee problem
Planned features
  • <[left v] mouse button down?::extension>
    retruns if left, right, middle or any mouse button is down
  • On [left v] click::extension hat
    called when left, right, middle or any mouse button is clicked
Code:
(function(ext) {
    ext._shutdown = function() {
        document.exitPointerLock();
    };
    ext._getStatus = function() {
        if (havePointerLock) {
            return {status: 2, msg: 'Your browser supports pointer lock.'};
        }
        return {status: 0, msg: 'Your browser doesn\'t supports pointer lock. Try Chrome or Firefox!'}
    };
    var moveCallback = function(e) {
        clearTimeout(stillTimeout);
        stillTimeout = setTimeout(mouseStill, 300);
        movementX = e.movementX         ||
                    e.mozMovementX      ||
                    e.webkitMovementX   ||
                    0;
        movementY = e.movementY         ||
                    e.mozMovementY      ||
                    e.webkitMovementY   ||
                    0;
    }
    var mouseStill = function() {
        movementX = 0;
        movementY = 0;
    }
    var element = document.body;
    var havePointerLock = 'pointerLockElement' in document ||
    'mozPointerLockElement' in document ||
    'webkitPointerLockElement' in document;
    element.requestPointerLock = element.requestPointerLock ||
    element.mozRequestPointerLock ||
    element.webkitRequestPointerLock;
    document.exitPointerLock = document.exitPointerLock ||
    document.mozExitPointerLock ||
    document.webkitExitPointerLock;
    document.addEventListener("mousemove", moveCallback, false);
    var stillTimeout;
    var movementX = 0;
    var movementY = 0;
    ext.lock = function() {
        element.requestPointerLock();
    }
    ext.unlock = function() {
        document.exitPointerLock();
    }
    ext.movex = function() {
        return movementX;
    }
    ext.movey = function() {
        return movementY;
    }
    ext.canlock = function() {
        return havePointerLock;
    }
    var descriptor = {
        blocks: [
            [' ', 'Lock mouse', 'lock'],
            [' ', 'Unlock mouse', 'unlock'],
            ['r', 'Mouse movement X', 'movex'],
            ['r', 'Mouse movement Y', 'movey'],
            ['b', 'Can lock mouse?', 'canlock']
        ]
    };
    ScratchExtensions.register('Pointer lock', descriptor, ext);
})({});
The extension can also be found here.

Last edited by gdavid04 (Nov. 4, 2017 11:39:02)


My Scratch extensions
My latest project: Program the sprite
JGames101
Scratcher
100+ posts

Pointer lock, an awesome extension for 3D projects

This isn't just awesome for 3D projects (although the mouse locking is clearly good for that), but also pretty much any project with the on click block!
gdavid04
Scratcher
16 posts

Pointer lock, an awesome extension for 3D projects

JGames101 wrote:

This isn't just awesome for 3D projects (although the mouse locking is clearly good for that), but also pretty much any project with the on click block!
Yes, i know that this block could be useful for any project because it would be able to detect even right click but this is just a planned feature for now, soon i will add it.

My Scratch extensions
My latest project: Program the sprite
CatIsFluffy
Scratcher
100+ posts

Pointer lock, an awesome extension for 3D projects

How will you add this to projects?
gdavid04
Scratcher
16 posts

Pointer lock, an awesome extension for 3D projects

CatIsFluffy wrote:

How will you add this to projects?
- open your browser's development console while in the Scratch editor
- copy the extension code from this topic
- paste it into the dev console and press enter
- done
You need to do this EVERY TIME you open the project or it just won't work.

My Scratch extensions
My latest project: Program the sprite
hunter601
Scratcher
27 posts

Pointer lock, an awesome extension for 3D projects

This is cool!
chaoxplayz
Scratcher
6 posts

Pointer lock, an awesome extension for 3D projects

lock mouse at x: () y: (0)



ThePlaneGuy45
Scratcher
26 posts

Pointer lock, an awesome extension for 3D projects

Hey I know this is kinda necroposting, but could you make one from scratch 3.0? The extension system Is alot different and this no longer works, if so, thanks!

Plane Expert///Loves Legos
9gr
Scratcher
1000+ posts

Pointer lock, an awesome extension for 3D projects

ThePlaneGuy45 wrote:

Hey I know this is kinda necroposting, but could you make one from scratch 3.0? The extension system Is alot different and this no longer works, if so, thanks!
This won't work for publishing projects now. There are scratch mods although
MAZ3MAK3R_test
Scratcher
1 post

Pointer lock, an awesome extension for 3D projects

Search “pointerlock” in scratch. Click here to see a project I made with this feature.

<
                            

Powered by DjangoBB