Blue sprite breaks when a coin flip is in its favor

33 views Asked by At

My coin flip for a project in code.org doesnt work, when the red sprite gets heads, it works. However, when the coin is in the blue sprites favor, it dosent destory the red sprite. I have the whole code here, the coin flip is at the bottom of

draw()

here is all the code i have to offer:

// call vars
var blu = createSprite(100,170);
blu.setAnimation("playerSP");
blu.x = 100;
blu.y = 170;
blu.tint = "blue";

var red = createSprite(328,178);
red.setAnimation("playerSP");
red.x = 328;
red.y = 178;
red.tint = "red";

var wall = createSprite(200,200);

var coin = randomNumber(0,1);

// logs the coin flip
// 1 = heads
// 0 = tails
console.log(coin);

//draw() is exclusive for code.org, its just loops code

function draw() {
  background("white");

  
  
  // WASD configurations for blue.
  if(keyDown("w") === true) {
    blu.y = blu.y  - 2;
    //console.log("'W' pressed");
  }
  if(keyDown("a") === true) {
    blu.x = blu.x - 2;
    //console.log("'A' pressed");
  }
  if(keyDown("s") === true) {
    blu.y = blu.y + 2;
   // console.log("'S' pressed");
  }
  if(keyDown("d") === true) {
    blu.x = blu.x + 2;
  //  console.log("'D' pressed");
  }
  
  
  // I have no idea why i added arrow and wasd config for both users. 
    // arrow configurations for blue.
  /*if(keyDown("up") === true) {
    blu.y = blu.y  - 2;
  }
  if(keyDown("left") === true) {
    blu.x = blu.x - 2;
  }
  if(keyDown("down") === true) {
    blu.y = blu.y + 2;
  }
  if(keyDown("right") === true) {
    blu.x = blu.x + 2;
  }*/
  
  
    // WASD configurations for red.
  /*if(keyDown("w") === true) {
    red.y = red.y  - 2;
  }
  if(keyDown("a") === true) {
    red.x = red.x - 2;
  }
  if(keyDown("s") === true) {
    red.y = red + 2;
  }
  if(keyDown("d") === true) {
    red.x = red.x + 2;
  }*/
    // arrow configurations for red.
  if(keyDown("up") === true) {
    red.y = red.y  - 2;
//    console.log("'UP_ARROW' pressed");
  }
  if(keyDown("left") === true) {
    red.x = red.x - 2;
//    console.log("'LEFT_ARROW' pressed");
  }
  if(keyDown("down") === true) {
    red.y = red.y + 2;
    //console.log("'DOWN_ARROW' pressed");
  }
  if(keyDown("right") === true) {
    red.x = red.x + 2;
    //console.log("'RIGHT_ARROW' pressed");
  }
  
  // check for collides
  if(red.x === 400) {
    red.x = 200;
    red.y = 200;
  }
  if(red.y === 400) {
    red.x = 200;
    red.y = 200;
  }
  if(red.x === 0) {
    red.x = 200;
    red.y = 200;
  }
  if(red.y === 0) {
    red.x = 200;
    red.y = 200;
  }
  
  
  if(blu.x === 400) {
    blu.x = 200;
    blu.y = 200;
  }
  if(blu.y === 400) {
    blu.x = 200;
    blu.y = 200;
  }
  if(blu.x === 0) {
    blu.x = 200;
    blu.y = 200;
  }
  if(blu.y === 0) {
    blu.x = 200;
    blu.y = 200;
  }
  
  
  // coin flip
   //TODO: turn into a elseif statement
   if(coin === 1) {
     if(red.isTouching(blu)) {
    blu.destroy();   
    console.log("red wins!");
     }
     else if(coin === 0) {
      if(blu.isTouching(red)) {
      red.destroy();   
      console.log("blu wins!");
     }
   }
  }
      drawSprites();
  }// <---- dont touch

   
   
  /*if(blu.isTouching(red)) {
    red.alpha = 0;
    console.log("blu wins!");
  }*/
    
  
   if(blu.isTouching(wall)) {
    blu.x = blu.x - 2;
  }

Thank you.

What I wanted: For blue sprite to get tagger. What happend: The blue collision broke.

0

There are 0 answers