cocos2d-x学习四子弹打怪兽结合二、三学习



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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MyObject-C

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值