cocos2d-x之Ref列表容器(实例:__Array容器)
cocos2d-x之Ref列表容器(实例:__Array容器)
HelloWorldScene.h
1 #ifndef __HELLOWORLD_SCENE_H__ 2 #define __HELLOWORLD_SCENE_H__ 3 4 #include "cocos2d.h" 5 6 #define MAX_COUNT 100 7 //定义宏,定义了一次生成的精灵数 8 9 10 class HelloWorld : public cocos2d::Layer 11 { 12 //声明—__Array * 的成员变量list 13 cocos2d::__Array *list; 14 15 public: 16 17 //声明析构函数,需要在析构函数中释放成员变量 18 ~HelloWorld(); 19 20 // there's no 'id' in cpp, so we recommend returning the class instance pointer 21 static cocos2d::Scene* createScene(); 22 23 // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone 24 virtual bool init(); 25 26 // a selector callback 27 void menuCloseCallback(cocos2d::Ref* pSender); 28 29 // implement the "static create()" method manually 30 CREATE_FUNC(HelloWorld); 31 }; 32 33 #endif // __HELLOWORLD_SCENE_H__
HelloWorldScene.cpp
1 #include "HelloWorldScene.h" 2 3 USING_NS_CC; 4 5 6 HelloWorld::~HelloWorld(){ 7 8 //移除容器中的所有对象,但还没有释放list容器对象本身 9 this->list->removeAllObjects(); 10 //安全释放成员变量list容器对象, 11 //CC_SAFE_RELEASE_NULL是将list容器对象释放后再赋予nullptr 12 CC_SAFE_RELEASE_NULL(this->list); 13 14 } 15 16 Scene* HelloWorld::createScene() 17 { 18 // 'scene' is an autorelease object 19 auto scene = Scene::create(); 20 21 // 'layer' is an autorelease object 22 auto layer = HelloWorld::create(); 23 24 // add layer as a child to scene 25 scene->addChild(layer); 26 27 // return the scene 28 return scene; 29 } 30 31 // on "init" you need to initialize your instance 32 bool HelloWorld::init() 33 { 34 ////////////////////////////// 35 // 1. super init first 36 if ( !Layer::init() ) 37 { 38 return false; 39 } 40 41 Size visibleSize = Director::getInstance()->getVisibleSize(); 42 Vec2 origin = Director::getInstance()->getVisibleOrigin(); 43 44 auto goItem = MenuItemImage::create("go-down.png", "go-up.png", CC_CALLBACK_1(HelloWorld::menuCloseCallback, this)); 45 goItem->setPosition(Vec2(origin.x + visibleSize.width - goItem->getContentSize().width / 2, origin.y + goItem->getContentSize().height / 2)); 46 47 auto menu = Menu::create(goItem, NULL); 48 menu->setPosition(Vec2::ZERO); 49 this->addChild(menu); 50 51 //创建__Array类型的list成员变量,使用createWithCapacity函数,参数是list容器的初始化容量 52 this->list = __Array::createWithCapacity(MAX_COUNT); 53 //采用静态函数createWithCapacity创建的list容器对象是autorelease的, 54 //如果不调用retain()函数保持内存, 55 //当init函数结束时, 56 //list容器对象会自动释放, 57 //这样在其他函数中在使用list就会出错 58 this->list->retain(); 59 60 for (int i = 0; i < MAX_COUNT; i++){ 61 //循环创建精灵对象 62 Sprite *sprite = Sprite::create("Ball.png"); 63 //将精灵添加到list容器对象中,但是这些精灵还没有被添加到场景中,因此,场景显示的时候他们是不出现的 64 this->list->addObject(sprite); 65 } 66 67 return true; 68 } 69 70 71 void HelloWorld::menuCloseCallback(Ref* pSender) 72 { 73 //#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) 74 // MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert"); 75 // return; 76 //#endif 77 // 78 // Director::getInstance()->end(); 79 // 80 //#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) 81 // exit(0); 82 //#endif 83 84 /************************** 85 测试 包装类"Value" 86 *************************** 87 Value v1; 88 CCASSERT(v1.isNull(), "");//判断v1是否为空 89 90 Value v2(100);//创建int类型的Value对象 91 //输出对象的描述信息,类似于java中的 toString() 92 log("<><> The description of the integer value:%s <><>", v2.getDescription().c_str()); 93 log("<><> v2.asByte() = %c <><>", v2.asByte());//将v2转换为byte类型 94 95 Value v3(101.4f); 96 log("<><> The description of the float value:%s <><>", v3.getDescription().c_str()); 97 98 Value v4(106.1); 99 log("<><> The description of the double value:%s <><>", v4.getDescription().c_str()); 100 log("<><> v4.asInt() = %d <><>", v4.asInt()); 101 102 unsigned char byte = 50; 103 Value v5(byte); 104 log("<><> The description of the byte value:%s <><>", v5.getDescription().c_str()); 105 106 Value v6(true); 107 log("<><> The description of the boolean value:%s <><>", v6.getDescription().c_str()); 108 109 Value v7("123"); 110 log("<><> The description of the string value:%s <><>", v7.getDescription().c_str()); 111 log("<><> v7.asInt() = %d <><><>", v7.asInt()); 112 113 ************************** 114 测试 包装类"Value" 115 ***************************/ 116 117 118 log("><>><><> list->count() = %d <>>><>>", this->list->count()); 119 Size visibleSize = Director::getInstance()->getVisibleSize(); 120 121 Ref *obj = nullptr; 122 123 //使用宏,循环遍历list容器中的数据 124 CCARRAY_FOREACH(this->list, obj){ 125 126 //获得精灵对象 127 Sprite *sprite = (Sprite *)obj; 128 129 //随机产生x轴坐标,CCRANDOM_0_1宏是随机产生0~1之间的随机数的宏 130 int x = CCRANDOM_0_1() *visibleSize.width; 131 int y = CCRANDOM_0_1() *visibleSize.height; 132 133 sprite->setPosition(Vec2(x, y)); 134 this->removeChild(sprite); 135 this->addChild(sprite); 136 137 } 138 139 }