Jump to content

Should you have multiple moveFunctions to move multiple players?


PixelProgrammer
 Share

Recommended Posts

Edit: Formatting seems to be a bit screwed up..just ignore the missing brackets and '.'

 

So currently I have a game in which two players move around shooting enemies. This is the current code I have implemented.

var preloadfunction loadimage'player1', 'assets/player1.png';
        loadimage'player2', 'assets/player2.png';    ,
    createfunction stagebackgroundColor '#000000';
        physicsstartSystemPhaserARCADE;
        rendererrenderSessionroundPixels true; //setting render system to pixel-based
        thisplayer1 addspritewidth2 40, height60, 'player1'; //adding player1
        
        thisplayer1cursors inputkeyboardcreateCursorKeys; //adding movement keys to player1
        thisplayer1fireButton inputkeyboardaddKeyPhaserKeyboardCONTROL;
        thisplayer2 addspritewidth2 40, height60, 'player2'; //adding player2
        
        thisplayer2cursors upinputkeyboardaddKeyPhaserKeyboardW,
                            downinputkeyboardaddKeyPhaserKeyboardS,
                            leftinputkeyboardaddKeyPhaserKeyboardA,
                            rightinputkeyboardaddKeyPhaserKeyboardD,;
        thisplayer2fireButton inputkeyboardaddKeyPhaserKeyboardSPACEBAR;
        thiscreateBullets; //creates weapon for p1 and p2
    ,
    updatefunction //all collision and overlap checks go first here!
        physicsarcadecollidethisplayer1, thisfloor;
        physicsarcadecollidethisplayer2, thisfloor
        physicsarcadeoverlapthisweapon_p1bullets, thisenemies, thishitEnemy, null, this;
        physicsarcadeoverlapthisweapon_p2bullets, thisenemies, thishitEnemy, null, this;
        physicsarcadeoverlapthisplayer1, thisenemies, thiskillPlayer, null, this;
        physicsarcadeoverlapthisplayer2, thisenemies, thiskillPlayer, null, this;

        thismovePlayer1; //movement for player 1
        thismovePlayer2; //movement for player 2
    ,
    movePlayer1function if thisplayer1alive//dont run if player 1 is dead
            return;
        //movements
        if thisplayer1cursorsleftisDownthisplayer1bodyvelocityx 300;
        else if thisplayer1cursorsrightisDownthisplayer1bodyvelocityx 300;
        else thisplayer1bodyvelocityx 0;
        
    
    movePlayer2: function () {

    if (!this.player2.alive) //dont run if player 1 is dead
        return;

    //movements
    if (this.player2.cursors.left.isDown){
        this.player2.body.velocity.x = -300;
    }
    else if (this.player2.cursors.right.isDown){
        this.player2.body.velocity.x = 300;
    }
    else {
        this.player2.body.velocity.x = 0;
    }
},

 

This code works great with no flaws, but I was thinking of creating just one function called movePlayer like this.

 

movePlayer: function (player) {

    if (!player.alive) //dont run if player 1 is dead
        return;

    //movements
    if (player.cursors.left.isDown){
        player.body.velocity.x = -300;
    }
    else if (player.cursors.right.isDown){
        player.body.velocity.x = 300;
    }
    else {
        player.body.velocity.x = 0;
    }

And call it in the update function as 

this.movePlayer(this.player1);
this.movePlayer(this.player2);

However, this seems to do nothing. I believe it has something to do with player being passed as a local variable?

Why doesn't this code work and also.. would you rather have one movement function handling both players or have one for each??

Edited by PixelProgrammer
Explaining formatting
Link to comment
Share on other sites

1 hour ago, drhayes said:

I don't see anything wrong with the code at this glance. If you put "console.log(player);" at the start of the method movePlayer, what gets printed to the developer console? Are there any errors there?

@drhayes - Did that and I got this.. Clicking any of the lines gives me the second image.

 

Nevermind.. I was just stupid. The function kept exiting because instead of 

if (!player.alive)

I accidently typed 

if (player.alive)

Programming..huh. xD

Link to comment
Share on other sites

Just now, b10b said:

Have you considered movePlayer() becoming player.move()?  General advice is that fewer-functions are better than more-functions (easier to maintain, less to transport, etc), but beware of creating functions that do more than one thing well.

I had thought about that and it's usually been a habit of mine to have a .move() or .draw() for every object.

However, the reason I decided to make a separate movePlayer function on it's own was so that changes in just that one function would reflect in all players.

What is the standard way of doing things? Creating player1.move and player2.move or just one movePlayer() function?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...