获得父类的node型指针调用父类函数this->getParent()

本文解析了Cocos2d-x中子层如何正确调用父层函数,通过实例说明了使用this关键字获取父类指针并进行类型转换的过程,避免了子类对象直接调用可能出现的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. void CenterLayer::zhanzheng(CCObject* pSender){  
  2.     ((GameScene*)this->getParent())->showLayer(GameScene::UI_SelectLayer);  
  3. }  

在这里 this->getParent() 会获得一个父类的一个node型指针,然后转换为父类类型,可以使用这个指针调用父类函数。

 

[cpp] view plain copy print?

  1. _centerLayer=CenterLayer::create();  
  2. this->addChild(_centerLayer);  

 

CenterLayer是一个层,添加在GameScene上面的场景上面的,

1:这里满的关键字要用this去掉使用getParten()函数,如果用子类对象去调用会出现错误。

2:强制类型转换(GameScene*)将node*类型的指针转换为GameScene*的类型

// 投影滤波器 void MainWindow::doActionPCLProjectInliers() { if (getSelectedEntities().size() != 1) { ccLog::Print(QStringLiteral("只能选择一个点云实体")); return; } ccHObject* entity = getSelectedEntities()[0]; ccPointCloud* ccCloud = ccHObjectCaster::ToPointCloud(entity); // ---------------------------读取数据到PCL---------------------------------- pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); cloud->resize(ccCloud->size()); for (int i = 0; i < cloud->size(); ++i) { const CCVector3* point = ccCloud->getPoint(i); cloud->points[i].x = point->x; cloud->points[i].y = point->y; cloud->points[i].z = point->z; } // -----------------------------对话框--------------------------------------- float radius = QInputDialog::getDouble(this, QStringLiteral("参数设置"), QStringLiteral("搜索半径: "), 0.1, 0, 100, 4); // ----------------------------投影滤波器-------------------------------------- //创建 x+y+z=0 平面 pcl::ModelCoefficients::Ptr coefficients(new pcl::ModelCoefficients()); coefficients->values.resize(4); //设置模系数的大小 coefficients->values[0] = 1.0; //x系数 coefficients->values[1] = 1.0; //y系数 coefficients->values[2] = 1.0; //z系数 coefficients->values[3] = 0.0; //常数项 //投影滤波 pcl::PointCloud<pcl::PointXYZ>::Ptr filtered(new pcl::PointCloud<pcl::PointXYZ>); pcl::ProjectInliers<pcl::PointXYZ> us; us.setModelType(pcl::SACMODEL_PLANE); //设置对象对应的投影模 us.setInputCloud(cloud); //设置输入点云 us.setModelCoefficients(coefficients);//设置模对应的系数 us.filter(*filtered); // ------------------------PCL->CloudCompare-------------------------------- if (!filtered->empty()) { ccPointCloud* newPointCloud = new ccPointCloud(QString("ProjectInliers")); for (int i = 0; i < filtered->size(); ++i) { double x = filtered->points[i].x; double y = filtered->points[i].y; double z = filtered->points[i].z; newPointCloud->addPoint(CCVector3(x, y, z)); } newPointCloud->setRGBColor(ccColor::Rgba(255, 255, 255, 255)); newPointCloud->showColors(true); if (ccCloud->getParent()) { ccCloud->getParent()->addChild(newPointCloud); } ccCloud->setEnabled(false); addToDB(newPointCloud); refreshAll(); updateUI(); } else { ccCloud->setEnabled(true); // Display a warning message in the console dispToConsole("Warning: example shouldn't be used as is", ccMainAppInterface::WRN_CONSOLE_MESSAGE); } } 提供代码绘制的关键函数关系图,图中使用中文注释关键步骤和函数调用关系
最新发布
07-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值