| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- /**
- * Game Objects required: add Text object named "message"
- */
- //*****************Scene Object - Initialize When Scene Starts Event***************//
- // world set up and randomization
- if ($this.scene.state() == "PLAY") {
- // iterate 8 columns
- for (var x = 1; x < 8; x++) {
- // iterates 6 rows
- for (var y = 1; y < 6; y++) {
- // place wall at random locations
- if(random(100) < 25) {
- var clonedWall = wall.clone();
- // use wall width and height to place properly
- clonedWall.x(100 * x);
- clonedWall.y(100 * y)
- } else if (random(100) < 15) {
- var clonedCoin = coin.clone();
- clonedCoin.x(100 * x);
- clonedCoin.y(100 * y);
- }
-
- }
- }
- }
- //********************wall Object - Update Every Frame Event***********************//
- // remove wall if touching stairs
- if ($this.isTouching(stairs)) {
- $this.remove();
- }
- // if the wall is touching the ninja
- if ($this.isTouching(ninja)) {
- // check the direction of movement
- switch (ninja.moveDirection) {
- case ("up"):
- ninja.y(ninja.y() + 5);
- break;
- case("down"):
- ninja.y(ninja.y() - 5 );
- break;
- case("left"):
- ninja.x(ninja.x() + 5);
- break;
- case("right"):
- ninja.x(ninja.x() - 5);
- break;
- }
- }
- //*********************coin Object - Update Every Frame Event**********************//
- // remove coin if touching stairs
- if($this.isTouching(stairs)) {
- $this.remove();
- }
- // animate the coin
- $this.incrementAnimation();
- // if the ninja collects a coin
- // increment the ninja's coin total
- // and remove the coin
- if ($this.isTouching(ninja)) {
- ninja.coins += 1;
- $this.remove();
- }
- //*******************ninja Object - Update Every Frame Event***********************//
- // new method of moving sprite introduced
- // check to see what arrow keys are pressed
- var upPressed = isKeyPressed(Keys.upArrow) || isKeyPressed(Keys.w);
- var downPressed = isKeyPressed(Keys.downArrow) || isKeyPressed(Keys.s);
- var leftPressed = isKeyPressed(Keys.leftArrow) || isKeyPressed(Keys.a);
- var rightPressed = isKeyPressed(Keys.rightArrow) || isKeyPressed(Keys.d);
- // store the ninja's direction as a string
- $this.moveDirection = "";
- // determine which key is pressed
- // and update the moveDirection variable
- if (upPressed && $this.y() > 0) {
- $this.moveDirection = "up";
- }
- if (downPressed && $this.y() < 520) {
- $this.moveDirection = "down";
- }
- if (leftPressed && $this.x() >0) {
- $this.moveDirection = "left";
- }
- if (rightPressed && $this.x() < 720) {
- $this.moveDirection = "right";
- }
- // switch on moveDirection variable
- switch ($this.moveDirection) {
- // move the ninja down
- case "down":
- $this.moveY(100);
- break;
- // move the ninja up
- case "up":
- $this.moveY(-100);
- break;
- // move the ninja left
- case "left":
- $this.moveX(-100);
- break;
- // move the ninja right
- case "right":
- $this.moveX(100);
- break;
- }
- //******************ninja Object - Initialize When Scene Starts Event************//
- // set up variable to track coins
- $this.coins = 0;
- //*******************stairs Object - Update Every Frame Event********************//
- // if the ninja reaches the stairs
- if ($this.isTouching(ninja)) {
- message.text("You reached the exit with " + ninja.coins + " coins.");
- message.z(10);
- ninja.remove();
- }
|