ts报类型“{}”上不存在属性“isDrag”
时间: 2024-06-11 22:06:01 浏览: 12
这个错误通常表示您在尝试访问一个不存在的属性。如果您的ts报类型“{}”上不存在属性“isDrag”,那么很可能是因为您的代码中没有定义“isDrag”属性或者您正在访问错误的变量。
解决此问题的方法是检查代码中是否存在“isDrag”属性的定义,并确保您正在访问正确的变量。您可以尝试使用断点调试或打印语句来确定代码执行到哪里,以及变量的值和类型。另外,如果您使用的是第三方库或框架,检查其文档以查看其属性和方法的正确使用方式。
相关问题
遍历QVector<stuARLabelShow> m_vecStuARLabelShow ,如果LabelShow中point 在鼠标范围5px内则将鼠标显示可拖拽。并将数组内LabelShow.isdrag 置为true
可以通过以下代码实现遍历过程、判断条件和修改 `isdrag` 标志位:
```c++
for(int i = 0; i < m_vecStuARLabelShow.size(); i++){
stuARLabelShow labelShow = m_vecStuARLabelShow.at(i);
QPoint labelPoint = labelShow.point;
QPoint mousePoint = QCursor::pos();
int distance = qSqrt(qPow(labelPoint.x() - mousePoint.x(), 2) + qPow(labelPoint.y() - mousePoint.y(), 2));
if(distance <= 5){
setCursor(Qt::OpenHandCursor);
m_vecStuARLabelShow[i].isdrag = true;
break;
}
}
```
其中,`m_vecStuARLabelShow` 是一个存储 `stuARLabelShow` 结构体的 QVector,`stuARLabelShow` 结构体包含一个 QPoint 类型的 point 成员变量和一个 bool 类型的 isdrag 成员变量,表示该标签在窗口中的位置以及是否可拖拽。在遍历 QVector 时,我们通过 `at` 方法获取每个结构体,然后获取该结构体的 point 成员变量。接着,我们获取当前鼠标的位置,并计算鼠标与标签位置的距离。如果距离小于等于 5 像素,则将鼠标的光标设置为可拖拽状态(OpenHandCursor),并将该结构体的 isdrag 标志位设置为 true。最后,我们退出遍历即可。这样,在后续的拖拽操作中,我们就可以根据 isdrag 标志位来判断哪些标签是可拖拽的。
if (ID == -1 || other.gameObject.layer != 9) { return; } if (other.transform.parent.name == "卡钳" && ID == 0) { transform.parent.Find("卡钳").gameObject.SetActive(true); other.transform.parent.GetComponent<FlashingController>().CloseHighlight(); DragObj = other.transform.gameObject; DragObj.SetActive(false); ID = 1; StepFubnction(ID); } else if (other.transform.name == "旧平衡块1" && ID == 1) { other.transform.GetComponent<FlashingController>().CloseHighlight(); other.transform.GetChild(0).gameObject.SetActive(true); other.transform.GetChild(0).GetComponent<FlashingController>().ShowHighlight(); DragObj.SetActive(false); isDrag = false; ID = 2; StepFubnction(ID); } else if (other.transform.parent.name == "卡钳透明" && ID == 2) { transform.parent.Find("卡钳").gameObject.SetActive(false); DragObj.SetActive(true); ID = 3; StepFubnction(ID); }优化代码
以下是可能的代码优化建议:
1. 将判断条件简化,避免多余的判断:
```
if (ID == -1 || other.gameObject.layer != 9) {
return;
}
if (ID == 0 && other.transform.parent.name == "卡钳") {
//...
}
else if (ID == 1 && other.transform.name == "旧平衡块1") {
//...
}
else if (ID == 2 && other.transform.parent.name == "卡钳透明") {
//...
}
```
2. 避免重复查找组件和对象,可以在 Start 方法中缓存相关组件和对象:
```
private GameObject cardClamp;
private GameObject dragObj;
private GameObject oldBalanceBlock1;
void Start() {
cardClamp = transform.parent.Find("卡钳").gameObject;
dragObj = ...; // 根据实际情况查找
oldBalanceBlock1 = ...; // 根据实际情况查找
}
void OnTriggerEnter(Collider other) {
if (ID == -1 || other.gameObject.layer != 9) {
return;
}
if (ID == 0 && other.transform.parent == cardClamp.transform) {
cardClamp.SetActive(true);
other.transform.parent.GetComponent<FlashingController>().CloseHighlight();
DragObj = other.transform.gameObject;
DragObj.SetActive(false);
ID = 1;
StepFubnction(ID);
}
else if (ID == 1 && other.transform == oldBalanceBlock1.transform) {
other.transform.GetComponent<FlashingController>().CloseHighlight();
other.transform.GetChild(0).gameObject.SetActive(true);
other.transform.GetChild(0).GetComponent<FlashingController>().ShowHighlight();
DragObj.SetActive(false);
isDrag = false;
ID = 2;
StepFubnction(ID);
}
else if (ID == 2 && other.transform.parent.name == "卡钳透明") {
cardClamp.SetActive(false);
DragObj.SetActive(true);
ID = 3;
StepFubnction(ID);
}
}
```
这样可以提高代码的可读性和执行效率。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)