我想让我的角色在我自己的基于SDL2的框架中登陆平台。目前它没有工作,因为,如果我有一个以上的平台,这是默认,如果我产生一个。角色只会留在最后添加的平台上。当grounded
布尔值从true
切换到false
时,其余的会慢慢落下。
这是我的代码:
void NHTVScene::EntitiesGrounded()
{
std::vector<Entity*>::iterator it = platformVector.begin();
while (it != platformVector.end()) {
if (player->isColliding((*it))) {
player->velocity = Vector2(0, 0);
player->grounded = true;
}
else if (!player->isColliding((*it))) {
player->grounded = false;
}
it++;
}
}
每个deltatime更新都会调用此函数。
我知道这段代码使得每个平台都没有发生碰撞,布尔值被设置为true,但我觉得奇怪的是它在最新的条目上没有这样做,而且我有不知道如何正确地做到这一点。
我希望这足以让我对问题有所了解。如果没有,请告诉我,我会提供更多。
答案 0 :(得分:0)
Public Sub Test()
Dim lRow As Long
Dim lTarget As Long
Dim x As Long
Dim rSource As Range
Dim rTarget As Range
lTarget = 9 'First row that data appears on in "Tab 2 - Nieuwe prijzen"
x = 0 'Used in loop to move down rows in "Tab 2 - Nieuwe prijzen"
'lRow is the row number on "Tab 1 - Prijslijst"
For lRow = 2 To 10
'Set the ranges for the specific row.
Set rSource = ThisWorkbook.Worksheets("Tab 1 - Prijslijst").Range("CQ" & lRow & ":ED" & lRow)
Set rTarget = ThisWorkbook.Worksheets("Tab 2 - Nieuwe prijzen").Range("D" & lTarget + x & ":AQ" & lTarget + x)
'Check the cell values in the row and flow code accordingly.
If CellValuesAreTheDifferent(rSource, rTarget) Then
Debug.Print "Colour Cell."
Else
Debug.Print "Call function."
End If
x = x + 1
Next lRow
End Sub
Public Function CellValuesAreTheDifferent(rSource As Range, rTarget As Range) As Boolean
Dim y As Long
'Check each cell value in the passed range.
For y = 1 To rSource.Cells.Count
'If the cell values are different then set to TRUE and exit the function.
If rSource.Cells(y) <> rTarget.Cells(y) Then
CellValuesAreTheDifferent = True
Exit For
End If
Next y
End Function
和while
子句,请考虑将嵌套缩进级别格式化得更清楚。if else
是一个布尔值,因此在检查为true之后检查为false应该是不必要的。考虑简化这种逻辑。这样的事情应该做你正在寻找的事情。
isColliding(Entity)