Only one sprite moves

I’m trying to set an X velocity so that enemies move. The problem is that when this is implemented, only one enemy moves (the enemy that is loaded in last).

Any solutions?

Can you post the code?

function preload() {

game.load.baseURL = 'joshmuszka67.wixsite.com/gamesprites';
game.load.crossOrigin = 'anonymous';

game.load.image("background", "https://static.wixstatic.com/media/aa8ebd_a3ab2ab9b1eb4926adeedab556f2a5cf~mv2_d_4096_3904_s_4_2.png/v1/fill/w_638,h_604,al_c,q_85,usm_0.66_1.00_0.01/updated%20scenery%204-1_png.webp");

game.load.image('player', 'https://static.wixstatic.com/media/aa8ebd_7a550c17a07b4dd1a743e556cfe04000~mv2.png/v1/fill/w_68,h_68,al_c,lg_1,q_80/Player-1_png%20(2).webp');

game.load.image('platform', 'https://static.wixstatic.com/media/aa8ebd_15f9bce00f524d2a98ccde509dece027~mv2.png/v1/fill/w_638,h_60,al_c,q_80,usm_0.66_1.00_0.01/upscaled_Foreground-1_png%20(1).webp');

game.load.image('enemy1', 'https://static.wixstatic.com/media/aa8ebd_2447d920490847648a736253bd76e083~mv2.gif');

game.load.spritesheet('enemy2', 'https://static.wixstatic.com/media/aa8ebd_6c73cf819ff34bc78ba6ff2d0e95f5be~mv2.png/v1/fill/w_145,h_272,al_c,lg_1,q_80/new%20Enemy%202.webp', 145, 64);

game.load.spritesheet('enemyii', '<img src="https://static.wixstatic.com/media/aa8ebd_648ca2b0d9d1414c8a754ed18c80e13b~mv2.gif" alt="Enemy 2 (1).gif"/>')

game.load.spritesheet('walk', "https://static.wixstatic.com/media/aa8ebd_72560045f4e94f46b29b17c07d0104f2~mv2.png/v1/fill/w_320,h_64,al_c,q_80/Player%20sprite%20(1).webp", 64, 64);

game.load.image('platform1', 'https://static.wixstatic.com/media/aa8ebd_8b88721a00a9488f94733a5de399d507~mv2.png/v1/fill/w_68,h_34,al_c,lg_1,q_80/upscaled_newplatform1-1_png%20(1).webp');

game.load.image('platform2', 'https://static.wixstatic.com/media/aa8ebd_6898be6bc22c418bbaa2b057b1030c1f~mv2.png/v1/fill/w_136,h_34,al_c,lg_1,q_80/upscaled_newplatform2-1_png.webp');

game.load.image('platform3', 'https://static.wixstatic.com/media/aa8ebd_3f0855eeeea54424a1738e5fb17f1b92~mv2.png/v1/fill/w_204,h_34,al_c,lg_1,q_80/upscaled_newplatform3-1_png.webp');

game.load.image('platform4', 'https://static.wixstatic.com/media/aa8ebd_25b09e09171b434e9407b54532a1e477~mv2.png/v1/fill/w_272,h_34,al_c,lg_1,q_80/upscaled_newplatform4-1_png.webp');

game.load.image('platform5', 'https://static.wixstatic.com/media/aa8ebd_d47e63152e144ababfb0b6cf0e45c6b7~mv2.png/v1/fill/w_340,h_34,al_c,lg_1,q_80/upscaled_newplatform5-1_png.webp');

game.load.image('foreground', 'https://static.wixstatic.com/media/aa8ebd_efa0fa99c51f4e43ab80dba2fba4065f~mv2.png/v1/fill/w_638,h_359,al_c,q_80,usm_0.66_1.00_0.01/Foreground-1_png%20(1).webp');

game.load.spritesheet('enemya', 'https://static.wixstatic.com/media/aa8ebd_7912694b6be44081a43e17a7cea9f510~mv2.png/v1/fill/w_34,h_136,al_c,lg_1,q_80/Enemy%201.webp', 32, 32);

game.load.spritesheet('back', 'https://static.wixstatic.com/media/aa8ebd_17cb8f669db244e39cda0473e12221b8~mv2.png/v1/fill/w_340,h_68,al_c,lg_1,q_80/Player%20sprite%20left.webp', 64, 64);

game.load.spritesheet('allAnimations', 'https://static.wixstatic.com/media/aa8ebd_340cfb5e890249878669fd04bcc01ded~mv2.png/v1/fill/w_68,h_680,al_c,lg_1,q_80/all%20player%20animations.webp', 64, 68, 10);

}

var player;
var platforms;
var cursors;
var jumpButton;
var background;
var enemy1;
var enemy2;
var health;
var platform1;
var platform2;
var platform3;
var platform4;
var platform5;
var emerald;
var hearts;
var heart;
var ladder;
var walk;
var back;
var allAnimations;
var enemyii;
var enemy1b;
var enemy1Group;
var enemyGroup;
var enemya;

function create() {

//World Boundaries, they keep you safe!
game.world.setBounds(0, 0, 99999999999999999999, 547);

//Background Lives Here :)
background = game.add.tileSprite(0, 0, 9999999999, 576, "background");

platforms = game.add.physicsGroup();
        platforms.create(320, 440, 'platform1');
        platforms.create(520, 370, 'platform5');
        platforms.create(100, 100, 'platform2');
    platforms.setAll('body.immovable', true);
    
//Player Spawns here
player = game.add.sprite(50, 512, 'allAnimations');
//player.frame = 0;
player.animations.add('animationWalkRight', [1,2,3,4], 9, true);
player.animations.add('animationWalkLeft', [5,6,7,8], 9, true);
player.animations.add('animationJump', [9], 0, true);
game.physics.arcade.enable(player);

player.body.collideWorldBounds = true;
player.body.gravity.y = 2000;

//Enemy spawns here
enemy1 = game.add.sprite(650, 515, 'enemya')
    enemy1.animations.add('enemyAnimation',[0,1,2,3], 2, true);
    game.physics.arcade.enable(enemy1);
    enemy1.body.collideWorldBounds = true;
    enemy1.body.gravity.y = 2000;

enemy1 = game.add.sprite(148, 68, 'enemy1');
    game.physics.arcade.enable(enemy1);
    enemy1.body.collideWorldBounds = true;
    enemy1.body.gravity.y = 2000;

//enemy2 = game.add.sprite(200, 200, 'enemy2');

cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

game.camera.follow(player);

}

function update () {
game.physics.arcade.collide(player, platforms);

player.body.velocity.x = 0;

enemy1.body.velocity.x = -40;
enemy1.animations.play('enemyAnimation');

if (cursors.right.isDown)
{
    player.body.velocity.x = 180;
    player.animations.play('animationWalkRight');
}

else if (cursors.left.isDown)
{
    player.body.velocity.x = -180;
    player.animations.play('animationWalkLeft');

}
else if (cursors.right.isUp)
{
    player.animations.stop('animationWalkRight');
    player.frame = 0;
}

 else if (cursors.left.isUp)
{
    player.animations.stop('animationWalkLeft');
    player.frame = 0;
}
if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down))
{
    player.body.velocity.y = -740;
}
if (jumpButton.isDown)
{
    player.animations.play('animationJump');
}

}

function worldMovementControls() {
if (!this.camera.atlimit.x) {
background.tilePosition.x -= (player.body.velocity.y * this.physicsElapsed);
}

}

function render() {

}

One thing I see off the bat is that you are assigning two different sprites to the same variable, enemy1, here:

enemy1 = game.add.sprite(650, 515, 'enemya')
    enemy1.animations.add('enemyAnimation',[0,1,2,3], 2, true);
    game.physics.arcade.enable(enemy1);
    enemy1.body.collideWorldBounds = true;
    enemy1.body.gravity.y = 2000;

enemy1 = game.add.sprite(148, 68, 'enemy1');
    game.physics.arcade.enable(enemy1);
    enemy1.body.collideWorldBounds = true;
    enemy1.body.gravity.y = 2000;

I am not sure if that was just done for testing, though.

Oh, thanks for spotting that mistake.

I tried changing them both to enemy1, and then changing them both to enemya, and the same problem occurs, no matter what.

But are you saving the references into two different variables? As it stands, you are losing the reference to your first enemy so when you try to set a value on the enemy1 value, only the second one will get it.

I’ve changed all references to enemya, but the same problem occurs.

Can you post just your update function again after the changes?

function update () {
game.physics.arcade.collide(player, platforms);

player.body.velocity.x = 0;

enemya.body.velocity.x = -40;
enemya.animations.play('enemyAnimation');

if (cursors.right.isDown)
{
    player.body.velocity.x = 180;
    player.animations.play('animationWalkRight');
}

else if (cursors.left.isDown)
{
    player.body.velocity.x = -180;
    player.animations.play('animationWalkLeft');

}
else if (cursors.right.isUp)
{
    player.animations.stop('animationWalkRight');
    player.frame = 0;
}

 else if (cursors.left.isUp)
{
    player.animations.stop('animationWalkLeft');
    player.frame = 0;
}
if (jumpButton.isDown && (player.body.onFloor() || player.body.touching.down))
{
    player.body.velocity.y = -740;
}
if (jumpButton.isDown)
{
    player.animations.play('animationJump');
}

}

function worldMovementControls() {
if (!this.camera.atlimit.x) {
background.tilePosition.x -= (player.body.velocity.y * this.physicsElapsed);
}

}

Oh crap! Sorry, I meant your create function :stuck_out_tongue:

Ah, no worries.

var player;
var platforms;
var cursors;
var jumpButton;
var background;
var enemy1;
var enemy2;
var health;
var platform1;
var platform2;
var platform3;
var platform4;
var platform5;
var emerald;
var hearts;
var heart;
var ladder;
var walk;
var back;
var allAnimations;
var enemyii;
var enemy1b;
var enemy1Group;
var enemyGroup;
var enemya;

function create() {

//World Boundaries, they keep you safe!
game.world.setBounds(0, 0, 99999999999999999999, 547);

//Background Lives Here :)
background = game.add.tileSprite(0, 0, 9999999999, 576, "background");

platforms = game.add.physicsGroup();
        platforms.create(320, 440, 'platform1');
        platforms.create(520, 370, 'platform5');
        platforms.create(100, 100, 'platform2');
    platforms.setAll('body.immovable', true);
    
//Player Spawns here
player = game.add.sprite(50, 512, 'allAnimations');
//player.frame = 0;
player.animations.add('animationWalkRight', [1,2,3,4], 9, true);
player.animations.add('animationWalkLeft', [5,6,7,8], 9, true);
player.animations.add('animationJump', [9], 0, true);
game.physics.arcade.enable(player);

player.body.collideWorldBounds = true;
player.body.gravity.y = 2000;

//Enemy spawns here
enemya = game.add.sprite(650, 515, 'enemya')
    enemya.animations.add('enemyAnimation',[0,1,2,3], 1.5, true);
    enemya.animations.play('enemyAnimation');
    game.physics.arcade.enable(enemya);
    enemya.body.collideWorldBounds = true;
    enemya.body.gravity.y = 2000;

enemya = game.add.sprite(148, 68, 'enemya');
    game.physics.arcade.enable(enemya);
    enemya.body.collideWorldBounds = true;
    enemya.body.gravity.y = 2000;
    game.physics.arcade.collide(enemya, platforms);        

cursors = game.input.keyboard.createCursorKeys();
jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

game.camera.follow(player);

}

enemya = game.add.sprite(650, 515, 'enemya')
    enemya.animations.add('enemyAnimation',[0,1,2,3], 1.5, true);
    enemya.animations.play('enemyAnimation');
    game.physics.arcade.enable(enemya);
    enemya.body.collideWorldBounds = true;
    enemya.body.gravity.y = 2000;

enemya = game.add.sprite(148, 68, 'enemya');
    game.physics.arcade.enable(enemya);
    enemya.body.collideWorldBounds = true;
    enemya.body.gravity.y = 2000;
    game.physics.arcade.collide(enemya, platforms);       

These cannot be saved into the same variable. You only want to set one sprite per variable. After you create the first sprite and save it to enemya, it has a reference to that sprite. Through that reference you can update its velocity or interact with it in many other ways. Once you set another sprite to that same variable, you lose the first reference. The sprite still exists, but you now have no way of interacting with it. This should fix it:

enemy1 = game.add.sprite(650, 515, 'enemya')
    enemy1.animations.add('enemyAnimation',[0,1,2,3], 1.5, true);
    enemy1.animations.play('enemyAnimation');
    game.physics.arcade.enable(enemy1);
    enemy1.body.collideWorldBounds = true;
    enemy1.body.gravity.y = 2000;

enemy2 = game.add.sprite(148, 68, 'enemya');
    game.physics.arcade.enable(enemy2);
    enemy2.body.collideWorldBounds = true;
    enemy2.body.gravity.y = 2000;
    game.physics.arcade.collide(enemy2, platforms);

My end goal for the game is to have enemies randomly spawn, so I’m not sure if that would work for me in the long run.

There is no good reason to set two existing sprites to the same variable. It isn’t saving you memory because the first sprite still exists and you will just lose control of any sprite created before the most current one. Once you are done with a sprite, you can call destroy() on it and then use the variable again to set a new sprite to it.

However, you can do something like this with “groups”. Basically, you create a group and you can tell the group how many sprites you want of what and where they should go, and you can act on the group as though it was one (like you can setScale once for the whole group). That may be what you are really looking for. Maybe check out the Examples section about Groups and see if that may work for you.

Another option is to simply create an array and store your sprites in the array. This will prevent you from having to create a variable for every single enemy you will ever want to use. You can create an array for each enemy type, and then just call push() when you create a new sprite to add it to the array.

1 Like

What if I were to assign all enemya in a group, and then tell the game to move the entire group as a whole?

I want to say there is a command for that, but the nice thing is with groups is that even if there is not a convenience method to perform an action on the whole group, you can easily loop through the whole group and perform the action like this:

Phaser.Actions.Call(yourGroup.getChildren(), function(individual) {
    individual.velocity.x = 10;
});

So essentially anything you can do to a single sprite, you can do to all sprites in a group. I am trying to see if there is convenience method to move them all at once but I don’t think there is as that may be kind of an unusual usecase.

You may want to check out the Group documentation page to get a feel for all of the methods and properties you have on Groups. You could also check out the official Phaser tutorial, as Groups are covered in part 8 (it looks like the Phaser tutorial is using a different way of looping through the Group to perform actions on each element. Both should work fine)

1 Like