Examples

Dodge Ball

It is highly recommended that you directly load the JS4Scratch examples by going to ScratchLink Coding Editor then start a JS4Scratch project on the left, click on the help on the right hand side then click on"Examples" and explore how it works in the Editor.

JS4Scratch Example: Dodge Ball Beginner

Global Variables Tab:

Forever(Boolean)

xPos(Number)

Sprites Tab:

Player

Ball

backdrop: stars

Ball.js Tab:

Ball.whenFlag(function(){
    this.wait(2)
    this.setSize(20)
    while (Forever === true) {
        this.clone()
        this.wait(randomRange(0.1,1.5))
    }
})

Ball.whenCloned(function(){
    this.alive = true
    this.addTo(stage)
    this.goTo(randomRange(-210,210),160)
    this.show()
    while(Forever === true) {
        this.changeY(-10)
        if (this.isTouching(Player)) {
            Forever = false
            this.say('Game Over')
        }
    }
})

Player.js Tab:

Player.whenFlag(function(){
    Forever = true
    xPos = 0
    this.show()
    this.setSize(50)
    this.goTo(0,-160)
    this.sayWait('Dodge the balls - arrow keys to move',2)
    while(Forever === true){
        if (stage.isKeyPressed('ArrowRight')) {
            xPos = xPos + 8
            if (xPos > 210) {
                xPos = 210
            }
        }
        if (stage.isKeyPressed('ArrowLeft')) {
            xPos = xPos - 8
            if (xPos < -210) {
                xPos = -210
            }
        }
        this.goTo(xPos,-160)
    }
})