bool HelloWorld::init()
{
//
// 1. super init first
if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255)))
{
return false;
}
m_arrTar = new CCArray;
m_arrPro = new CCArray;
CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();
CCSprite *pSprite = CCSprite::create("Player.png");
pSprite->setPosition(ccp(20.0,screenSize.height/2));
this->addChild(pSprite);
this->schedule(schedule_selector(HelloWorld::time),2);
this->schedule(schedule_selector(HelloWorld::update));
this->setTouchEnabled(true);
return true;
}
void HelloWorld::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
CCTouch *touch = (CCTouch *)pTouches->anyObject();
CCPoint locInVw = touch->getLocationInView();
CCPoint locInGl = CCDirector::sharedDirector()->convertToGL(locInVw);
CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();
CCSprite *proj = CCSprite::create("Projectile.png");
proj->setPosition(ccp(20.0,screenSize.height/2.0));
this->addChild(proj);
m_arrPro->addObject(proj);
proj->setTag(2);
double dx = locInGl.x - 20;
double dy = locInGl.y - screenSize.height/2.0;
double d = sqrt(dx*dx + dy*dy);
double D = sqrt(screenSize.width * screenSize.width + screenSize.height * screenSize.height);
double ratio = D/d;
double endx = ratio * dx + 20;
double endy = ratio *dy + screenSize.height/2.0;
CCMoveTo *move = CCMoveTo::create(D/320,ccp(endx,endy));
CCCallFuncN *sd = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefinedAc));
CCSequence *actions = CCSequence::create(move,sd,NULL);
proj->runAction(actions);
}
void HelloWorld::update(float val)
{
CCObject *tar,*pro;
CCARRAY_FOREACH(m_arrTar,tar)
{
CCSprite *pTar = (CCSprite *)tar;
CCRect rtTar = CCRectMake(pTar->getPosition().x,pTar->getPosition().y,pTar->getContentSize().width
,pTar->getContentSize().height);
CCARRAY_FOREACH(m_arrPro,pro)
{
CCSprite *pPro = (CCSprite *)pro;
CCRect rtPro = CCRectMake(pPro->getPosition().x,pPro->getPosition().y,pPro->getContentSize().width
,pPro->getContentSize().height);
if(rtPro.intersectsRect(rtTar))
{
m_arrTar->removeObject(pTar);
m_arrPro->removeObject(pPro);
pTar->removeFromParentAndCleanup(true);
pPro->removeFromParentAndCleanup(true);
break;
}
}
}
}
void HelloWorld::time(float val)
{
spriteAct();
}
void HelloWorld::spriteAct()
{
CCSize screenSize = CCDirector::sharedDirector()->getVisibleSize();
CCSprite *pSprite = CCSprite::create("Target.png");
int y = rand()%(int)screenSize.height;
pSprite->setPosition(ccp(screenSize.width-20,y));
this->addChild(pSprite);
m_arrTar->addObject(pSprite);
pSprite->setTag(1);
CCMoveTo *move = CCMoveTo::create(3.0,ccp(0,y));
CCCallFuncN *pSelfdefinedAc = CCCallFuncN::create(this,callfuncN_selector(HelloWorld::selfdefinedAc));
CCSequence *actions = CCSequence::create(move,pSelfdefinedAc,NULL);
pSprite->runAction(actions);
}
void HelloWorld::selfdefinedAc(CCNode* pSender)
{
pSender->removeFromParentAndCleanup(true);
int tag = pSender->getTag();
if(1 == tag)
{
m_arrTar->removeObject(pSender);
}
else if(2 == tag)
{
m_arrPro->removeObject(pSender);
}
}
HelloWorld::~HelloWorld()
{
delete m_arrTar;
delete m_arrPro;
}