1 decade ago by gothikon
Hi
After purchasing Impact the first thing I did was modify a few of the example games and add support for local multiplayer. Initially I duplicated the entire EntityPlayer file for the jumpnrun example, rename EntityPlayer to EntityPlayer2 and change the keyboard bindings, everything worked fine but the amount of code duplication quickly became a maintenance problem so I decided to give inheritance a shot.
This is roughly how the code looks now:
player.js
player-2.js
I fired up the game and everything looked okay until I tried to control the players, all the inputs for EntityPlayer would move EntityPlayer AND EntityPlayer2. When I tried to move EntityPlayer2 only EntityPlayer2 would respond but x-axis movement didn't work properly, movement in the y-axis was fine.
It's almost as if EntityPlayer2 is running the code in both update methods, my assumption was that any methods with the same name would overwrite the inherited methods.
I'd really appreciate any help!
After purchasing Impact the first thing I did was modify a few of the example games and add support for local multiplayer. Initially I duplicated the entire EntityPlayer file for the jumpnrun example, rename EntityPlayer to EntityPlayer2 and change the keyboard bindings, everything worked fine but the amount of code duplication quickly became a maintenance problem so I decided to give inheritance a shot.
This is roughly how the code looks now:
player.js
ig.module( 'game.entities.player' ).requires( 'impact.entity' ).defines(function () { EntityPlayer = ig.Entity.extend({ // generic properties for physics, collision, maxVel etc. init: function (x, y, settings) { // ... } update: function () { // input handling code if (ig.input.state('left')) { // ... } if (ig.input.state('right')) { // ... } // etc } }); });
player-2.js
ig.module( 'game.entities.player-2' ).requires( 'impact.entity', 'game.entities.player' ).defines(function () { EntityPlayer = EntityPlayer.extend({ update: function () { // input handling code if (ig.input.state('left2')) { // ... } if (ig.input.state('right2')) { // ... } // etc } }); });
I fired up the game and everything looked okay until I tried to control the players, all the inputs for EntityPlayer would move EntityPlayer AND EntityPlayer2. When I tried to move EntityPlayer2 only EntityPlayer2 would respond but x-axis movement didn't work properly, movement in the y-axis was fine.
It's almost as if EntityPlayer2 is running the code in both update methods, my assumption was that any methods with the same name would overwrite the inherited methods.
I'd really appreciate any help!