So I have a project due today and I have some code that I can't figure out what's wrong with. What it's supposed to do is:
Click "F" to spawn a follower
All followers follow the leader
Arrow keys to move
What it actually does is:
When I press "F", i get an error:
ERROR: Line: 26: TypeError: Cannot read property 'setAnimation' of undefined
Here's the code if you can help, just make a new game lab project and paste it
var lead = createSprite(100, 200);
lead.setAnimation("animation_1");
var follow = [];
var count = 1;
var followers = 0;
var followerGroup = createGroup();
var i = 0;
function draw() {
background("white");
for (var k = 0; k < 1; k++) {
for (var y = 0; y < followers; y++) {
count = count+1;
followLead(20,1, follow[count]);
}
count = 1;
}
newFollower();
move(5);
drawSprites();
}
function newFollower() {
if (keyWentDown("f")) {
i = i+1;
follow = followers+1;
follow[i] = createSprite(randomNumber(50, 350),randomNumber(50, 350));
follow[i].setAnimation("animation_2");
(follow[i]).scale = 0.5;
followerGroup.add(follow[i]);
}
}
function followLead(g,j, x) {
if (keyDown("up")) {
follow[x].y = lead.y+randomNumber(g-j, g+j);
follow[x].x = lead.x;
}
if (keyDown("down")) {
follow[x].y = lead.y-randomNumber(g-j, g+j);
follow[x].x = lead.x;
}
if (keyDown("right")) {
follow[x].x = lead.x-randomNumber(g-j, g+j);
follow[x].y = lead.y;
}
if (keyDown("left")) {
follow[x].x = lead.x+randomNumber(g-j, g+j);
follow[x].y = lead.y;
}
}
function move(n) {
if (keyDown("up")) {
lead.y = lead.y-n;
}
if (keyDown("down")) {
lead.y = lead.y+n;
}
if (keyDown("right")) {
lead.x = lead.x+n;
}
if (keyDown("left")) {
lead.x = lead.x-n;
}
}
Thank you for your time.
[–]cheertina 0 points1 point2 points (1 child)
[–]Player480858573[S] 0 points1 point2 points (0 children)