cocos2d-x学习三触摸抬起响应喷发子弹

来源:互联网 发布:网络买卖软件 编辑:程序博客网 时间:2024/05/17 02:08



bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayerColor::initWithColor(ccc4(255,255,255,255)))    {        return false;    } 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->setTouchEnabled(true);    return true;}


1.固定子弹速度,固定距离D,也就是固定了move动作的时间间隔。

2.通过相似三角形获得子弹终点。

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);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::selfdefinedAc(CCNode* pSender){//m_pSprite->setPosition(ccp(0.0,0.0));//m_pSprite->setScale(2.0);pSender->removeFromParentAndCleanup(true);}


0 0