Jump to content

[HELP]Multiplayer WASD & Arrow Keys Problem


KnightBreaker
 Share

Recommended Posts

Could someone help me? I am trying to figure out why about my problem with 2 players controls here is the whole game could someone please help me fix the Level1

Mobile App.rar

 

This is the part where i am so confused and idk which one to fix.

Quote

Game.Level1 = function(game){};

var map;
var layer;
var player;
var player2;
var controls = {};
var playerSpeed = 150;
var jumpTimer = 0;

var shootTime = 0;
var androids;

var enemy;
var enemy2;
var score = 0;

Game.Level1.prototype = {
    create: function(game){
        this.stage.backgroundColor = '#3A5963';

        this.physics.arcade.gravity.y = 1400;

        map = this.add.tilemap('map', 64, 64);

        map.addTilesetImage('tileset');
        
        layer = map.createLayer(0);
        
        layer.resizeWorld();

        map.setCollisionBetween(0,2);

        map.setTileIndexCallback(5, this.resetPlayer, this);

        map.setTileIndexCallback(7, this.getCoinOne, this);
        map.setTileIndexCallback(8, this.getCoinTwo, this);
        map.setTileIndexCallback(9, this.getCoinThree, this);

        player = this.add.sprite(80, 560, 'player');
        player.anchor.setTo(0.5);
        player.animations.add('idle',[0,1], 1, true);
        player.animations.add('jump', [2], 1, true);
        player.animations.add('run', [3,4,5,6,7,8], 7, true);
        this.physics.arcade.enable(player);
        this.camera.follow(player);
        player.body.collideWorldBounds = true;
            
        
        player2 = this.add.sprite(100, 560, 'player2');
        player2.anchor.setTo(0.5);
        player2.animations.add('idle',[0,1], 1, true);
        player2.animations.add('jump', [2], 1, true);
        player2.animations.add('run', [3,4,5,6,7,8], 7, true);
        this.physics.arcade.enable(player2);
        player2.body.collideWorldBounds = true;
        
        controls = {
            right: this.input.keyboard.addKey(Phaser.Keyboard.RIGHT),
            left: this.input.keyboard.addKey(Phaser.Keyboard.LEFT),
            up: this.input.keyboard.addKey(Phaser.Keyboard.UP),
            shoot: this.input.keyboard.addKey(Phaser.Keyboard.END)
            
        };

        enemy = new Enemy1(0, game, player.x+150, player.y-100);
        enemy2 = new Enemy2(0, game, player.x+500, player.y-100);

        androids = game.add.group();
        androids.enableBody = true;
        androids.physicsBodyType = Phaser.Physics.ARCADE;
        androids.createMultiple(5, 'ios');
        androids.setAll('anchor.x', 0.5);
        androids.setAll('anchor.y', 0.5);
        androids.setAll('scale.x', 0.05);
        androids.setAll('scale.y', 0.05);
        androids.setAll('outOfBoundsKill', true);
        androids.setAll('checkWorldBounds', true);
    },

    update: function(){
        this.physics.arcade.collide(player, layer);
        this.physics.arcade.collide(player, enemy.enemy1, enemy2.enemy2, this.resetPlayer);
        
        player.body.velocity.x = 0;

        if(controls.right.isDown){
            player.animations.play('run');
            player.scale.setTo(1, 1);
            player.body.velocity.x += playerSpeed;
        }

        if(controls.left.isDown){
            player.animations.play('run');
            player.scale.setTo(1, 1);
            player.body.velocity.x -= playerSpeed;
        }

        if(controls.up.isDown && (player.body.onFloor() || player.body.touching.down) && this.time.now>jumpTimer){
            player.body.velocity.y = -600;
            jumpTimer = this.time.now + 750;
            player.animations.play('jump');
        }

        if(controls.shoot.isDown){
            this.shootAndroid();
        }

        /*if(checkOverlap(player, enemy.enemy1)){
            this.resetPlayer();
        }*/

        if(checkOverlap(androids, enemy.enemy1, enemy2.enemy2)){
            enemy.enemy1.kill();
            score += 100;
        }

        if(player.body.velocity.x == 0 && player.body.velocity.y == 0){
            player.animations.play('idle');
        }
        
    },

    resetPlayer: function(){
        player.reset(100, 560);
    },
    
    removePlayer: function(){
        player.reset(100, 560);
    },

    getCoinOne: function(){
        map.putTile(-1, layer.getTileX(player.x), layer.getTileY(player.y));
        score += 10;
    },

    getCoinTwo: function(){
        map.putTile(-1, layer.getTileX(player.x), layer.getTileY(player.y));
        score += 20;
    },

    getCoinThree: function(){
        map.putTile(-1, layer.getTileX(player.x), layer.getTileY(player.y));
        score += 30;
    },

    shootAndroid: function(){
        if(this.time.now > shootTime){
            android = androids.getFirstExists(false);
            if(android){
                android.reset(player.x, player.y);
                android.body.velocity.y = -300;
                android.body.velocity.x = 300;
                shootTime = this.time.now + 900;
            }
        }
    }
}

function checkOverlap(spriteA, spriteB){
    var boundsA = spriteA.getBounds();
    var boundsB = spriteB.getBounds();
    return Phaser.Rectangle.intersects(boundsA, boundsB);
}

Enemy1 = function(index, game, x, y){
    this.enemy1 = game.add.sprite(x, y, 'enemy1');
    this.enemy1.anchor.setTo(0.5, 0.5);
    this.enemy1.scale.setTo(0.05, 0.05);
    this.enemy1.name = index.toString();
    game.physics.enable(this.enemy1, Phaser.Physics.ARCADE);
    this.enemy1.body.immovable = true;
    this.enemy1.body.collideWorldBounds = true;
    this.enemy1.body.allowGravity = false;
    this.enemy1Tween = game.add.tween(this.enemy1).to({
        y: this.enemy1.y + 25
    }, 2000, 'Linear', true, 0, 200, true);
}

Enemy2 = function(index, game, x, y){
    this.enemy2 = game.add.sprite(x, y, 'enemy2');
    this.enemy2.anchor.setTo(0.5, 0.5);
    this.enemy2.scale.setTo(0.05, 0.05);
    this.enemy2.name = index.toString();
    game.physics.enable(this.enemy2, Phaser.Physics.ARCADE);
    this.enemy2.body.immovable = true;
    this.enemy2.body.collideWorldBounds = true;
    this.enemy2.body.allowGravity = false;
    this.enemy2Tween = game.add.tween(this.enemy2).to({
        y: this.enemy2.y + 30
    }, 3000, 'Linear', true, 0, 300, true);
};

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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