试图实施触摸和保持(子弹射击)? - cocos2d-x

时间:2014-02-22 02:15:30

标签: c++ ios cocos2d-x

即使在查看其他人的解决方案之后,我也很难实现一个平稳可靠的触发保持解决方案来解雇子弹(精灵)。

解决方案必须在触摸开始,触摸移动和触摸无缝结束之间切换:始终在触摸位置发射子弹,直到手指被释放。目前我在可靠性和稳定性方面存在许多问题,但每个案例都有触摸移动,这很好。

准确的问题是手指按住(touchBegan + scheduler)大约一半的时间,但子弹出现但稍后消失,但有时候他们会完美地移动触摸 - 有些东西正在删除它们,我没有太多东西体验调度员或行动以了解什么。

继承我的代码,我使用了两种不同的触发方法:一种计划在touchBegan之后每0.05秒运行一次,每次触摸touchMoved时触发一次。 touchMoved一个正常工作,但让它与不可靠的touchBegan工作是一个麻烦。真正烦人的部分是,即使我删除了触摸部分,只是安排精灵出现并从init不间断地运行预定动作,同样的可靠性问题发生(消失/删除)。也许我不明白让调度程序和操作发挥得很好,或者可能是一个更好的触摸和保持方法?提前感谢您的帮助。

bool HelloWorld::init()
{ 
... miscellaneous sprite creation
this->schedule(schedule_selector(HelloWorld::fireBullets), 0.05);   
}

void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    CCTouch* touch = (CCTouch*)( pTouches->anyObject() );               // get single-touch as opposed to multitouch

    touchLocation = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());

    if (touchLocation.x > 400)
    {
        float dX = touchLocation.x - gun->getPosition().x;
        float dY = touchLocation.y - gun->getPosition().y;

        touchAngle = atan2(dY, dX);  
        gun->setRotation(-CC_RADIANS_TO_DEGREES(touchAngle));

        cursor->setPosition(touchLocation);

        screenHeld = true;
    }
}

void HelloWorld::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
    CCTouch* touch = (CCTouch*)( pTouches->anyObject() );               // for single-touch as opposed to multitouch

    touchLocation = CCDirector::sharedDirector()->convertToGL(touch->getLocationInView());

    if (touchLocation.x > 400)
    {   
        float dX = touchLocation.x - gun->getPosition().x;
        float dY = touchLocation.y - gun->getPosition().y;

        float angle = atan2(dY, dX);  
        gun->setRotation(-CC_RADIANS_TO_DEGREES(angle));

        cursor->setPosition(touchLocation);

        screenHeld = false; //not technically true but touchMoved bullet firing works differently (not scheduled, every movement instead)

        if (getTimeTick() - lastBulletFire > 50) //getTickTime is simple get system time method, works fine
        {
            fireBullet(angle);
        }
    }
}

//this is for touchBegan and has issues, scheduled to run every 50ms touch-held
void HelloWorld::fireBullets(CCTime dt) 
{
  if (screenHeld)
  {
    CCSprite* bullet = CCSprite::create("bullet.png");
    bullet->setPosition(ccp(gun->getPosition().x, gun->getPosition().y));
    bullet->setRotation(-CC_RADIANS_TO_DEGREES(touchAngle));

    //send bullet towards touchlocation
    bullet->runAction(CCSequence::create(CCMoveBy::create(1.5f, ccp(800 * cos(touchAngle), 800 * sin(touchAngle))), NULL));

    this->addChild(bullet, 5);
  }
}


//this is for touchMoved and works fine, everytime finger is moved bullet fired
void HelloWorld::fireBullet(float angle) 
{
  CCSprite* bullet = CCSprite::create("bullet.png");
  bullet->setPosition(ccp(gun->getPosition().x, gun->getPosition().y)); //add a random spread to the y value (or maybe the y-value of the destination)
  this->addChild(bullet, 5);

  bullet->setRotation(-CC_RADIANS_TO_DEGREES(angle));

  bullet->runAction(CCSequence::create(CCMoveBy::create(1.5f, ccp(800 * cos(angle), 800 * sin(angle))), NULL));

  lastBulletFire = getTimeTick();
}

1 个答案:

答案 0 :(得分:0)

找到解决方案,它与动作/调度程序没有任何关系,但事实上我没有在子弹上调用retain()。通常情况下,我会手动管理C ++内存,但这种混合风格让我失去了