I'm trying to create a small game that has a soldier moving around a small area and fires bullets. I'm a new programmer who just started javascript not but a few months ago, and so I apologize if my question is poorly worded or my code is clunky.
I've tried using [group].get(i) to isolate each individual sprite that comes out of the gun, but it simply breaks and crashes the program.
Here is all of the code for my (small) program:
var soldier = createSprite(200, 200);
soldier.setAnimation("soldier_still");
var bullet = createGroup();
function draw() {
background(rgb(100, 85, 45));
move();
attack();
drawSprites();
}
function attack() {
if (mouseWentDown("leftButton")) {
for (var i = 0; i < 3; i++) {
bullet.add(createSprite(soldier.x, soldier.y));
bullet.setAnimationEach("bullet");
bullet.pointToEach(World.mouseX, World.mouseY);
bullet.setLifetimeEach(50);
bullet.setSpeedAndDirectionEach(20, World.mouseX, World.mouseY);
}
}
}
function move() {
createEdgeSprites();
World.allSprites.bounceOff(topEdge);
World.allSprites.bounceOff(bottomEdge);
World.allSprites.bounceOff(leftEdge);
World.allSprites.bounceOff(rightEdge);
soldier.pointTo (World.mouseX,World.mouseY);
if (keyDown() == false) {
soldier.setAnimation("soldier_still");
soldier.velocityX = 0;
soldier.velocityY = 0;
}
if (keyDown("up")) {
soldier.setAnimation("soldier_move");
soldier.velocityY = -5;
}
if (keyDown("down")) {
soldier.setAnimation("soldier_move");
soldier.velocityY = 5;
}
if (keyDown("left")) {
soldier.setAnimation("soldier_move");
soldier.velocityX = -5;
}
if (keyDown("right")) {
soldier.setAnimation("soldier_move");
soldier.velocityX = 5;
}
}
The program either crashes, or all bullet sprites face the mouse's position. Is there anyway I can improve my code, or a solution to making each group (bullet) sprite its own individual sprite?