用HelloCpp工程修改代码:
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
CREATE_FUNC(HelloWorld);
private:
void createWindow();
void destoryWindow(CCObject* pSender);
void popWindow(CCObject* pSender);
};
#endif
#include "HelloWorldScene.h"
#include "AppMacros.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
CCScene *scene = CCScene::create();
HelloWorld *layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}
bool HelloWorld::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
CCMenuItemImage *pPopWindowItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::popWindow));
pPopWindowItem->setPosition(ccp(origin.x + visibleSize.width - pPopWindowItem->getContentSize().width/2 ,
origin.y + pPopWindowItem->getContentSize().height/2));
CCMenu* pPopWindowMenu = CCMenu::create(pPopWindowItem, NULL);
pPopWindowMenu->setPosition(CCPointZero);
this->addChild(pPopWindowMenu, 1);
CCMenuItemImage *pDestoryWindowItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::destoryWindow));
pDestoryWindowItem->setPosition(ccp(50.0f, 50.0f));
CCMenu* pDestoryWindowMenu = CCMenu::create(pDestoryWindowItem, NULL);
pDestoryWindowMenu->setPosition(CCPointZero);
this->addChild(pDestoryWindowMenu, 1);
return true;
}
void HelloWorld::popWindow(CCObject* pSender)
{
createWindow();
}
void HelloWorld::createWindow()
{
CCSprite* pWindow = CCSprite::create("HelloWorld.png");
pWindow->setScale(0.2f);
pWindow->setPosition(ccp(240.0f, 140.0f));
pWindow->setTag(10);
this->addChild(pWindow);
CCScaleTo *pScaleTo = CCScaleTo::create(1.0f, 0.7f, 0.7f);
CCActionInterval *pAction = CCEaseElasticOut ::create(pScaleTo);
pWindow->runAction(pAction);
}
void HelloWorld::destoryWindow( CCObject* pSender )
{
this->getChildByTag(10)->removeFromParent();
}
原文:http://blog.csdn.net/zlqqhs/article/details/19766211