Home > Software design >  I do not know how to limit the time in between calling my function
I do not know how to limit the time in between calling my function

Time:05-18

  
//let screenWidth = window.screen.width;
//let screenHeight = window.screen.height;
let screenWidth = 800;
let screenHeight = 600;
let assets = {};
let frames = 60;

let score = 0;
let lives = 3;
let player;
// let enemie;
//let enemies;
let bullet;
//let bullets;
let powerup = 0;
let gameOver = true;

function drawScoreBoard() {
  textSize(20);
  fill('white');
  text(`Score: ${score} / Lives: ${lives}`, 20, 40);
}

function preload() {
  assets.player = loadImage('assets/Player.png');
  assets.enemie = loadImage('assets/Enemie.png');
  assets.bulletRight = loadImage('assets/Bullet_Right.png');
  assets.bulletLeft = loadImage('assets/Bullet_Left.png');
  assets.bulletUp = loadImage('assets/Bullet_Up.png');
  assets.bulletDown = loadImage('assets/Bullet_Down.png');
}

function setup() {
  bullets = createGroup();
  enemies = createGroup();
  assets.player.resize(30, 30);
  assets.enemie.resize(30, 30);
  assets.bulletRight.resize(30, 30);
  assets.bulletLeft.resize(30, 30);
  assets.bulletUp.resize(30, 30);
  assets.bulletDown.resize(30, 30);
  createCanvas(screenWidth, screenHeight);
}

function createBullet(){
    let numList = [0, 90, 180, 270, 360];
    let bulletDirection = [assets.bulletLeft, assets.bulletUp, assets.bulletRight, assets.bulletDown];
    let randomdirection = numList[Math.floor(Math.random() * numList.length)];
    let bullet = createSprite(bulletDirection[(Math.round(player.getDirection()/90))]);
    
    enemie.centerX = random(0, screenWidth);
    enemie.setSpeed(random(1,10));
    enemie.setDirection(randomdirection);
    enemie.setCollider("circle");

    bullets.add(bullet);
}

function createPlayer(){
  player = createSprite(assets.player);
  player.bottom = screenHeight - 20;
  player.centerX = screenWidth / 2;
}


function shoot(amountofbulletstobeshot) {
  let bulletDirection = [assets.bulletLeft, assets.bulletUp, assets.bulletRight, assets.bulletDown];
  let bullet = createSprite(bulletDirection[Math.abs(((Math.round(player.getDirection()/90))))]);
  bullets.add(bullet);
//  bullet.direction = player.direction;
  bullet.centerX = player.centerX;
  bullet.centerY = player.centerY;
  bullet.setVelocity(11, player.getDirection());

//  console.log('The players current direction right now is:  '   player.getDirection());

}
function shooting() {
  if (keyIsDown(KEY.SPACE)) {
    if (powerup === 1) {
      shoot(3);
    }
    else {
      shoot(1);
    }
  }
    if (bullet) {
      if (bullet.centerX[1] === screenWidth) {
        bullets.remove(bullet);
    }
  }
}

function updateplayer() {
  //movement
  if (keyIsDown) {
    if (keyIsDown(KEY.RIGHT_ARROW)) {
      player.setVelocity(6, 0);
    }
    if (keyIsDown(KEY.LEFT_ARROW)) {
      player.setVelocity(6, 180);
    }
    if (keyIsDown(KEY.UP_ARROW)) {
      player.setVelocity(6, 270);
    }
    if (keyIsDown(KEY.DOWN_ARROW)) {
      player.setVelocity(6, 90);
    }
  }

  //dont go offscreen
  if (player.left < 0) {
    player.left = 0;
  }
  if (player.right > screenWidth) {
    player.right = screenWidth;
  }
  if (player.top < 0) {
    player.top = 0;
  }
  if (player.bottom > screenHeight) {
    player.bottom = screenHeight;
  }
  enemies.overlap(player, HandlePlayerEnemieCollision);
  //end up updateplayer
}
function updateEnemie() {
if (frameCount % 1 === 0) {
    let directions = ["LEFT", "RIGHT", "UP", "DOWN"];
    let direction = random(directions);
    if (direction === "LEFT" && enemie.left > 0) {
      enemie.centerX -= 5;
    }
    if (direction === "RIGHT" && enemie.right < screenWidth) {
      enemie.centerX  = 5;
    }
    if (direction === "UP" && enemie.top > 0) {
      enemie.centerY -= 5;
    }
    if (direction === "DOWN" && enemie.bottom < screenHeight) {
      enemie.centerY  = 5;
    }

}
}

function createEnemie() {
  let directions = [270, 180, 0, 90];
  direction = directions[(Math.floor(Math.random() * 5))];
  enemies.overlap(bullets, HandleEnemieBulletCollision);
  if (frameCount % 60 === 0) {
  enemie = createSprite(assets.enemie);
    enemie.centerX = Math.floor(Math.random() * 300)   100;
    enemie.centerY = Math.floor(Math.random() * 300)   100;
    enemie.setVelocity(Math.floor(Math.random() * 5)   1, direction);
    enemies.add(enemie);
  }
}

function HandleEnemieEdgeCollision(enemie, edge) {
  if (enemie.centerY === screenWidth) {
    enemie.remove();
  }
}

function HandleEnemieBulletCollision(enemie, bullet) {
   enemie.remove();
   bullet.remove();
   score  ;
}

function HandlePlayerEnemieCollision(player, enemie) {
  enemie.remove();
  player.remove();
  lives--;
  if (lives === 0) {
        gameOver = true;
    }
  createPlayer();
}

/*
function updateEnemie() {
  player.setVelocity(7, player.direction);
}
*/

function cheat() {
  score = (score   1000000);
  lives = (lives   1000000);
  cheats = 'on';
  if (cheats === 'on') {
    textSize(50);
    fill('yellow');
    text('CHEATS ACTIVATED', 400, 300);
  }
}

/*
function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i  ) {
    if ((new Date().getTime() - start) > milliseconds) {
      break;
    }
  }
}
*/


function drawGameOverScreen() {
    background("black");
    textAlign(CENTER);
    fill("white");
    textSize(40);
    text("WELCOME", screenWidth/2, screenHeight/2);
    textSize(20);
    text("Press SPACE to play!", screenWidth/2, screenHeight/2   100);

    if (keyWentDown(KEY.SPACE)){
        resetGame();
        gameOver = false;
    }
}

function resetGame(){
    score = 0; 
    lives = 3;

    createPlayer();
}

function drawGame() {
   background('lightgreen');
  drawScoreBoard();
  updateplayer();
  drawSprites();
  shooting();
//  updateEnemie();
  createEnemie();  
}

function draw() {
  
  if (gameOver === true) {
        drawGameOverScreen();
    } else {
        drawGame();
    }
}

There is my code, I want to add a time limit for how many seconds it takes until you can shoot again, right now you can just spam and make all of the bullets go in a straight line I need to wait before I let the player shoot again, Thanks! This will help out allot and i neede this in as few minutes as possible.

CodePudding user response:

Use a flag to know when shooting is enabled and disabled. After each shot disable the ability to shoot. Then use a setTimeout to enable the shooting after a certain amount of time.

let shootingEnabled = true;

function tempDisableShooting(duration) {
  shootingEnabled = false;

  setTimeout(() => {
    shootingEnabled = true;
  }, duration);
}

function shooting() {
  if (shootingEnabled && keyIsDown(KEY.SPACE)) {
    if (powerup === 1) {
      shoot(3);
    } else {
      shoot(1);
    }

    tempDisableShooting(1000); // Disable shooting for 1 second.
  }

  if (bullet) {
    if (bullet.centerX[1] === screenWidth) {
      bullets.remove(bullet);
    }
  }
}

CodePudding user response:

Add property to track the disabled state of the shooting and a constant for the delay time in milliseconds.

let shootDisabled = false;
const shootDelayMS = 300;

Update your shoot function to only shoot if shootDisabled == false, update the shootDisabled to true after each shot, and to triggers a setTimeout which waits shootDelayMS milliseconds before setting shootDisabled to false again.

function shoot(amountofbulletstobeshot) {
  if (shootDisabled == false) {
    let bulletDirection = [assets.bulletLeft, assets.bulletUp, assets.bulletRight, assets.bulletDown];
    let bullet = createSprite(bulletDirection[Math.abs(((Math.round(player.getDirection()/90))))]);
    bullets.add(bullet);
    bullet.centerX = player.centerX;
    bullet.centerY = player.centerY;
    bullet.setVelocity(11, player.getDirection());

    shootDisabled = true;
    setTimeout(() => shootDisabled = false, shootDelayMS );
  }
}

CodePudding user response:

one way is the hold the data in two variables: is_shooting, shot_reset.

if(is_shooting){
    if(shot_reset>=x){ //x is frame count till able to shoot again)
        shot_reset==0;
        is_shooting=false;
    }else{
        shot_reset  
    }

}
  • Related