Thursday, January 9, 2025
spot_img

Gamepad Instance – Cocos Creator


Hello All,

Struggled to search out any examples of the gamepad options or a lot documentation. So now that I’ve figured it out I assumed I’d share some code that will help you work with gamepads in your video games.

So I made a folder in my property referred to as Scripts → World and one other folder inside Scripts referred to as Scenes. I’ve then created this primary defaultScript into the Scenes folder and a gamepad script into World.

Place the scene script into your scene after which run the mission and examine the console for gamepad button presses. I’ve additionally mapped this as an xbox controller structure (XYBA).

defaultScript

import { _decorator, Part, EventGamepad, enter, Enter } from 'cc';
import { gamepad } from '../World/gamepad';
const { ccclass, property } = _decorator;

@ccclass('defaultScript')
export class defaultScript extends Part {


    begin() {
        enter.on(Enter.EventType.GAMEPAD_INPUT, this.onButtonDown, this);
    }

    onButtonDown(occasion: EventGamepad){
        const button = gamepad.examine(occasion.gamepad);
        var buttonPressed = false;
        if(button.button_a == 1){
            buttonPressed = "a";
        }
        if(button.button_x == 1){
            buttonPressed = "x";
        }
        if(button.button_y == 1){
            buttonPressed = "y";
        }
        if(button.button_b == 1){
            buttonPressed = "b";
        }
        if(button.button_start == 1){
            buttonPressed = "begin";
        }
        if(button.button_select == 1){
            buttonPressed = "choose";
        }
        if(button.button_up == 1){
            buttonPressed = "dpad up";
        }
        if(button.button_down == 1){
            buttonPressed = "dpad down";
        }
        if(button.button_right == 1){
            buttonPressed = "dpad proper";
        }
        if(button.button_left == 1){
            buttonPressed = "dpad left";
        }
        if(button.l1 == 1){
            buttonPressed = "l1";
        }
        if(button.l2 == 1){
            buttonPressed = "l2";
        }
        if(button.r1 == 1){
            buttonPressed = "r1";
        }
        if(button.r2 == 1){
            buttonPressed = "r2";
        }
        if(button.button_left_stick == 1){
            buttonPressed = "Left stick";
        }
        if(button.button_right_stick == 1){
            buttonPressed = "Proper stick";
        }
        if(buttonPressed != false){
            console.log("Button "+buttonPressed+" pressed");
        }
    }
}

gamepad

import { _decorator, Part } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('gamepad')
export class gamepad extends Part {

    public static examine(gamepad){
        var button  = [0];
        button.button_a = gamepad.buttonSouth.getValue();
        button.button_x = gamepad.buttonWest.getValue();
        button.button_y = gamepad.buttonNorth.getValue();
        button.button_b = gamepad.buttonEast.getValue();
        button.button_select = gamepad.buttonShare.getValue();
        button.button_start = gamepad.buttonOptions.getValue();
        button.button_up = gamepad.dpad.up.getValue();
        button.button_down = gamepad.dpad.down.getValue();
        button.button_left = gamepad.dpad.left.getValue();
        button.button_right = gamepad.dpad.proper.getValue();
        button.l1 = gamepad.buttonL1.getValue();
        button.l2 = gamepad.buttonL2.getValue();
        button.r1 = gamepad.buttonR1.getValue();
        button.r2 = gamepad.buttonR2.getValue();
        button.button_left_stick = gamepad.buttonL3.getValue();
        button.button_right_stick = gamepad.buttonR3.getValue();
        button.left_stick = gamepad.leftStick.getValue();
        button.left_stick_up = 0;
        button.left_stick_down = 0;
        button.left_stick_left = 0;
        button.left_stick_right = 0;
        if(gamepad.leftStick.getValue().y > 0){
            button.left_stick_up = gamepad.leftStick.getValue().y;
        } else if(gamepad.leftStick.getValue().y  0){
            button.left_stick_right = gamepad.leftStick.getValue().x;
        } else if(gamepad.leftStick.getValue().x  0){
            button.right_stick_up = gamepad.rightStick.getValue().y;
        } else if(gamepad.rightStick.getValue().y  0){
            button.right_stick_right = gamepad.rightStick.getValue().x;
        } else if(gamepad.rightStick.getValue().x 

Thanks,
-iDev



3 Likes

Thanks for sharing ! I pinned this good code pattern



1 Like

Hello, sorry to resurrect this thread,

I’m utilizing the most recent cocos (3.8.5) and tried on each newest chrome and firefox.

I’ve been attempting to learn the gamepad enter and though I can use EventType.GAMEPAD_CHANGE, I by no means get EventType.GAMEPAD_INPUT.

e.g.

  begin() {
        enter.on(Enter.EventType.GAMEPAD_CHANGE, this.onGamepadChange, this);
        enter.on(Enter.EventType.GAMEPAD_INPUT, this.onGamepadInput,  this);
    }

Any assist gratefully obtained, thanks

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -spot_img

Latest Articles