在尝试执行触摸和保持(子弹发射)时卡住了?-椰子2d-x
Stuck trying to implement touch and hold (bullet firing)? - cocos2d-x
我在为发射子弹(精灵)实现平稳可靠的触摸保持解决方案时遇到了很多困难,即使在查看了其他人的解决方案之后也是如此。
解决方案必须在触摸开始、触摸移动和触摸结束之间无缝切换:始终在触摸位置发射子弹,直到手指松开。目前,我在每一个案例的可靠性和稳定性方面都有很多问题,但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++内存,但这种混合风格让我放弃了
相关文章:
- 2D数组来自文本输入,中间有空格
- 将值指定给向量(2D)的向量中的某个位置
- 如何使用用户输入在C++中正确填充2D数组
- 如何在C++中检查2D数组中负值的输入验证
- 当我在main中声明了我的2d数组时,为什么我的程序会退出
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 四边形的 2D 旋转
- 打印第二列时的2d字符矢量打印空间
- 如何将以逗号和空格分隔的整数读取到 2D 数组中?
- 如何在C++函数中声明静态 2D 数组?
- 我是 C++ 的初学者,我想知道如何在 2D 矢量中获取重复值
- 如何声明一个可以在整个程序中使用的全局 2d 3d 4d .. 数组(堆版本)变量?
- opengl glBegin(GL_LINES) 和 glBegin(GL_POINT) 在 2D 中不可视化点矢量
- 在C++中迭代 2D 容器的最干净方法
- 如何引用 2D 指针?
- 如何在 C++ 中使用它的构造函数初始化 unique_ptrs 的 2D 向量?
- 在尝试执行触摸和保持(子弹发射)时卡住了?-椰子2d-x
- 圆角矩形在椰子2d-x与贝塞尔
- 如何使用rapidjson浏览.json中的数组?(椰子2d-x)
- 椰子2d-x中的EXC_BAD_ACCESS