Examples

Cocos Creator GDK Examples

You can check outthe example code in the package at negdk/assets/negdk/sample/NEGdkSample.ts

Initialize the GDK

export class ExampleInit extends Component {
    protected onLoad(): void {
        const config: NEGdkConfig = {
            gameKey: "eternal",
            platformSource: "ETERNAL",
            ramperAppId: "vgzjondflu",
            scheme: "negdkcc",
            subPlatform: "",
            environment: NEEnvironment.DEV,
            loginCacheEnabled: true
        }
        NEGdk.getInstance().initialize(config, (errorCode) => {
            const success = errorCode === NEErrorCode.SUCCESS;
            if (success) {
                conslose.log("GDK initialize successfully!");
            } else {
                conslose.error(`GDK initialize failed, errorCode = ${errorCode}`);
                // Check if already login in case you are using login cache
                if (negdk.isLoggedIn()) {
                    // already login 
                } else {
                    // not login yet
                }
            }
        });
    }
}

Login

Social Login

export class ExampleSocialLogin extends Component {

    private loginWithGoogle() {
        NEGdk.getInstance().loginWithGoogle(this.onLoginCompleted.bind(this));
    }

    private loginWithFacebook() {
        NEGdk.getInstance().loginWithFacebook(this.onLoginCompleted.bind(this));
    }

    private loginWithEmail() {
        NEGdk.getInstance().loginWithEmail(this.onLoginCompleted.bind(this));
    }

    private loginWithTelegram() {
        NEGdk.getInstance().loginWithTelegram(this.onLoginCompleted.bind(this));
    }

    private loginWithApple() {
        NEGdk.getInstance().loginWithApple(this.onLoginCompleted.bind(this));
    }

    private onLoginCompleted(errorCode: number, data: NELoginData) {
        const success = errorCode === NEErrorCode.SUCCESS;
        if (success) {
            console.log(`Login success, authen token: ${data.authToken}`});
        } else {
            console.log(`Login failed, errorCode: ${errorCode}`});
        }
    }
}

Create/Restore Wallet

export class ExampleWalletLogin extends Component {

    private createWalletAndLogin(): string {
        // Create a new wallet
        const phrase = NEGdk.getInstance().createWallet();

        // Login with new wallet
        NEGdk.getInstance().loginWithPhrase(phrase, this.onLoginCompleted.bind(this));
    }

    private restoreWallet() {
        // Allow user to enter seedphrase
        const phrase = this.editBoxPhrase.string;

        // Restore wallet and login
        NEGdk.getInstance().loginWithPhrase(phrase, this.onLoginCompleted.bind(this));
    }

    private onLoginCompleted(errorCode: number, data: NELoginData) {
        const success = errorCode === NEErrorCode.SUCCESS;
        if (success) {
            console.log(`Login success, authen token: ${data.authToken}`});
        } else {
            console.log(`Login failed, errorCode: ${errorCode}`});
        }
    }
}

Get User Pets Data

export class ExampleUserPets extends Component {
    private requestUserPets() {
        NEGdk.getInstance().requestUserPets((response) => {
            if (!response.errorCode) {
                var pets = response.data; // get user pets data from response
            } else {
                // error handling
            }
        });
    }
}

Get User Balances Data

export class ExampleUserBalances extends Component {
    private requestUserBalances() {
        NEGdk.getInstance().requestUserBalances((response,) => {
            if (!response.errorCode) {
                var balances = response.data; // get user balances data from response
            } else {
                // error handling
            }
        });
    }
}

Get Shop Data

export class ExampleShop extends Component {
    private requestShopInfo() {
        NEGdk.getInstance().requestShopInfo((response) => {
            if (!response.errorCode) {
                var shopPacks = response.data; // get shop packs data from response
            } else {
                // error handling
            }
        });
    }
}

Buy a Pack in Shop

export class ExampleShopBuy extends Component {
    private buyShopPack() {
        var packId = 26;
        var amount = 1;
        var currency = "SPACE_STONE";
        NEGdk.getInstance().buyShopPack(packId, amount, currency, (response) => {
            if (!response.errorCode) {
                var remainBalance = response.data; // user balance for the currency after purchase
            } else {
                // Failed to buy the pack!!!
            }
        });
    }
}

Last updated