Set Blocks in Workspace on Initialization

2,008 views
Skip to first unread message

Steven Ledsworth

unread,
Feb 6, 2015, 7:11:31 PM2/6/15
to blo...@googlegroups.com
Under the setMovable() documentation it says that it can be set on initialization. How can I initialize with a block already in the workspace?


Neil Fraser

unread,
Feb 6, 2015, 7:19:00 PM2/6/15
to blo...@googlegroups.com
Hmm, that section of the documentation could be improved.

setMovable() may be called with true or false as many times as you want at any time.  It will simply anchor/unanchor the block in its current position.  To programmatically move the block (which will work regardless of whether the block is movable or not), call its moveBy(dx, dy) function.

Let me know if anything else is unclear.  I'll update those docs.

On 6 February 2015 at 16:11, Steven Ledsworth <sleds...@gmail.com> wrote:
Under the setMovable() documentation it says that it can be set on initialization. How can I initialize with a block already in the workspace?


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Neil Fraser

unread,
Feb 6, 2015, 7:43:44 PM2/6/15
to blo...@googlegroups.com
The next version of the docs will read:

setMovable

this.setMovable(false);

When set to false, the user will not be able to move the block within the workspace. If this setter is not called, the block defaults to being movable (unless Blockly was initialized as readonly).

Any block (even unmovable ones) may be moved programmatically once they are on a workspace by calling block.moveBy(dx, dy). The starting position for a block on a workspace defaults to (0, 0) unless otherwise specified.


Steven Ledsworth

unread,
Feb 9, 2015, 3:26:48 PM2/9/15
to blo...@googlegroups.com, ro...@neil.fraser.name
That is helpful as I would like to be able to initialize blocks in certain locations; I guess my problem is I do not know where the initialization of blocks in the workspace happen. Where can I declare the blocks that I want to be placed in the workspace when the page is loaded?

Neil Fraser

unread,
Feb 9, 2015, 6:53:22 PM2/9/15
to blo...@googlegroups.com
On 9 February 2015 at 12:26, Steven Ledsworth <sleds...@gmail.com> wrote:
That is helpful as I would like to be able to initialize blocks in certain locations; I guess my problem is I do not know where the initialization of blocks in the workspace happen. Where can I declare the blocks that I want to be placed in the workspace when the page is loaded?

Oh, that's easy, here is an example:
https://github.com/google/blockly/blob/master/demos/generator/index.html

    Blockly.inject(document.getElementById('blocklyDiv'),
        {toolbox: document.getElementById('toolbox')});
    Blockly.Xml.domToWorkspace(Blockly.mainWorkspace,
        document.getElementById('startBlocks'));

The 'startBlocks' XML blob can have movable="false" on any blocks you want to be immovable.
Reply all
Reply to author
Forward
0 new messages