C++ 在尝试实施触摸和保持(子弹射击)时卡住了?-cocos2d-x
即使在看过别人的解决方案后,我也一直很难实现一个平滑可靠的触屏式射击解决方案(sprites) 解决方案必须在触摸开始、触摸移动和触摸结束之间无缝切换:始终在触摸位置发射子弹,直到手指松开。目前,我在可靠性和稳定性方面遇到了很多问题,但touchmoved除外,它运行良好 确切的问题是,大约有一半的时间手指被按下(touchStart+scheduler),子弹会出现,但会在一秒钟后消失,但其他时候它们会朝着触摸方向完美移动——有些东西正在删除它们,我对调度程序或动作没有太多经验,不知道是什么 这是我的代码,我使用了两种不同的触发方法:一种计划在TouchStart之后每0.05秒运行一次,另一种在每次检测到touchMoved时触发。touchMoved一个工作正常,但要让它与不可靠的touchMoved一个一起工作很麻烦。真正令人恼火的是,即使我删除了触摸部分,只安排精灵出现,并从init不间断地运行计划动作,同样的可靠性问题也会发生(消失/删除)。也许我不明白如何让调度程序和动作更好地发挥作用,或者也许有更好的“触控-保持”方法?提前感谢您的帮助C++ 在尝试实施触摸和保持(子弹射击)时卡住了?-cocos2d-x,c++,ios,cocos2d-x,C++,Ios,Cocos2d X,即使在看过别人的解决方案后,我也一直很难实现一个平滑可靠的触屏式射击解决方案(sprites) 解决方案必须在触摸开始、触摸移动和触摸结束之间无缝切换:始终在触摸位置发射子弹,直到手指松开。目前,我在可靠性和稳定性方面遇到了很多问题,但touchmoved除外,它运行良好 确切的问题是,大约有一半的时间手指被按下(touchStart+scheduler),子弹会出现,但会在一秒钟后消失,但其他时候它们会朝着触摸方向完美移动——有些东西正在删除它们,我对调度程序或动作没有太多经验,不知道是什么
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();
}
找到了解决方案,这与操作/调度程序无关,而是我没有对项目符号调用retain()。通常我会手动管理C++内存,但是这种混合方式抛弃了我的,因为COCOS2D-X是多平台的,而在Android PooeWe上开发的是GETTimeTime[]定义的。