这是我想要做的:
loop {
nums1[backwards_idx] = nums1[m_idx];
if m_idx == 0 { break; }
m_idx -= 1;
backwards_idx -= 1;
}
我得到的错误是:
interface VehicleSeatData {
index: number;
positions: Vector3Mp;
}
interface VehicleSeat {
[key: string]: VehicleSeatData;
}
getSeatData(vehicle: VehicleMp): VehicleSeat | null {
if(!vehicle) {
return null;
}
let seats: VehicleSeat = {};
seats['seat_r'].index = vehicle.getBoneIndexByName('seat_r');
seats['seat_pside_f'].index = vehicle.getBoneIndexByName('seat_pside_f');
seats['seat_r'].positions = vehicle.getWorldPositionOfBone(seats['seat_r'].index);
seats['seat_pside_f'].positions = vehicle.getWorldPositionOfBone(seats['seat_pside_f'].index);
return seats;
}
我不确定我哪里出错了,我读过的所有内容(到目前为止)都告诉我我的方向是正确的。这不可能吗?
答案 0 :(得分:0)
您将 seats
初始化为一个空对象。
let seats = {}
。
然后您访问默认为 seats.seat_r
的 seats['seat_r']
(与 undefined
相同)。然后您尝试访问其不存在的 index
属性 (undefined.index
-> property 'index' of undefined
)。
为了解决这个问题,您必须将 seats
的每个属性设置为 VehicleSeatData
类型的空版本。
let seats: VehicleSeat = {
'seat_r': {
index: undefined,
positions: undefined,
},
'seat_pside_f': {
index: undefined,
positions: undefined,
},
'seat_r': {
index: undefined,
positions: undefined,
},
'seat_pside_f': {
index: undefined,
positions: undefined,
},
};
// It is also enough to just initialize the properties as an empty object:
// let seats: VehicleSeat = {
// 'seat_r': {},
// 'seat_pside_f': {},
// 'seat_r': {},
// 'seat_pside_f': {},
// };
// Now you can access the properties and set its sub-properties.
seats['seat_r'].index = vehicle.getBoneIndexByName('seat_r');
seats['seat_pside_f'].index = vehicle.getBoneIndexByName('seat_pside_f');
seats['seat_r'].positions = vehicle.getWorldPositionOfBone(seats['seat_r'].index);
seats['seat_pside_f'].positions = vehicle.getWorldPositionOfBone(seats['seat_pside_f'].index);
这也意味着你可能需要改变你的类型
interface VehicleSeatData {
index: number;
positions: Vector3Mp;
}
到
interface VehicleSeatData {
index?: number;
positions?: Vector3Mp;
}
因为在此示例中,属性可能未定义,直到您正确设置它们。
答案 1 :(得分:-1)
VehicleSeatData 没有任何属性 Seat_r,请尝试以下代码。
seats = {
seat_r:{
index: vehicle.getBoneIndexByName('seat_r');
positions: Vector3Mp;
}
}