1

我正在 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++;})));

}     
4

0 回答 0