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!


Hey! I like working with Scratch 3.

Scratch 3 Modding

My Website
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
19 posts

Pointer lock, an awesome extension for 3D projects

This is cool!

Powered by DjangoBB

Standard | Mobile