Cocos2d-3.x_基本数据类型(cocos2d::Value)

前端之家收集整理的这篇文章主要介绍了Cocos2d-3.x_基本数据类型(cocos2d::Value)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. USING_NS_CC;
  7.  
  8.  
  9. class HelloWorld : public cocos2d::Layer
  10. {
  11. public:
  12. static cocos2d::Scene* createScene();
  13.  
  14. virtual bool init();
  15.  
  16. CREATE_FUNC(HelloWorld);
  17.  
  18. private:
  19. LabelTTF *pLabel;
  20. };
  21.  
  22. #endif // __HELLOWORLD_SCENE_H__
  1. #include "HelloWorldScene.h"
  2.  
  3. Scene* HelloWorld::createScene()
  4. {
  5. auto scene = Scene::create();
  6. auto layer = HelloWorld::create();
  7. scene->addChild(layer);
  8.  
  9. return scene;
  10. }
  11.  
  12. bool HelloWorld::init()
  13. {
  14. if ( !Layer::init() )
  15. {
  16. return false;
  17. }
  18. Size visibleSize = Director::getInstance()->getVisibleSize();
  19. Vec2 origin = Director::getInstance()->getVisibleOrigin();
  20.  
  21. pLabel = LabelTTF::create("HelloWorld","fonts/Arial.ttf",30);
  22. pLabel->setPosition(visibleSize.width / 2.0,visibleSize.height / 2.0);
  23. this->addChild(pLabel);
  24.  
  25. // 在Cocos2d-3.x中,把所有的int、float、double等等都封装成了Value类型
  26. cocos2d::Value intVar;
  27. intVar = 10;
  28. log("intVar:%d",intVar.asInt());
  29.  
  30. cocos2d::Value floatVar;
  31. floatVar = 10.3f;
  32. log("floatVar:%.1f",floatVar.asFloat());
  33.  
  34. cocos2d::Value strVar;
  35. strVar = "Hello";
  36. log("strVar:%s",strVar.asString().c_str());
  37.  
  38. return true;
  39. }

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