Cocos2d-x Array Samples 数组初始化、字符转浮点值,数组纬度转换、字符串分割

前端之家收集整理的这篇文章主要介绍了Cocos2d-x Array Samples 数组初始化、字符转浮点值,数组纬度转换、字符串分割前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    // a selector callback
    void menuCloseCallback(cocos2d::Ref* pSender);

    // implement the "static create()" method manually
    CREATE_FUNC(HelloWorld);
    //masai
    float arr_heroId[10];
    HelloWorld()
    {
        for (int i = 0; i < 10; i++)
        {
            arr_heroId[i] = i;
        }

    }
};

#endif // __HELLOWORLD_SCENE_H__

CPP文件

#include "HelloWorldScene.h"

USING_NS_CC;

Scene* HelloWorld::createScene()
{
    // 'scene' is an autorelease object
    auto scene = Scene::create();

    // 'layer' is an autorelease object
    auto layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    /////////////////////////////
    // 2. add a menu item with "X" image,which is clicked to quit the program
    // you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    auto closeItem = MenuItemImage::create(
                                           "CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));

    closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2));

    // create menu,it's an autorelease object
    auto menu = Menu::create(closeItem,NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu,1);

    /////////////////////////////
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label

    auto label = Label::createWithTTF("Hello World","fonts/Marker Felt.ttf",24);

    // position the label on the center of the screen
    label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));

    // add the label as a child to this layer
    this->addChild(label,1);

    // add "HelloWorld" splash screen"
    //auto sprite = Sprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
   // sprite->setPosition(Vec2(visibleSize.width/2 + origin.x,visibleSize.height/2 + origin.y));

    // add the sprite as a child to this layer
   // this->addChild(sprite,0);

    //float arr_heroId[10] = { 0,1,2,3,4,5,6,7,8,9 };
    float arr_heroActived[10] = { 0,0,0 };
    float arr_heroLvl_1_MaxExp[10] = { 300,300,300 };
    float arr_heroCurrLvl[10] = { 1,1,1 };
    float arr_heroCurrExp[10] = { 0,0 };
    float arr_heroCurrMaxHp[10] = { 150,180,100,150,150 };
    float arr_heroAttackForce[10] = { 14,10,20,20 };
    float arr_heroDefenceForce[10] = { 0,0 };
    float arr_heroRecoveryForce[10] = { 7,7,7 };
    float arr_heroExp_LvlupRate[10] = { 2,2,2 };
    float arr_heroHp_LvlupRate[10] = { 1.2,1.2,1.2 };
    float arr_heroAttackForce_LvlupRate[10] = { 1.15,1.15,1.15 };
    float arr_heroDefenceForce_LvlupRate[10] = { 1.15,1.15 };
    float arr_heroRecoveryForce_LvlupRate[10] = { 1.1,1.1,1.1 };
    //float arr_heroData[14][10] = { { 0,9 },{ 0,0 },{ 300,300,300 },{ 1,1 },{ 150,180,100,150,150 },{ 14,10,20,20 },{ 7,7 },{ 2,2 },{ 1.2,1.2,1.2 },{ 1.15,1.15,1.15 },{ 1.1,1.1,1.1 } };
    float arr_heroData[14][10] = {  { arr_heroId[0],arr_heroId[1],arr_heroId[2],arr_heroId[3],arr_heroId[4],arr_heroId[5],arr_heroId[6],arr_heroId[7],arr_heroId[8],arr_heroId[9] },{ arr_heroActived[0],arr_heroActived[1],arr_heroActived[2],arr_heroActived[3],arr_heroActived[4],arr_heroActived[5],arr_heroActived[6],arr_heroActived[7],arr_heroActived[8],arr_heroActived[9] },{ arr_heroLvl_1_MaxExp[0],arr_heroLvl_1_MaxExp[1],arr_heroLvl_1_MaxExp[2],arr_heroLvl_1_MaxExp[3],arr_heroLvl_1_MaxExp[4],arr_heroLvl_1_MaxExp[5],arr_heroLvl_1_MaxExp[6],arr_heroLvl_1_MaxExp[7],arr_heroLvl_1_MaxExp[8],arr_heroLvl_1_MaxExp[9] },{ arr_heroCurrLvl[0],arr_heroCurrLvl[1],arr_heroCurrLvl[2],arr_heroCurrLvl[3],arr_heroCurrLvl[4],arr_heroCurrLvl[5],arr_heroCurrLvl[6],arr_heroCurrLvl[7],arr_heroCurrLvl[8],arr_heroCurrLvl[9] },{ arr_heroCurrExp[0],arr_heroCurrExp[1],arr_heroCurrExp[2],arr_heroCurrExp[3],arr_heroCurrExp[4],arr_heroCurrExp[5],arr_heroCurrExp[6],arr_heroCurrExp[7],arr_heroCurrExp[8],arr_heroCurrExp[9] },{ arr_heroCurrMaxHp[0],arr_heroCurrMaxHp[1],arr_heroCurrMaxHp[2],arr_heroCurrMaxHp[3],arr_heroCurrMaxHp[4],arr_heroCurrMaxHp[5],arr_heroCurrMaxHp[6],arr_heroCurrMaxHp[7],arr_heroCurrMaxHp[8],arr_heroCurrMaxHp[9] },{ arr_heroAttackForce[0],arr_heroAttackForce[1],arr_heroAttackForce[2],arr_heroAttackForce[3],arr_heroAttackForce[4],arr_heroAttackForce[5],arr_heroAttackForce[6],arr_heroAttackForce[7],arr_heroAttackForce[8],arr_heroAttackForce[9] },{ arr_heroDefenceForce[0],arr_heroDefenceForce[1],arr_heroDefenceForce[2],arr_heroDefenceForce[3],arr_heroDefenceForce[4],arr_heroDefenceForce[5],arr_heroDefenceForce[6],arr_heroDefenceForce[7],arr_heroDefenceForce[8],arr_heroDefenceForce[9] },{ arr_heroRecoveryForce[0],arr_heroRecoveryForce[1],arr_heroRecoveryForce[2],arr_heroRecoveryForce[3],arr_heroRecoveryForce[4],arr_heroRecoveryForce[5],arr_heroRecoveryForce[6],arr_heroRecoveryForce[7],arr_heroRecoveryForce[8],arr_heroRecoveryForce[9] },{ arr_heroExp_LvlupRate[0],arr_heroExp_LvlupRate[1],arr_heroExp_LvlupRate[2],arr_heroExp_LvlupRate[3],arr_heroExp_LvlupRate[4],arr_heroExp_LvlupRate[5],arr_heroExp_LvlupRate[6],arr_heroExp_LvlupRate[7],arr_heroExp_LvlupRate[8],arr_heroExp_LvlupRate[9] },{ arr_heroHp_LvlupRate[0],arr_heroHp_LvlupRate[1],arr_heroHp_LvlupRate[2],arr_heroHp_LvlupRate[3],arr_heroHp_LvlupRate[4],arr_heroHp_LvlupRate[5],arr_heroHp_LvlupRate[7],arr_heroHp_LvlupRate[8],arr_heroHp_LvlupRate[9] },{ arr_heroAttackForce_LvlupRate[0],arr_heroAttackForce_LvlupRate[1],arr_heroAttackForce_LvlupRate[2],arr_heroAttackForce_LvlupRate[3],arr_heroAttackForce_LvlupRate[4],arr_heroAttackForce_LvlupRate[5],arr_heroAttackForce_LvlupRate[6],arr_heroAttackForce_LvlupRate[7],arr_heroAttackForce_LvlupRate[8],arr_heroAttackForce_LvlupRate[9] },{ arr_heroDefenceForce_LvlupRate[0],arr_heroDefenceForce_LvlupRate[1],arr_heroDefenceForce_LvlupRate[2],arr_heroDefenceForce_LvlupRate[3],arr_heroDefenceForce_LvlupRate[4],arr_heroDefenceForce_LvlupRate[5],arr_heroDefenceForce_LvlupRate[6],arr_heroDefenceForce_LvlupRate[7],arr_heroDefenceForce_LvlupRate[8],arr_heroDefenceForce_LvlupRate[9] },{ arr_heroRecoveryForce_LvlupRate[0],arr_heroRecoveryForce_LvlupRate[1],arr_heroRecoveryForce_LvlupRate[2],arr_heroRecoveryForce_LvlupRate[3],arr_heroRecoveryForce_LvlupRate[4],arr_heroRecoveryForce_LvlupRate[5],arr_heroRecoveryForce_LvlupRate[6],arr_heroRecoveryForce_LvlupRate[7],arr_heroRecoveryForce_LvlupRate[8],arr_heroRecoveryForce_LvlupRate[9] }
                                    };
    //masai
    //float arr_test[2][10] = { { arr_heroId[10] },{ arr_heroActived[10] } };

// for (int i = 0; i < 10; i++)
// CCLOG("Array Output Data: %f",arr_heroCurrMaxHp[i]);

    char str1[2000];
    char str2[2000];
    str2[0] = '\0';

    for (int i = 0; i < 14; i++)
    {
        for (int j = 0; j < 10; j++)
        {
            //CCLOG("Array Output Data: %f",arr_heroData[i][j]);
            sprintf(str1,"%f",arr_heroData[i][j]);
            strcat(str2,str1);
            strcat(str2,",");
        }
    }
    //CCLOG("String Output Data::%s",str2);

    CCLOG("Ready to Split string\n");

    char* pch;
    pch = strtok(str2,");
    CCLOG("FirstTime pch:%s",pch);
    int i = 0;
    //int j = 0;
    //int k = 0;
    float Manager_arr_heroData[14][10] = {};
    while (pch != NULL)
    {
        CCLOG("Serial num %d: %s\n",i,pch);
        CCLOG("new xiabiao i=%d j=%d \n",(i / 10),(i % 10));
        Manager_arr_heroData[(i / 10)][(i % 10)] = String::createWithFormat("%s",pch)->floatValue();
        pch = strtok(NULL,");
        i++;
    }

    for (int j = 0; j < 14; j++)
    {
        for (int k = 0; k < 10; k++)
        {
            CCLOG("Manager heroData Array output Test:%f",Manager_arr_heroData[j][k]);
        }
        CCLOG("\n");
    }

    //for (int l = 0; l < 10; l++)
    // CCLOG("Array OutPut Test:%f",arr_heroId[l]);

    return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
{
    Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}
原文链接:https://www.f2er.com/cocos2dx/338477.html

猜你在找的Cocos2d-x相关文章