Control

JS4Scratch Reference Manual:

Wait

Syntax:

wait(1)

Example:

spriteA.whenFlag(function() {
   this.show()
   this.say('Start')
   this.wait(1)
   this.say('1 second later')
})

Repeat

Syntax:

for (var counter = 1; counter < 11; counter++) {
  /* your code */
}

Example:

spriteA.whenFlag(function() {
   for (var counter = 1; counter < 11; counter++){
      this.say('Repeat Loop Iteration: ' + counter)
      console.log(counter)
      this.wait(1)
   }
})

Forever

Syntax:

while (true) {
  /* your code */
}

Example:

spriteA.whenFlag(function() {
   while (true){
      this.nextCostume()
      this.wait(1)
   }
})

If Then

Syntax:

if (yourCondition) {/* your code */}

Example:

spriteA.whenFlag(function () {
    while(true){
        this.say("waiting for you to press the up arrow key")
        if (stage.isKeyPressed('ArrowUp')) {
            this.say("Up Arrow key was just pressed")
            this.wait(1)
        }
    }
})

If Then Else

Syntax:

if (yourCondition) {
  /* your code */
} else {
  /* your code */
  }

Example:

spriteA.whenFlag(function () {
   while(true) {
      if (stage.isKeyPressed('ArrowUp')) {
         this.say("Up Arrow key was just pressed")
         this.wait(1)
      } else {
         this.say("waiting for you to press the up arrow key")
      }
   }
})

Wait Until

Syntax:

//use JS conditional while loop as below

Example:

wait_condition = true
spriteA.whenFlag (function () {
    this.show()
   while (wait_condition === true) {
      wait_condition = !stage.isKeyPressed ('ArrowUp')
      this.say ('just doing nothing until the wait condition becomes true i.e press the up arrow key')
      this.wait (0)        // stops older devices from lagging
   }
   this.say ('Move on to next code line because the wait condition is now true')
   console.log('wait condition is now true so code procedes')
})

Stop All

Syntax:

//stop all using conditional while loop- see example below: 

Example:

forever_condition = true
spriteA.whenFlag(function() {
    this.show()
   while (forever_condition === true){
      forever_condition = !stage.isKeyPressed ('space')
      this.say ("I'm in the forever loop- press space bar to stop loop")
      this.nextCostume()
      this.wait(0.1)
   }
   this.say ("I've broken out of the forever loop now and stopped code loop")
   console.log('code loop has stopped')
})

Clones

When I Start as a Clone:

Syntax:

whenCloned(function (){                                 
    this.alive = true
    this.addTo(stage)
    /*your code*/
    })

Create Clone:

Syntax:

myself.clone()

Example:

spriteA.whenFlag(function () {
    this.show()
    this.clone()
    this.goTo(-150,50)
    this.say('I am the original sprite')
    while(true){                                                   
     this.glide(3, -150, -50)
     this.glide(3, -150, 50)
    }       
})
spriteA.whenCloned(function (){
    this.wait(2)
    this.alive = true
    this.addTo(stage)
    this.goTo(0,0)
    this.say('I am the clone and I have my own code')
    while(true){                                                   
        this.glide(3, 100, 0)
        this.glide(3, 0, 0)
    }
})

Delete Clone:

Syntax:

removeFrom(stage)
alive = false

Example:

spriteD.whenFlag(function() {
    this.show()
    this.setSize(20)
    while(true){
        this.clone()
        this.wait(1)
    }
})
spriteD.whenCloned(function (){               
    this.alive = true
    this.addTo(stage)
    this.pointInDirection(randomRange(0,360))
    while(true){ 
        this.move(10)    
        if (this.isTouchingEdge() ){
            this.removeFrom(stage)          
            this.alive = false
        }
    }
})