【问题标题】:Stuck trying to implement touch and hold (bullet firing)? - cocos2d-x试图实现触摸并按住(子弹射击)? - cocos2d-x
【发布时间】:2014-02-22 02:15:29
【问题描述】:

即使在查看了其他人的解决方案之后,我在实施用于发射子弹(精灵)的流畅且可靠的触控解决方案时也遇到了很多麻烦。

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

确切的问题是大约一半的时间手指被按住(touchBegan + scheduler)子弹出现但一秒钟后消失但其他时候它们完美地移向触摸 - 有些东西正在删除它们,我没有太多了解调度程序或操作的经验。

这是我的代码,我使用了 2 种不同的触发方法:一种计划在 touchBegan 后每 0.05 秒运行一次,另一种在每次检测到 touchMoved 时触发。 touchMoved 可以正常工作,但是要让它与不可靠的 touchBegan 一起工作是很麻烦的。真正令人讨厌的部分是,即使我删除了触摸部分,只是安排精灵出现并从初始化开始不间断地运行预定的动作,同样的可靠性问题也会发生(消失/删除)。也许我不明白让调度程序和动作发挥得很好,或者也许有更好的触摸和保持方法?提前感谢您的帮助。

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

【问题讨论】:

  • 因为cocos2d-x是多平台的,我是在安卓手机上开发的
  • getTimeTick() 是在哪里定义的?

标签: c++ ios cocos2d-x


【解决方案1】:

找到了解决方案,这与动作/调度程序无关,而是我没有在项目符号上调用 retain()。通常我会手动管理 C++ 内存,但这种混合风格让我失望了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多