Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了关于cocos2dx 中 std::vector中删除某个元素的认识大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
//_robot是一个std::vector的数组,定义为std::vector<Robot *> _robot
//Robot是我自己定义的一个类
//首先要先取出_robot中的robot在哪里,得到a
auto a = std::find(_robots.begin(),_robots.end(),robot);
//然后再移除robot
removeChild(robot);
//最后从_robot中删除a这个位置
_robots.erase(a);

但是在迭代器中使用这种方法会报错,经人指点,学习到另一种方法!

//如果金币容器不为0则遍历容器内的node
	if (goldArmature.size()!=0)
	{
		for (int i = 0; i < goldArmature.size();i++)
		{
			//得到所取出的gold的当前X Y坐标
			auto x = goldArmature.at(i)->getPositionX();
			auto y = goldArmature.at(i)->getPositionY();
			//如果金币飞出屏幕的把金币存入另一个容器goldOutRange中
			if (x< -goldArmature.at(i)->getContentSize().width / 2 || x>visibleSize.width + goldArmature.at(i)->getContentSize().width / 2 ||
				y<-goldArmature.at(i)->getContentSize().height / 2 || y>visibleSize.height + goldArmature.at(i)->getContentSize().height / 2)
			{
				removeChild(goldArmature.at(i));
				goldArmature.erase(i);
			}
		}
	}
这样就可以删除其中的某一项了!

大佬总结

以上是大佬教程为你收集整理的关于cocos2dx 中 std::vector中删除某个元素的认识全部内容,希望文章能够帮你解决关于cocos2dx 中 std::vector中删除某个元素的认识所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。