射击小游戏一01(子弹添加添加)

void HelloWorld::ccTouchesEnded(CCSet *touches,cocos2d::CCEvent *event)

{

    CCTouch *touch = (CCTouch*)(touches->anyObject());

    CCPoint location = touch->getLocationInView();

    location = CCDirector::sharedDirector()->convertToGL(location);

    

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCSprite *projectile = CCSprite::create("Projectile.png",CCRectMake(0, 0, 20, 20));

    projectile->setPosition(ccp(20, winSize.height/2));

    

     // Determinie offset of location to projectile

    int offX = location.x - projectile->getPosition().x;

    int offY = location.y - projectile->getPosition().y;

    

    if (offX <= 0) return;

    

    this->addChild(projectile);

    

     // Determine where we wish to shoot the projectile to

    int realX = winSize.width + (projectile->getContentSize().width/2);

    float ratio = (float)offY/(float)offX;

    int realY = (realX*ratio) + projectile->getPosition().y;

    CCPoint realDest = ccp(realX,realY);

    

     // Determine the length of how far we're shooting

    int offRealX = realX - projectile->getPosition().x;

    int offRealY = realY - projectile->getPosition().y;

    float length = sqrtf(( offRealX * offRealX )+(offRealY * offRealY));

    

    float velocity = 480/1;// 480pixels/1sec

    float realMoveDuration = length / velocity;

    

    // Move projectile to actual endpoint

    projectile->runAction(CCSequence::create(CCMoveTo::create(realMoveDuration, realDest),CCCallFuncN::create(this, callfuncN_selector(HelloWorld::spriteMoveFinished)),NULL));

    

    

}
原文地址:https://www.cnblogs.com/jiackyan/p/3017899.html