player objects don't have individual key input.

I want to make a game where players can play with just one button. This is just a test sketch. There is a function for joining in with a keypress. If press enter you will enter the next room(game). when in this room a player press his key he will move forward. but there is a problem when a other player presses his key the others players isn't working. I think it has something to do with the structure. I can't figure it out to realize individual key input. Any solutions?

    //global
    var room = 0
    var players = []



    function setup() {
        if (room === 0); {
            createCanvas(800, 600)
            setup_room0()
        }
        if (room === 1) {
            setup_room1()
        }
    }


    function draw() {
        if (room === 0) {
            post_draw_room0();
            draw_room0();
        }
        if (room === 1) {
            post_draw_room1();
            draw_room1();
        }
    }


function keyPressed() {
    if (keyCode == ENTER) {
        if (room === 0) {
            room = 1
            setup()
        }
    }
}

function keyTyped() {


    if (room === 0) {
        players.push(key)
        playersCheck.push(false)
    }

}




// room 0
var text1 = "buttons"
var playersCheck = []

//player select room
function setup_room0() {

}

function post_draw_room0() {
    background(255, 0, 255);


    // joining and leaving of player
    for (var i = 0; i < players.length; i++) {
        for (var j = 0; j < players.length; j++) {
            if (players[i] === players[j] && i != j) {
                players.splice(j, 1)
                playersCheck.splice(j, 1)

                if (key === players[i] && playersCheck[i] === true) {
                    players.splice(i, 1)
                    playersCheck.splice(i, 1)
                }
            }
            if (key === players[i] && playersCheck[i] === false) {
                playersCheck[i] = !playersCheck[i]
            }
        }
    }
}

function draw_room0() {
    textSize(20);
    textAlign(CENTER);
    text("presss one buton to join and press Enter to play", width / 2, height / 2)
    text(players, width / 2, height / 2 + 20)
}

///////////////////////////////////////////////
//room1
var players_room1 = [];

function setup_room1() {
    for (var i = 0; i < players.length; i++){
    players_room1[i] = {

        playerX: 100,
        playerY: 300,
        playerH: 100,
        playerW: 100,
        playerID: i,

        display: function() {
            rect(this.playerX, this.playerY, this.playerH, this.playerW)
        },

        move: function() {
            if (key === players[this.playerID]) {
                this.playerX += 1
            }
        }
    }
 }
}


function post_draw_room1() {
    background(255, 255, 0)
        for (var i = 0; i < players.length; i++){
    players_room1[i].move();
}
}

function draw_room1() {
        for (var i = 0; i < players.length; i++){
    players_room1[i].display();
        }
}
}
Tagged:

Answers

  • I did some testing the real problem is that p5 can't detect multi able key presses. any solution for this?

  • Answer ✓

    I think the most likely explanation for you issue is the OS key repeat blocking the other key (see notes on keyRepeat). You might get better results using keyReleased(); but I suspect you'll need some more complex debouncing to make this work the way you want.

  • i already figured it out. I used the keyPressed and keyReleased functions to create a boolean.. and that works. Thanks for answering

Sign In or Register to comment.