#include "HelloWorldScene.h"
#include<ctime>
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
this->addChild(pMenu, 1);
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
CC_BREAK_IF(! pSprite);
pSprite->setPosition(ccp(size.width/2, size.height/2));
this->addChild(pSprite, 0);
//
//初始化水果
orange = CCSprite::create("ball.jpg");
orange->setPosition(ccp(150,300));
this->addChild(orange,0);
//移动水果
CCActionInterval* moveto = CCMoveTo::create(3,ccp(150,0));
orange->runAction(moveto);
//
lanzi = CCSprite::create("lanzi.jpg");
lanzi->setPosition(ccp(150,0));
this->addChild(lanzi,0);
//定时器 获取精灵动态坐标
this->schedule( schedule_selector(HelloWorld::update) );
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::update(float dt)
{
CCRect rectorange = CCRectMake(orange->getPosition().x-(orange->getContentSize().width/2)
,orange->getPosition().y-(orange->getContentSize().height/2)
,orange->getContentSize().width
,orange->getContentSize().height);
CCRect rectlanzi = CCRectMake(lanzi->getPosition().x-(lanzi->getContentSize().width/2)
,lanzi->getPosition().y-(lanzi->getContentSize().height/2)
,lanzi->getContentSize().width
,lanzi->getContentSize().height);
if(rectorange.intersectsRect(rectlanzi))
{
orange = CCSprite::create("ball.jpg");
orange->setPosition(ccp(200,200));
this->addChild(orange,0);
CCLog("YYYYY");
}
else
{
CCLog("NNNNN");
}
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
CCSprite* orange = CCSprite::create("ball.jpg");
orange->setPosition(ccp(150,300));
this->addChild(orange,0);
//移动水果
CCActionInterval* moveto = CCMoveTo::create(3,ccp(150,0));
orange->runAction(moveto);
//
CCSprite *lanzi = CCSprite::create("lanzi.jpg");
lanzi->setPosition(ccp(150,0));
this->addChild(lanzi,0);
for(int i=0;i++;i<100){
Sleep(1000);
CCRect rectorange = CCRectMake(orange->getPosition().x-(orange->getContentSize().width/2)
,orange->getPosition().y-(orange->getContentSize().height/2)
,orange->getContentSize().width
,orange->getContentSize().height);
CCRect rectlanzi = CCRectMake(lanzi->getPosition().x-(lanzi->getContentSize().width/2)
,lanzi->getPosition().y-(lanzi->getContentSize().height/2)
,lanzi->getContentSize().width
,lanzi->getContentSize().height);
if(rectlanzi.intersectsRect(rectorange))
{
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
CC_BREAK_IF(! pLabel);
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, 1);
CCLog("YYYYY");
}
else
{
CCLog("NNNNN");
}}
// "close" menu item clicked
// CCDirector::sharedDirector()->end();
}