Quantcast
Channel: Cocos Forums - Latest topics
Viewing all articles
Browse latest Browse all 17070

Pause with additional key presses

$
0
0

@Bushstar wrote:

I have the following to handle multiple keyboard presses. When holding down a key to make a sprite go left it continues to go left, when you then press the down key to go up the sprite pauses for half a second then moves diagonally as expected, if you then let go of the key to go left there is another half second pause before the sprite moves up.

The half second pauses are a killer. The code below is something I quickly cooked up and I have no idea if this is best practice. I am pretty new to Cocos2d-JS. Let me know if the best way to do this or what I'm doing wrong.

var keyboardListener = cc.EventListener.create({
    event: cc.EventListener.KEYBOARD,
    onKeyPressed: function(keyCode, event){
        keyPressed[keyCode] = true;
        cc.log("Key Pressed: " + keyCode);
        // Up
        if (keyCode == 87 || keyPressed[87]) {
            shapeArray[0].setPosition(shapeArray[0].getPositionX(), shapeArray[0].getPositionY() + 1);
        }
        // Down
        if (keyCode == 83 || keyPressed[83]) {
            shapeArray[0].setPosition(shapeArray[0].getPositionX(), shapeArray[0].getPositionY() - 1);
        }
        // Left
        if (keyCode == 65 || keyPressed[65]) {
            shapeArray[0].setPosition(shapeArray[0].getPositionX() - 1, shapeArray[0].getPositionY());
        }
        // Right
        if (keyCode == 68 || keyPressed[68]) {
            shapeArray[0].setPosition(shapeArray[0].getPositionX() + 1, shapeArray[0].getPositionY());
        }
    },
    onKeyReleased: function(keyCode, event){
        keyPressed[keyCode] = false;
    }
});

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 17070

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>