Skip to content

zCriterionValidationLibrary Documentation

Dan edited this page Apr 19, 2023 · 13 revisions

zCriterionValidationLibrary Functions

To use these functions, make sure to include the zCriterionValidationLibrary helper library with your level!

When looking at a function: if there's a See: link, then it will take you to a level that uses that function for validation.

Copy-and-paste as the starting validation code for each level that will use this library.

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious(); 
  addCriteria(function() {
    //update to the criteria you want
    return true;
  }, "");
}
getHelperVars();
// drawHandsOnUnclickedSprites(); //uncomment on levels where the user needs to click sprites to advance
check();
updatePrevious();

Functions

member(elt, array)boolean

Checks if an element is a member of an array. Equivalent to array.includes(elt).

getHelperVars()

Updates several helper variables that are used in validation code:

  • spriteIds - an array of all the current sprite IDs
  • animations - an array all the the animation costumes in use
  • eventLog - an array of all events during the course of a level, where each element is a string representing an onscreen event
  • printLog - an array of all printed text during the course of a level, where each element is a string that is created using print blocks
  • soundLog - an array of all sounds played during the running of a level
  • eventSpriteIds - an array of just the spriteIds who were involved in an event during the current frame of the level
  • promptVars - an object of all prompts that students have answered during the running of a level, in the form of {promptVariable: "response"}
  • spriteBehaviorsObj - an object of all current behaviors for any sprite on the screen This function should get called in the validation code for every validation level (see example);
setupPrevious()

Sets up a previous object that's a snapshot of the state of the level from the previous frame. This is necessary to detect things like:

  • Did a new event happen?
  • Did a sprite start speaking as a result of touching another sprite?
  • Did the background change when something happened? This function should get called in the validation code for every validation level (see example);
updatePrevious()

Updates the previous object at the very end of the validation code. This function should get called in the validation code for every validation level (see example);

extendFailTime(n)boolean

Extends the fail time in a level, such as when the user interacts with a sprite.

member(elt, array) ⇒ boolean

Checks if an element is a member of an array. Equivalent to array.includes(elt).

Kind: global function
Returns: boolean - true if member, false otherwise

Param Type Description
elt the element to check for
array array the array to search through for the element

Example

var colors = ["red", "green", "blue"];
// This will return true
member("red", colors);
// This will return false
member("orange", colors);

getHelperVars()

Updates several helper variables that are used in validation code:

  • spriteIds - an array of all the current sprite IDs
  • animations - an array all the the animation costumes in use
  • eventLog - an array of all events during the course of a level, where each element is a string representing an onscreen event
  • printLog - an array of all printed text during the course of a level, where each element is a string that is created using print blocks
  • soundLog - an array of all sounds played during the running of a level
  • eventSpriteIds - an array of just the spriteIds who were involved in an event during the current frame of the level
  • promptVars - an object of all prompts that students have answered during the running of a level, in the form of {promptVariable: "response"}
  • spriteBehaviorsObj - an object of all current behaviors for any sprite on the screen This function should get called in the validation code for every validation level (see example);

Kind: global function
Summary: Updates several helper variables that are used in validation code
Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();

  // addCriteria functions here

}
getHelperVars(); //<-----------
check();
updatePrevious();

setupPrevious()

Sets up a previous object that's a snapshot of the state of the level from the previous frame. This is necessary to detect things like:

  • Did a new event happen?
  • Did a sprite start speaking as a result of touching another sprite?
  • Did the background change when something happened? This function should get called in the validation code for every validation level (see example);

Kind: global function
Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious(); //<-----------

  // addCriteria functions here

}
getHelperVars();
check();
updatePrevious();

updatePrevious()

Updates the previous object at the very end of the validation code. This function should get called in the validation code for every validation level (see example);

Kind: global function
Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();

  // addCriteria functions here

}
getHelperVars();
check();
updatePrevious(); // <-----------

extendFailTime(n) ⇒ boolean

Extends the fail time in a level, such as when the user interacts with a sprite.

Kind: global function
Returns: boolean - always returns true so it can be used within the return part of criterion commands.
See

Param Type Description
n number the amount of frames to extend the fail time by.

Example

//Extends the fail time by 45 frames (1.5 seconds)
addCriteria(function() {
   return checkAtLeastNPromptsAnswered(1) && extendFailTime(45);
 }, "cscFunctionsPromptNotAnswered");

Functions

checkMovedSpritePin(spriteId)boolean

Checks if a particular sprite was moved from the default (200, 200) location in a level

checkNotDefaultSize(spriteId)boolean

Checks if the sprite's size has been changed from the default. Not designed to be used in an event.

checkNotDefaultCostume(spriteId, defaultCostume)boolean

Checks if the sprite's costume has been changed from the default. Not designed to be used in an event.

checkSpriteSpeech(spriteId)boolean

Checks if a particular sprite was speaking when the program started running. Not designed to be used in an event.

atLeastOneFromCostumes(spriteCostumes)boolean

Checks if at least one of the costumes (in an array) is set to a sprite in the scene

exactlyOneFromCostumes(spriteCostumes)boolean

Checks if there is exactly one of the costumes from the array of costumes (ie: the level expects just one character, but a student added many)

getExactlyOneSpriteIdFromCostumes(spriteCostumes)number

Returns the spriteId of a sprite that has the costume in the array. Should be used with exactlyOneFromCostumes() to ensure there is a unique ID returned. If there is no sprite: -1 is returned

checkMovedSpritePin(spriteId) ⇒ boolean

Checks if a particular sprite was moved from the default (200, 200) location in a level

Kind: global function
Returns: boolean - true if the specific sprite was moved before the level started, and false otherwise
See: Click here for example - Check if we move the sprite

Param Description
spriteId the sprite to check if it was touched, usually accessed via spriteIds[] array

Example

addCriteria(function() {
   return spriteIds.length >= 1 && checkMovedSpritePin(spriteIds[0]);
 }, "changeLocation");  // include i18n feedback string

checkNotDefaultSize(spriteId) ⇒ boolean

Checks if the sprite's size has been changed from the default. Not designed to be used in an event.

Kind: global function
Returns: boolean - true if the specific sprite is not still the default size, and false otherwise
See: Click here for example - Check if we resized a sprite

Param Description
spriteId the sprite to check the size, usually accessed via spriteIds[] array

Example

addCriteria(function() {
    return checkNotDefaultSize(spriteIds[0]);
  }, "useSetpropBlock");

checkNotDefaultCostume(spriteId, defaultCostume) ⇒ boolean

Checks if the sprite's costume has been changed from the default. Not designed to be used in an event.

Kind: global function
Returns: boolean - true if the specific sprite is not the default costume, and false otherwise
See: Click here for example - Add a landmark

Param Type Description
spriteId the sprite to check the costume, usually accessed via spriteIds[] array
defaultCostume string the default costume in the level. This can be accessed by dragging out a sprite block, then viewing the code and looking at the costume that is generated

Example

//Checks whether the second sprite is not the default costume
 addCriteria(function() {
   return spriteIds.length >= 2 && checkNotDefaultCostume(spriteIds[1], "red_shirt_wave2");
 }, "cscLandmarkChangeCostume");  // include i18n feedback string

checkSpriteSpeech(spriteId) ⇒ boolean

Checks if a particular sprite was speaking when the program started running. Not designed to be used in an event.

Kind: global function
Returns: boolean - true if the specific sprite is speaking when the program is run, and false otherwise
See: Click here for example - Make your characters say something

Param Description
spriteId the sprite to check if it is speaking, usually accessed via spriteIds[] array

Example

addCriteria(function() {
    return checkSpriteSpeech(spriteIds[0]);
  }, "noSpeech");

atLeastOneFromCostumes(spriteCostumes) ⇒ boolean

Checks if at least one of the costumes (in an array) is set to a sprite in the scene

Kind: global function
Returns: boolean - true if at least one of these costumes is in the scene, false otherwise
See: Click here for example - checking for an octopus

Param Description
spriteCostumes An array of costumes (as strings) to check for

Example

addCriteria(function() {
    return World.frameCount == 1 && atLeastOneFromCostumes(["octopus_green", "octopus_purple", "octopus_red"]);
  }, "There is no octopus at the beginning of the scene!");

exactlyOneFromCostumes(spriteCostumes) ⇒ boolean

Checks if there is exactly one of the costumes from the array of costumes (ie: the level expects just one character, but a student added many)

Kind: global function
Returns: boolean - true if exactly one of these costumes is in the scene, false otherwise
See: Click here for example - checking for an octopus

Param Description
spriteCostumes An array of costumes (as strings) to check for

Example

addCriteria(function() {
    return World.frameCount == 1 && exactlyOneFromCostumes(["octopus_green", "octopus_purple", "octopus_red"]);
  }, "There are too many octopi in the beginning of this scene!");

getExactlyOneSpriteIdFromCostumes(spriteCostumes) ⇒ number

Returns the spriteId of a sprite that has the costume in the array. Should be used with exactlyOneFromCostumes() to ensure there is a unique ID returned. If there is no sprite: -1 is returned

Kind: global function
Returns: number - number representing the spriteId; -1 otherwise
See: Click here for example - checking for an octopus

Param Description
spriteCostumes An array of costumes (as strings) to check for

Example

addCriteria(function() {
    var octopusId = getExactlyOneSpriteIdFromCostumes(["octopus_green", "octopus_purple", "octopus_red"]);
    return checkSpriteTouchedThisFrame() && checkNewSpriteCostumeThisFrame(octopusId);
  }, "There was an event, but the octopus sprite didn't change costume!");

Functions

checkNumClickedSprites(n)boolean

Checks for the number of unique sprites that have been clicked over the course of the entire level.

checkNumTouchedSprites(n)boolean

Checks for the number of unique sprites that have been touched over the course of the entire level.

checkNewEventThisFrame()boolean

Checks if a new event happened this frame

checkSpriteClickedThisFrame()boolean

Checks if a sprite was clicked this frame

checkSpriteTouchedThisFrame()boolean

Checks if a sprite was touched this frame

checkThisSpriteTouchedThisFrame(spriteId)boolean

Checks if a particular sprite was touched this frame

checkThisSpriteClickedThisFrame(spriteId)boolean

Checks if a particular sprite was clicked this frame

checkNewBackground()boolean

Checks if the background changed from the previous version. Designed to be used to check with an event

checkAtLeastNEvents(n)boolean

Checks if at least n events have occured through the running of the level

checkPromptWithEvent()boolean

Checks if Checks if a prompt appeared with an event

checkTheseTwoSpriteCostumesTouched(costume1, costume2)boolean

Returns whether two specific sprite costumes are touching, such as wanting to check that a certain sprite interacted with a another specific

trackSpriteSpeech(spriteId)

Updates current sprite speech for given sprite id. Meant to be called every frame, similar to getHelperVars()

updatePrevSpriteSpeech(spriteId)

Keeps track of previous frame sprite speech. Meant to be called just before the end of the validation loop, similar to updatePrevious();

checkNewSpriteSpeakThisFrame(spriteId)

Checks if the sprite's speech changed this frame, either because started speaking or because saying something new

trackSpriteCostume(spriteId)

Updates current sprite costume for given sprite id. Meant to be called every frame, similar to getHelperVars()

updatePrevSpriteCostume(spriteId)

Keeps track of previous frame sprite costume. Meant to be called just before the end of the validation loop, similar to updatePrevious();

checkNewSpriteCostumeThisFrame(spriteId)

Checks if the sprite's costume changed this frame

checkNumClickedSprites(n) ⇒ boolean

Checks for the number of unique sprites that have been clicked over the course of the entire level.

Kind: global function
Returns: boolean - true if at least n sprites have been clicked throughout the level, false otherwise
See: Click here for example - Click 2 sprites level

Param Type Description
n number number of unique click events to test for

Example

addCriteria(function() {
    return checkNumClickedSprites(2);
  }, "clickAllSprites");

checkNumTouchedSprites(n) ⇒ boolean

Checks for the number of unique sprites that have been touched over the course of the entire level.

Kind: global function
Returns: boolean - true if at least n sprites have been touched throughout the level, false otherwise
See: Click here for example - Touch all landmarks level

Param Type Description
n number number of unique click events to test for

Example

addCriteria(function() {
    return checkNumTouchedSprites(2);
  }, "touchAllSprites");

checkNewEventThisFrame() ⇒ boolean

Checks if a new event happened this frame

Kind: global function
Returns: boolean - true if a new event happened this frame, and false otherwise
Example

addCriteria(function() {
    return checkNewEventThisFrame();
  }, "noEvent");

checkSpriteClickedThisFrame() ⇒ boolean

Checks if a sprite was clicked this frame

Kind: global function
Returns: boolean - true if a sprite was clicked this frame, and false otherwise
Example

addCriteria(function() {
    return checkSpriteClickedThisFrame();
  }, "noSpriteClicked");

checkSpriteTouchedThisFrame() ⇒ boolean

Checks if a sprite was touched this frame

Kind: global function
Returns: boolean - true if a sprite was touched this frame, and false otherwise
Example

addCriteria(function() {
    return checkSpriteTouchedThisFrame();
  }, "noSpriteTouched");

checkThisSpriteTouchedThisFrame(spriteId) ⇒ boolean

Checks if a particular sprite was touched this frame

Kind: global function
Returns: boolean - true if the specific sprite was touched this frame, and false otherwise
See: Click here for example - Check touching most recent landmark

Param Description
spriteId the sprite to check if it was touched, usually accessed via spriteIds[] array

Example

addCriteria(function() {
    return spriteIds.length >= 3 && checkThisSpriteTouchedThisFrame(spriteIds[2]);
  }, "cscLandmarkMostRecent");

checkThisSpriteClickedThisFrame(spriteId) ⇒ boolean

Checks if a particular sprite was clicked this frame

Kind: global function
Returns: boolean - true if the specific sprite was clicked this frame, and false otherwise
See: Click here for example - Check if Hank says something when clicked

Param Description
spriteId the sprite to check if it was touched, usually accessed via spriteIds[] array

Example

addCriteria(function() {
    return spriteIds.length >= 2 && checkThisSpriteClickedThisFrame(spriteIds[1]);
  }, "cscBookcoverNoClick");

checkNewBackground() ⇒ boolean

Checks if the background changed from the previous version. Designed to be used to check with an event

Kind: global function
Returns: boolean - true if the background changed from the previous frame
See: Click here for example - Check if storytellers change background
Example

//For second sprite: check that the background changed when we touch the sprite
  addCriteria(function() {
    return spriteIds.length >= 3 && checkThisSpriteTouchedThisFrame(spriteIds[1]) && checkNewBackground();
  }, "cscLandmarkBackgroundStoryteller");

  //For third sprite: check that the background changed
  addCriteria(function() {
    return spriteIds.length >= 3 && checkThisSpriteTouchedThisFrame(spriteIds[2]) && checkNewBackground();
  }, "cscLandmarkBackgroundStoryteller");  // include i18n feedback string

checkAtLeastNEvents(n) ⇒ boolean

Checks if at least n events have occured through the running of the level

Kind: global function
Returns: boolean - true if at least n events have been detected

Param Type Description
n number the number of events to check for

Example

addCriteria(function() {
   return checkAtLeastNEvents(1);
 }, "noEvents");

checkPromptWithEvent() ⇒ boolean

Checks if Checks if a prompt appeared with an event

Kind: global function
Returns: boolean - true if a prompt is detected with an event
Example

addCriteria(function() {
   return checkAtLeastNEvents(1);
 }, "noEvents");

checkTheseTwoSpriteCostumesTouched(costume1, costume2) ⇒ boolean

Returns whether two specific sprite costumes are touching, such as wanting to check that a certain sprite interacted with a another specific

Kind: global function
Returns: boolean - Whether or not two sprites with these costumes are touching
See: Click here for example - checking for an octopus adapting to its environment

Param Description
costume1 A string of one costume to check
costume2 A string of one costume to check

Example

addCriteria(function() {
    var octopusId = getExactlyOneSpriteIdFromCostumes(["octopus_green", "octopus_purple", "octopus_red"]);
    return checkSpriteTouchedThisFrame() && checkNewSpriteCostumeThisFrame(octopusId) &&
      	  (checkTheseTwoSpriteCostumesTouched("octopus_green", "underseadeco_34") || 
          checkTheseTwoSpriteCostumesTouched("octopus_red", "underseadeco_25") ||
          checkTheseTwoSpriteCostumesTouched("octopus_purple", "underseadeco_24"));
  }, "cscAdaptationsMoveOctopusToPlant");

trackSpriteSpeech(spriteId)

Updates current sprite speech for given sprite id. Meant to be called every frame, similar to getHelperVars()

Kind: global function
See: Click here - checking if a sprite speaks with a prompt

Param Type Description
spriteId number the id of the sprite to track, usually by passing an index of the spriteIds object

Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();

  addCriteria(function() {
    //Check that a sprite started speaking the same frame a prompt was answered
    return checkPromptUpdatedThisFrame() && checkNewSpriteSpeakThisFrame(spriteIds[0]);
  }, "cscFunctionsAddSpeachToPrompt");
}
getHelperVars();
trackSpriteSpeech(spriteIds[0]); // <-------------
check();
updatePrevSpriteSpeech(spriteIds[0]);
updatePrevious();

updatePrevSpriteSpeech(spriteId)

Keeps track of previous frame sprite speech. Meant to be called just before the end of the validation loop, similar to updatePrevious();

Kind: global function
See: Click here - checking if a sprite speaks with a prompt

Param Type Description
spriteId number the id of the sprite to track, usually by passing an index of the spriteIds object

Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();

  addCriteria(function() {
    //Check that a sprite started speaking the same frame a prompt was answered
    return checkPromptUpdatedThisFrame() && checkNewSpriteSpeakThisFrame(spriteIds[0]);
  }, "cscFunctionsAddSpeachToPrompt");
}
getHelperVars();
trackSpriteSpeech(spriteIds[0]);
check();
updatePrevSpriteSpeech(spriteIds[0]); // <-------------
updatePrevious();

checkNewSpriteSpeakThisFrame(spriteId)

Checks if the sprite's speech changed this frame, either because started speaking or because saying something new

Kind: global function
See: Click here - checking if a sprite speaks with a prompt

Param Type Description
spriteId number the id of the sprite to track, usually by passing an index of the spriteIds object

Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious();

  addCriteria(function() {
    //Check that a sprite started speaking the same frame a prompt was answered
    return checkPromptUpdatedThisFrame() && checkNewSpriteSpeakThisFrame(spriteIds[0]); // <-------------
  }, "cscFunctionsAddSpeachToPrompt");
}
getHelperVars();
trackSpriteSpeech(spriteIds[0]);
check();
updatePrevSpriteSpeech(spriteIds[0]); 
updatePrevious();

trackSpriteCostume(spriteId)

Updates current sprite costume for given sprite id. Meant to be called every frame, similar to getHelperVars()

Kind: global function

Param Type Description
spriteId number the id of the sprite to track, usually by passing an index of the spriteIds object

Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious(); 
  addCriteria(function() {
    //update to the criteria you want
    return minimumSprites(1) && checkThisSpriteClickedThisFrame(spriteIds[0]) && checkNewSpriteCostumeThisFrame(spriteIds[0]);
  }, "Make sure you change the costume of your sprite!");
}
getHelperVars();
trackSpriteCostume(spriteIds[0]); // <-------
check();
updatePrevSpriteCostume(spriteIds[0]);
updatePrevious();

updatePrevSpriteCostume(spriteId)

Keeps track of previous frame sprite costume. Meant to be called just before the end of the validation loop, similar to updatePrevious();

Kind: global function

Param Type Description
spriteId number the id of the sprite to track, usually by passing an index of the spriteIds object

Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious(); 
  addCriteria(function() {
    //update to the criteria you want
    return minimumSprites(1) && checkThisSpriteClickedThisFrame(spriteIds[0]) && checkNewSpriteCostumeThisFrame(spriteIds[0]);
  }, "Make sure you change the costume of your sprite!");
}
getHelperVars();
trackSpriteCostume(spriteIds[0]); 
check();
updatePrevSpriteCostume(spriteIds[0]); // <-------
updatePrevious();

checkNewSpriteCostumeThisFrame(spriteId)

Checks if the sprite's costume changed this frame

Kind: global function

Param Type Description
spriteId number the id of the sprite to track, usually by passing an index of the spriteIds object

Example

if(World.frameCount == 1) {
  setFailTime(150);
  setDelayTime(90);
  setupPrevious(); 
  addCriteria(function() {
    //update to the criteria you want
    return minimumSprites(1) && checkThisSpriteClickedThisFrame(spriteIds[0]) && checkNewSpriteCostumeThisFrame(spriteIds[0]); // <-------
  }, "Make sure you change the costume of your sprite!");
}
getHelperVars();
trackSpriteCostume(spriteIds[0]); 
check();
updatePrevSpriteCostume(spriteIds[0]); 
updatePrevious();

Functions

checkAtLeastNPrints(n)boolean

Checks if there are at least n things printed using the print block

checkAtLeastNPromptsAnswered(n)boolean

Checks that there's at least N prompts have been answered in the workspace. This only works after the prompt appears on the screen - for example, if a level has 3 prompts but they're all in events, the user needs to activate all 3 events first before we can detect the 3 prompts.

checkAllPromptVariablesSet()boolean

Checks that all the prompt variables have been set by a student in the level (ie: are not still ???). This function can be called in multiple criteria functions, especially if multiple prompts are used in a level (ie: once when the first prompt is answered, then again when the second prompt is answered).

checkPromptUpdatedThisFrame()boolean

Checks if a prompt was answered this frame. If the level has a when [X] answered block, this will be true and we can validate if other things happened with the event

checkAtLeastNPrints(n) ⇒ boolean

Checks if there are at least n things printed using the print block

Kind: global function
Returns: boolean - true if at least n print logs are detected
See

Param Type Description
n number the number of print statements to check for

Example

addCriteria(function() {
   return checkAtLeastNPrints(1);
 }, "addPrintBlock");

checkAtLeastNPromptsAnswered(n) ⇒ boolean

Checks that there's at least N prompts have been answered in the workspace. This only works after the prompt appears on the screen - for example, if a level has 3 prompts but they're all in events, the user needs to activate all 3 events first before we can detect the 3 prompts.

Kind: global function
Returns: boolean - true if at least n prompts are detected
See: Click here for example - Check for a prompt after a click event

Param Type Description
n number the number of prompts to check for

Example

addCriteria(function() {
   return checkAtLeastNPromptsAnswered(1);
 }, "cscBookcoverNoPromptAnswered");

checkAllPromptVariablesSet() ⇒ boolean

Checks that all the prompt variables have been set by a student in the level (ie: are not still ???). This function can be called in multiple criteria functions, especially if multiple prompts are used in a level (ie: once when the first prompt is answered, then again when the second prompt is answered).

Kind: global function
Returns: boolean - true if all prompt variables are not ???; false if any of them are ???
See: Click here for example - check the prompt variable changed
Example

addCriteria(function() {
   return checkAllPromptVariablesSet();
 }, "cscFunctionsUpdatePromptVariable");

checkPromptUpdatedThisFrame() ⇒ boolean

Checks if a prompt was answered this frame. If the level has a when [X] answered block, this will be true and we can validate if other things happened with the event

Kind: global function
Returns: boolean - true if a prompt was asked or answered this frame; false otherwise
See: Click here for example - checking when a prompt has been answered
Example

addCriteria(function() {
   //Assumes there's a variable called output.
   //This will be true if someone answered a prompt and the output variable got updated to not be 0 (assuming it was 0 before)
   return checkPromptUpdatedThisFrame() && output != 0;
 }, "");

checkSetBackground() ⇒ boolean

Checks if the background was set to something. Designed to be implemented in levels without any background block, so setting background for first time

Kind: global function
Returns: boolean - true if the background exists
See: Click here for example - Check background in first level
Example

addCriteria(function() {
   return checkSetBackground();
 }, "spritelabFeedbackChangeBackgroundColor");

Functions

trackStudentVariable(variableName)

Updates value of a student variable. Meant to be called every frame, similar to getHelperVars()

updateStudentVariable(variableName)

Keeps track of previous frame variable. Meant to be called just before the end of the validation loop, similar to updatePrevious();

checkStudentVariableChangedThisFrame(variableName)boolean

Checks if the variable changed this frame

checkLastPrintStatementContains(string)boolean

Checks that the last thing printed by the user contains a given string. For example, if this criterion function was passed the string "example", then the criterion function would return true if the user printed "this is an example" or "example" but not "examp".

checkInteractiveSpriteMovement()boolean

Checks that a user caused a sprite to move with the arrow keys by detecting arrow keypresses and comparing the position of each sprite between the previous and current frames.

checkSpriteMoved()boolean

Checks that a sprite has been moved from their default position in one given dimension by a given amount.

trackStudentVariable(variableName)

Updates value of a student variable. Meant to be called every frame, similar to getHelperVars()

Kind: global function
See: Click here for example level

Param Type Description
variableName string the name of the variable we want to track

Example

if(World.frameCount == 1) {
  setFailTime(250);
  setDelayTime(150);
  setupPrevious();

  addCriteria(function() {
    //Check we clicked the sprite and the output variable changed
    return checkThisSpriteClickedThisFrame(spriteIds[0]) && checkStudentVariableChangedThisFrame("output");
  }, "cscFunctionsNoFunctionBlock");

}
getHelperVars();
drawHandsOnUnclickedSprites(0);
trackStudentVariable("output"); // <--------
check();
updateStudentVariable("output");
updatePrevious();

updateStudentVariable(variableName)

Keeps track of previous frame variable. Meant to be called just before the end of the validation loop, similar to updatePrevious();

Kind: global function
See: Click here for example level

Param Type Description
variableName string the name of the variable we want to update

Example

if(World.frameCount == 1) {
  setFailTime(250);
  setDelayTime(150);
  setupPrevious();

  addCriteria(function() {
    //Check we clicked the sprite and the output variable changed
    return checkThisSpriteClickedThisFrame(spriteIds[0]) && checkStudentVariableChangedThisFrame("output");
  }, "cscFunctionsNoFunctionBlock");

}
getHelperVars();
drawHandsOnUnclickedSprites(0);
trackStudentVariable("output"); 
check();
updateStudentVariable("output"); // <--------
updatePrevious();

checkStudentVariableChangedThisFrame(variableName) ⇒ boolean

Checks if the variable changed this frame

Kind: global function
Returns: boolean - true if the variable changed this frame; false if it didn't.
See: Click here for example level

Param Type Description
variableName string the name of the variable we want to check

Example

if(World.frameCount == 1) {
  setFailTime(250);
  setDelayTime(150);
  setupPrevious();

  addCriteria(function() {
    //Check we clicked the sprite and the output variable changed
    return checkThisSpriteClickedThisFrame(spriteIds[0]) && checkStudentVariableChangedThisFrame("output"); // <--------
  }, "cscFunctionsNoFunctionBlock");

}
getHelperVars();
drawHandsOnUnclickedSprites(0);
trackStudentVariable("output");
check();
updateStudentVariable("output");
updatePrevious();

checkLastPrintStatementContains(string) ⇒ boolean

Checks that the last thing printed by the user contains a given string. For example, if this criterion function was passed the string "example", then the criterion function would return true if the user printed "this is an example" or "example" but not "examp".

Kind: global function
Returns: boolean - true if the statement printed by a print block contains
See: Click here for example

Param Type Description
string string the string to search for in the last print statement

Example

addCriteria(function() {
   return checkLastPrintStatementContains(example);
 }, "genericFailure");

checkInteractiveSpriteMovement() ⇒ boolean

Checks that a user caused a sprite to move with the arrow keys by detecting arrow keypresses and comparing the position of each sprite between the previous and current frames.

Kind: global function
Returns: boolean - true if the any sprite moved in the same frame that a user pressed an arrow key
See: Click here for example
Example

addCriteria(function() {
   return checkInteractiveSpriteMovement()
 }, "genericFailure");

checkSpriteMoved() ⇒ boolean

Checks that a sprite has been moved from their default position in one given dimension by a given amount.

Kind: global function
Returns: boolean - true if the sprite with the given spriteID moved an amount of pixels in a given direction
See: Click here for example
Example

if (World.frameCount == 1) {

  setFailTime(400); // Frames to wait before failing student
  setDelayTime(90); // Frames to wait after success before stopping program
  setupPrevious(); //Defines the validationProps.previous{} object. To use it, call updatePrevious() at the end of this box
  
  addCriteria(function() {
    return spriteIds.length >= 1 && checkSpriteMoved(spriteIds[0], 'x', 30);
  }, "didntMoveUp");  // include i18n feedback string

}
getHelperVars();
check();
updatePrevious();
Clone this wiki locally