[Posts in the debugging category need to have the following pieces of information included in order to help other teachers or our moderator team best help you. Feel free to delete everything in brackets before posting your request for debugging help.]
Link to the project or level: [https://studio.code.org/projects/gamelab/ztm3436Zv0ok7OSp1hGiUZMdqx0lf2LAEUZ6C50OKLY]
What I expect to happen: [
The player tries to to AVOID a laser that bounces off a ‘blank’ sprite (placed at 0, 200 at the very top of the background) to send it back toward the player. He or she has to ‘evade’ the laser in order to avoid loss of lives. If player is hit by the laser or an enemy ship they lose a life.
What we expect to happen is a normal speed of the laser bounce back rate which allows the player to have a chance at getting away and having a chance to play a spaceship game.
Please Note: As the student clicks to enter the game, the ‘space’ key fires the laser to hit the moving enemy spaceships for points.
]
What actually happens: [If you click play to enter the game, press ‘space’ to fire the lasers, and if un-comment out the code which I have inactivated… in order to help this student debug his issues — you can see any type of code to remove ONE life based on the laser hitting the player crashes the game. Instead of it taking away just ONE life as it should, any attempt for this lives = lives -1; causes GAME OVER instantly. We have not found any way to place it into the code without it crashing the game.]
What I’ve tried: [
function playerExplosion() {
laser1.bounceOff(blank);
if(player.isTouching(laser1)){
lives = lives-1;
playSound(“sound://category_explosion/8bit_explosion.mp3”, false);
resetPlayer();
}
if(player.isTouching(enemy1)){
lives = lives -1;
playSound(“sound://category_explosion/8bit_explosion.mp3”, false);
resetPlayer();
}
if(player.isTouching(enemy2)){
lives = lives -1;
playSound(“sound://category_explosion/8bit_explosion.mp3”, false);
resetPlayer();
}
if(player.isTouching(enemy3)){
lives = lives -1;
playSound(“sound://category_explosion/8bit_explosion.mp3”, false);
resetPlayer();
}
if(player.isTouching(enemy4)){
lives = lives -1;
playSound(“sound://category_explosion/8bit_explosion.mp3”, false);
resetPlayer();
}
}
function resetPlayer() {
player.x = randomNumber(50,350);
player.y = 300;
}
]
Please Note Relevant Code in Program (not in any particular order – but just for your information)
The main sprite is labeled 'player’
// This is the blank sprite in the background at the top which bounces the laser
var blank = createSprite(200,0);
blank.setAnimation(“blanks”);
blank.setCollider(“rectangle”);
//This is the laser related code here
var laser1= createGroup();
var laser1Index = 0;
laser1.add(createSprite(player.x,player.y));
laser1.setColliderEach(“circle”);
laser1.setVisibleEach(false);
laser1.setAnimationEach(“laserUp”);
laser1.setWidthEach(5);
laser1.setScaleEach(0.6);
function Laser1(){
if (keyDown(“space”)) {
laser1.setVisibleEach(true);
laser1.add(createSprite(player.x, player.y));
laser1.setAnimationEach(“laserUp”);
laser1Index = laser1Index + 1;
laser1.get(laser1Index).velocityY = -10;
playSound(“sound://category_projectile/retro_game_weapon_-laser_shot_single.mp3”, false);
} if (laser1.y < 0){
laser1.setVisibleEach(false);
}
}
//Game over code
function gameover() {
if (lives <= 0) {
player.visible = false;
enemy1.visible = false;
enemy1.velocityX = 0;
enemy2.visible = false;
enemy2.velocityX = 0;
enemy3.visible = false;
enemy3.velocityX = 0;
enemy4.visible = false;
enemy4.velocityX = 0;
loser.visible = true;
loser2.visible = true;
fill(“yellow”);
textSize(17);
background(“black”);
fill(“red”);
textSize(50);
text(“Game over”, 100, 200);
}
}
//The player starts out with three lives
var lives = 3;