老虎机转盘停在正确位置时出现问题

时间:2019-06-20 04:50:29

标签: game-loop cocossharp

我正在Cocossharp中建立一个老虎机游戏,但在使卷轴停在正确的位置并保持两块之间的间距相等方面遇到问题。 每行的底部应等于底部变量。

为了将片段强制放置在正确的位置,我调用AdjustStoppedPosition()。这样做的问题是,它开始在部件之间放置额外的空间。

moveBySpeed = 95;
offset = 10;
bottom = 260; // this is where the bottom of the last visible piece should be
// lastNode is set to the last (very top) piece that was added

private void OnFrame(float timeInSeconds)
{
    if (doneCount == ChildrenCount)
    {
        doneCount = 0;
        adjusting = false;
        OnStopped();
    }

//The following is being called in the game loop:

if (moveBySpeed <= 0)
    return;

for (int i = 0; i < ChildrenCount; i++)
{
    var piece = Children[i];
    if (piece.PositionY - moveBySpeed >= 0)
    {
        piece.Position = new CCPoint(piece.PositionX, piece.PositionY - moveBySpeed);
    }
    else
    {
        // Move back to the top.                
        piece.Position = new CCPoint(piece.PositionX, lastNode.BoundingBox.MaxY + offset);
        lastNode = piece;                        
    }
}

moveBySpeed -= .25f;

if (moveBySpeed <= 0)
   AdjustStoppedPosition();
}

private void AdjustStoppedPosition()
{
    adjusting = true;

    CCNode closest = null;
    float distance = float.MaxValue;

    for (int i = 0; i < ChildrenCount; i++)
    {
        var diff = Children[i].BoundingBox.MinY - bottom;

        if (diff > 0 && diff < distance)
        {
            distance = diff;
            closest = Children[i];
        }
    }

    for (int i = 0; i < ChildrenCount; i++)     
        Children[i].RunAction(new CCSequence(new CCMoveTo(.5f, new CCPoint(Children[i].PositionX, Children[i].BoundingBox.MinY - distance)), new CCCallFunc( () => {doneCount++;})));

}     

0 个答案:

没有答案