尊重开发者的劳动成果,转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/17151883
上节回顾
【cocos2d-x IOS游戏开发-捕鱼达人15】炮弹攻击效果1
1、 炮弹的移动
炮弹规则
玩家点击屏幕后,将从大炮位置向点击位置发射一枚炮弹。
炮弹的移动所需的值
炮弹速度
炮弹发射点
炮弹发射方向
炮弹射程
不同等级的差异
不同等级的炮弹速度不一样(等级越高,速度越慢)
不同等级的炮弹射程不一样(等级越高,射程越大)
炮弹速度相关的计算
根据速度方向,计算出炮弹的水平增量(cos)和竖直增量(sin)
2、 炮弹的爆炸效果
爆炸的时机
达到最大射程
与鱼产生碰撞
爆炸的效果
在爆炸点产生一个网。一定时间后消失
实现触屏相应:
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { //CC_UNUSED_PARAM(pTouch); //CC_UNUSED_PARAM(pEvent); cocos2d::CCPoint pt = pTouch->getLocationInView(); pt = cocos2d::CCDirector::sharedDirector()->convertToGL(pt); cocos2d::CCPoint cannonPt = getPosition(); float dx = pt.x - cannonPt.x; float dy = pt.y - cannonPt.y; float t = dx/dy; float at = atan(t); float degree = at/3.1415926*180; //旋转炮台 m_pCannon->setRotation(degree); //相应触屏事件处理 Event_TapScreen(cannonPt,degree); //printf("touch %f,%f\n",pTouch->locationInView().x,pTouch->locationInView().y); return false; }
base::Delegate<void(const CCPoint&,float)> tes(this,&CPlayUILogic::slot_TapScreen); m_CannonLayout.Event_TapScreen += tes;触屏响应:
void slot_TapScreen(const CCPoint& pt,float dir) { printf("pt:(%f,%f),dir:%f \n",pt.x,pt.y,dir); m_pGameLogic->onShoot(pt.x,pt.y,dir); }射出炮弹:
void CGameLogic::onShoot(float x,float y,float dir ) { scene::ISceneObject* pS = m_pGameScene->createObject(1005); CBulletObject* pObj = new CBulletObject(pS,this,m_iLevel); pObj->setPosition(x,y); pObj->setDuration(dir); m_vecBulletsList.push_back(pObj); }炮弹状态改变:
class CBulletObject { enum{ EXPLODE_TIME = 1, }; scene::ISceneObject* m_pSceneObject; EBulletObjectState m_eButtletObjectState; IGameLogic* m_pGameLogic; float m_OrgX; float m_OrgY; float m_CurrentX; float m_CurrentY; float m_Dir; float m_Time; bool m_bNeedDestroyed; float m_Speed; float m_Distance; public: CBulletObject(scene::ISceneObject* pObj,IGameLogic* pLogic,int level) :m_pSceneObject(pObj) ,m_pGameLogic(pLogic) ,m_eButtletObjectState(EBOS_NORMAL) ,m_Time(0) ,m_bNeedDestroyed(false) { const config::SBulletData* pData = config::CBulletConfig::getData(level); if(pData==NULL){ m_Speed = 0; m_Distance = 0; } else{ m_Speed = pData->Speed; m_Distance = pData->Distance; } char buf[32]; sprintf(buf,"bullet%d",level); pObj->setAnimation(buf); } scene::ISceneObject* getSceneObj(){ return m_pSceneObject;} bool needDestroyed(){ return m_bNeedDestroyed; } void setPosition(float x,float y) { m_OrgX = x; m_OrgY = y; m_CurrentX = x; m_CurrentY = y; m_pSceneObject->setPosition(x,y); } void setDuration(int degree) { m_Dir = degree; m_pSceneObject->setDuration(degree); } void update(float dt) { m_Time+=dt; switch(m_eButtletObjectState) { case EBOS_NORMAL: updateNormal(dt); break; case EBOS_EXPLODE: if(m_Time > EXPLODE_TIME){ m_bNeedDestroyed = true; } break; } } private: void updateNormal(float dt) { float angle = (90-m_Dir)/180*3.1416926; float s = sin(angle); float c = cos(angle); float dtx = m_Speed*c*dt; float dty = m_Speed*s*dt; m_CurrentX += dtx; m_CurrentY += dty; m_pSceneObject->setPosition(m_CurrentX,m_CurrentY); dtx = m_CurrentX - m_OrgX; dty = m_CurrentY - m_OrgY; if(dtx*dtx+dty*dty > m_Distance*m_Distance){ m_Time = 0; m_eButtletObjectState = EBOS_EXPLODE; m_pSceneObject->setAnimation("net"); } } };可以看到效果图:
下节实现炮弹与鱼的碰撞检测