睡眠阻止整个程序(Smalltalk Squeak)

时间:2016-05-10 07:49:01

标签: smalltalk squeak

我正在用gui制造一个N * N皇后问题。 我希望gui在每个女王的每次移动中停止x秒,问题是,程序只是将所有等待堆叠起来然后以速度运行所有内容。 我在这里给出了代码:http://pastebin.com/s2VT0E49

编辑: 这是我的工作区:

board := MyBoard new initializeWithStart: 8.
Transcript show:'something'.
3 seconds asDelay wait.
board solve.
3 seconds asDelay wait.
board closeBoard.

这是我希望等待发生的地方

canAttack: testRow x: testColumn
    | columnDifference  squareMark |
    columnDifference := testColumn - column.
    ((row = testRow
        or: [row + columnDifference = testRow])
        or: [row - columnDifference = testRow]) ifTrue: [
            squareDraw := squareDraw
            color: Color red.
            0.2 seconds asDelay wait.           
            ^ true ].

  squareDraw := squareDraw color: Color black.
  ^ neighbor canAttack: testRow x: testColumn

3 个答案:

答案 0 :(得分:5)

由于您使用的是Morphic,因此您应该使用步进来制作动画,而不是处理或延迟。在Morph中实施step方法。这将自动重复执行。同时实现stepTime以毫秒为单位回答间隔,例如:每4秒4000

在步骤方法中,计算新状态。如果每个女王被建模为一个单独的变形,你只需移动位置,那么Morphic将负责更新屏幕。如果您拥有自己的drawOn:方法,请在self changed方法中调用step,以便Morphic稍后调用您的绘图代码。

请参阅本教程:http://static.squeak.org/tutorials/morphic-tutorial-1.html

答案 1 :(得分:4)

您要暂停的进程是您的程序运行的进程。此过程也恰好是UI进程。因此,当您暂停程序时,您还会暂停UI,因此UI元素永远不会有机会自行更新。尝试在单独的过程中运行您的程序:

[ MyProgram run ] forkAt: Processor userBackgroundPriority.

请注意,UI进程通常以优先级40运行。#userBackgroundPriority为30.这可确保您无法锁定UI。

答案 2 :(得分:1)

要使工作区代码正常工作,请在延迟之前插入:

World doOneCycle.

这将导致Morphic世界被重新显示。

请注意,这是一种快速且非常脏的黑客攻击,并且正确的方法(请参阅我的其他答案)。延迟会阻止整个UI过程,而Morphic的重点在于您可以在代码执行时同时执行许多操作。