简体   繁体   中英

How to output a message in Googles Blockly?

I'm having a bit of fun on https://blockly-games.appspot.com and have got to the last level where you can write full blown javascript ( new Date().getTime(); is very handy).

Despite having the console open in Chrome (ctrl-shift-j) I can't seem to write anything to it with console.log("test"); from within the game. When I do I see:

// Starting battle with 2 players.                         compressed.js:420
// [Player] throws an error: Unknown identifier: console   compressed.js:423
// undefined                                               compressed.js:423
// [Player] dies.                                          compressed.js:416

Yet if I type console.log("hmm"); in the console I properly see:

// console.log("hmm"); 
// hmm                                                     VM1038:2 
// undefined

Same story if I try to use alert("test") .

I want to debug, but the only forms of output I've found are manipulating the duck and throwing Unknown identifiers. Is there any way out of this sandbox?

Update: How do I print debug messages in the Google Chrome JavaScript Console? has some ideas for restoring console but they don't seem to work in this case.

Update: just to be clear this is what chrome looks like when experiencing this error.

在此处输入图片说明

Found a kludgey workaround:

  throw "\nYour message here.";

Displays:

//  [Player] throws an error: Unknown identifier:         compressed.js:423 
// Your message here.                                     
//  undefined                                             compressed.js:423 
//  [Player] dies.                                        compressed.js:416

Killing yourself just to log something may seem harsh but now you can write some snazzy asserts.

function assertTrue(test, message) {
  if (!test) {
    throw message;
  }
}

It works, but Lord knows I wish there was a better way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM