编辑: 向日葵8AS | 2016-08-20 |
//将菜单定位到窗口右下角 (0,0点在哪?,猜下) ??? //?创建菜单? ??? CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
???? pMenu->
setPosition(CCPointZero);
???? this->
addChild(pMenu, 1);
下面3行添加HelloWorld文字 在窗口找到了没???? CCLabelTTF* pLabel = CCLabelTTF::create( Hello World , Arial , 24);
???? pLabel->
setPosition(ccp(origin.x + visibleSize.width/2,origin.y + visibleSize.height - pLabel->
getContentSize().height));
????? this->
addChild(pLabel, 1);
下面3行添加整个图片???? CCSprite* pSprite = CCSprite::create( HelloWorld.png );
??? pSprite->
setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
??? this->
addChild(pSprite, 0)有了这句该图层会接收事件回调ccTouchesBegan,如果想捕获抬起事件,如何做????? this->
setTouchEnabled(true)return true;
?} 按下Menu回调 void HelloWorld::menuCloseCallback(CCObject* pSender)?{???? CCDirector::sharedDirector()->
end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)???? exit(0);
?#endif?} 在Layer按下将回调 void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)?{???? CCTouch* touch = (CCTouch*)(* pTouches->
begin(CCPoint pos = touch->
getLocation(CCLog( touch, x = %f, y = %f , pos.x, pos.y);
?} 最后这个 Layer如何显示到屏幕的呢? ?? //?创建一个场景???? CCScene *pScene = HelloWorld::scene();
??? //?显示这个场景到窗口,必然所有的绘制在场景中定义的???? pDirector->
runWithScene(pScene);
CCScene* HelloWorld::scene(scene'
is an autorelease object???? CCScene *scene = CCScene::create();
? //貌似和1.x版本有所不同,注意了.layer'
is an autorelease object???? HelloWorld *layer = HelloWorld::create();
??? // add layer as a child to scene???? scene->
addChild(layer);
??? // return the scene???? return scene;
?} 在scene方法中创建了一个CCScene对象并把HelloWorldLayer 添加到场景了. 关于内存 c++内存机制,采用new关键字实例化的对象,必须在不使用的时候手动delete掉,否则new的时候开辟的内存就不能被回收,造成内存泄露 Npc * n1=new Npc();
if(exit=0) { delete n1;
} cocos2d-x 内存管理的方式,cocos2d-x采用引用计数的方式进行内存管理,当一个对象的引用计数为0的时候,就会被引擎自动delete掉在cocos2d-x中绝大多数类继承CCObject ?CCObject::CCObject(void)?{???? static unsigned int uObjectCount = 0;
??? m_uID = ++uObjectCount;
? //??对象id ??? m_nLuaID = 0;
?? m_uReference = 1;
引用计数???? m_bManaged = false;
是否自动autorelease?} void CCObject::release(void)? //减少引用?{???? CCAssert(m_uReference >
0, reference count should greater than
0 m_uReference;
??? if (m_uReference == 0)delete this;
void CCObject::retain(void) //增加引用?{???? CCAssert(m_uReference >
0, reference count should greater than
0 );
??? ++m_uReference;
?} 自动处理 CCObject* CCObject::autorelease(void)?{???? CCPoolManager::sharedPoolManager()->
addObject(this);
??? m_bManaged = true;
???? return this;
?} CCObject::~CCObject(void)?{???? if (m_bManaged)CCPoolManager::sharedPoolManager()->
removeObject(this);
???? } } 自动释放池的释放是在哪里呢?大家可以研究下源码 在调试过程中可以跟踪对象的 ??? m_uReference = 1;
引用计数???? m_bManaged = false;
是否自动autorelease 这2个值来检查内存使用. 法则: 只要retain的放对应相同的release