| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /**
- Ninjas are presented with a completely blank scene and will have to build an entire
- activity from the ground up using everything that they’ve learned in Yellow Belt.
- */
- //*********************Scene Object - Initialize When Scene Starts Event***********//
- if ($this.scene.state() == "PLAY") {
- var nrock = rocks.clone();
- nrock.x(random(800));
- nrock.y(random(600));
- }
- //**************rock Object - Initialize When Scene Starts Event******************//
- if ($this.scene.state() == "PLAY") {
- $this.speedX(random(50,-50));
- $this.speedY(random(50,-50));
- $this.rot = random(30,-30);
- }
- //******************rock Object - Update Every Frame Event*************************//
- moveX($this);
- moveY($this);
- $this.spin($this.rot);
- // keep rocks in the scene
- if($this.y() > 600) {
- $this.y(0);
- }
- if($this.y() < 0) {
- $this.y(600);
- }
- if($this.x() > 800) {
- $this.x(0);
- }
- if($this.x() < 0) {
- $this.x(800);
- }
- // collision check
- //find the bullet on the scene
- var bullet = $this.scene.projectile;
- if(bullet && $this.isTouching(bullet)) {
- //instead of making the variable, we add to the score directly
- lblScore.text(parseInt(lblScore.text() + 10));
- $this.scaleX($this.scaleX()/2); //start to reduce the rock by 1/2
- $this.scaleY($this.scaleY()/2);
- $this.offsetX($this.offsetX()/2);
- $this.offsetY($this.offsetY()/2);
- if($this.scaleX() >= 0.125) {
- var nrock = $this.clone(); //split the rock as long as as it's bigger than 0.125
- } else {
- $this.remove();
- }
- $this.scene.projectile = null; //remove bullet and reseet value of scene projectile
- bullet.remove();
- }
- //*******************rocket Group - Update Every Frame Event***********************//
- var leftPressed = isKeyPressed(Keys.leftArrow);
- var rightPressed = isKeyPressed(Keys.rightArrow);
- var upPressed = isKeyPressed(Keys.upArrow);
- if(upPressed) { // if pressed, move rocket forward
- $this.moveForwardByRotation();
- }
- if(leftPressed) { // if pressed, rotate to the left
- $this.spin(-40);
- }
- if(rightPressed) {
- $this.spin(40);
- }
- // keep rocket in the scene
- if ($this.y() > 600) {
- $this.y(0);
- }
- if ($this.y() < 0) {
- $this.y(600);
- }
- if ($this.x()>800) {
- $this.x(0);
- }
- if( $this.x()<0) {
- $this.x(800);
- }
- // projectile functionality - rocket group talks to bullet, creates a 'moving' boolean
- //bullet is listening for moving to be true
- var spacePresssed = isKeyPressed(Keys.space)
- var bullet = $this.findName("bullet"); //find the active bullet on the scene
- if(spacePressed) {
- if(!$this.scene.projectile) {
- var b = bullet.clone(true, true, $this.scene); //cloning the bullet from bullet
- b.x(bullet.getStagePos().x); //getting the x,y and rotation in the scene
- b.y(bullet.getStagePos().y);
- b.rotation($this.rotation());
- b.moving = true; //set boolean to true so bullet will excecute the code when moving is true
- b.z(1000); //ensure it's always on top of scene
- $this.scene.projectile = b;
- }
- }
- //*****************spaceShip Object - Update Every Frame Event********************//
- // animation code
- var upPressed = isKeyPressed(Keys.upArrow);
- if (upPressed) {
- $this.animation("thrust");
- $this.incrementAnimation();
-
- } else {
- $this.animation("default");
-
- }
- //******************bullet Object - Update Every Frame Event**********************//
- //the moving boolean created from rocket group and so it knows to shoot when the
- //space bar is pressed
- if ($this.moving) {
- $this.moveForwardByRotation();
- // condition here will prevent overloading the screen with bullets,
- // it checks for current bullets on the screen
- if ($this.y() > 600 || $this.y() < 0 || $this.x() < 0 || $this.x() > 800) {
- $this.scene.projectile = null;
- $this.remove(); // rock is removed when collision occurs
- }
- }
- /**
- Ninjas are presented with a completely blank scene and will have to build an entire
- activity from the ground up using everything that they’ve learned in Yellow Belt.
- */
- //*********************Scene Object - Initialize When Scene Starts Event***********//
- if ($this.scene.state() == "PLAY") {
- var nrock = rocks.clone();
- nrock.x(random(800));
- nrock.y(random(600));
- }
- //**************rock Object - Initialize When Scene Starts Event******************//
- if ($this.scene.state() == "PLAY") {
- $this.speedX(random(50,-50));
- $this.speedY(random(50,-50));
- $this.rot = random(30,-30);
- }
- //******************rock Object - Update Every Frame Event*************************//
- moveX($this);
- moveY($this);
- $this.spin($this.rot);
- // keep rocks in the scene
- if($this.y() > 600) {
- $this.y(0);
- }
- if($this.y() < 0) {
- $this.y(600);
- }
- if($this.x() > 800) {
- $this.x(0);
- }
- if($this.x() < 0) {
- $this.x(800);
- }
- // collision check
- //find the bullet on the scene
- var bullet = $this.scene.projectile;
- if(bullet && $this.isTouching(bullet)) {
- //instead of making the variable, we add to the score directly
- lblScore.text(parseInt(lblScore.text() + 10));
- $this.scaleX($this.scaleX()/2); //start to reduce the rock by 1/2
- $this.scaleY($this.scaleY()/2);
- $this.offsetX($this.offsetX()/2);
- $this.offsetY($this.offsetY()/2);
- if($this.scaleX() >= 0.125) {
- var nrock = $this.clone(); //split the rock as long as as it's bigger than 0.125
- } else {
- $this.remove();
- }
- $this.scene.projectile = null; //remove bullet and reseet value of scene projectile
- bullet.remove();
- }
- //*******************rocket Group - Update Every Frame Event***********************//
- var leftPressed = isKeyPressed(Keys.leftArrow);
- var rightPressed = isKeyPressed(Keys.rightArrow);
- var upPressed = isKeyPressed(Keys.upArrow);
- if(upPressed) { // if pressed, move rocket forward
- $this.moveForwardByRotation();
- }
- if(leftPressed) { // if pressed, rotate to the left
- $this.spin(-40);
- }
- if(rightPressed) {
- $this.spin(40);
- }
- // keep rocket in the scene
- if ($this.y() > 600) {
- $this.y(0);
- }
- if ($this.y() < 0) {
- $this.y(600);
- }
- if ($this.x()>800) {
- $this.x(0);
- }
- if( $this.x()<0) {
- $this.x(800);
- }
- // projectile functionality - rocket group talks to bullet, creates a 'moving' boolean
- //bullet is listening for moving to be true
- var spacePresssed = isKeyPressed(Keys.space)
- var bullet = $this.findName("bullet"); //find the active bullet on the scene
- if(spacePressed) {
- if(!$this.scene.projectile) {
- var b = bullet.clone(true, true, $this.scene); //cloning the bullet from bullet
- b.x(bullet.getStagePos().x); //getting the x,y and rotation in the scene
- b.y(bullet.getStagePos().y);
- b.rotation($this.rotation());
- b.moving = true; //set boolean to true so bullet will excecute the code when moving is true
- b.z(1000); //ensure it's always on top of scene
- $this.scene.projectile = b;
- }
- }
- //*****************spaceShip Object - Update Every Frame Event********************//
- // animation code
- var upPressed = isKeyPressed(Keys.upArrow);
- if (upPressed) {
- $this.animation("thrust");
- $this.incrementAnimation();
-
- } else {
- $this.animation("default");
-
- }
- //******************bullet Object - Update Every Frame Event**********************//
- //the moving boolean created from rocket group and so it knows to shoot when the
- //space bar is pressed
- if ($this.moving) {
- $this.moveForwardByRotation();
- // condition here will prevent overloading the screen with bullets,
- // it checks for current bullets on the screen
- if ($this.y() > 600 || $this.y() < 0 || $this.x() < 0 || $this.x() > 800) {
- $this.scene.projectile = null;
- $this.remove(); // rock is removed when collision occurs
- }
- }
|