在尝试执行触摸和保持(子弹发射)时卡住了?-椰子2d-x

Stuck trying to implement touch and hold (bullet firing)? - cocos2d-x

本文关键字:2d-x 椰子 发射 子弹 执行 触摸      更新时间:2023-10-16

我在为发射子弹(精灵)实现平稳可靠的触摸保持解决方案时遇到了很多困难,即使在查看了其他人的解决方案之后也是如此。

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

确切的问题是,大约有一半的时间手指被按下(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();
}

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