如何使用NSTimer从10秒倒计时然后停止计时器?

时间:2015-11-28 18:53:26

标签: swift nstimer

这是我到目前为止所做的。

 private void updateShape(CubeComponent cc) {

    // Kill any existing shapes.
    if (cc.fixture != null && cc.fixture.getBody() != null) {
        cc.body.destroyFixture(cc.fixture);
        cc.fixture = null;
    }

    // don't even think about making a non-existing shape. It's almost as
    // bad as dividing by zero.
    if (cc.scale <= 0) {
        return;
    }

    // Make a fixture
    FixtureDef fdef = new FixtureDef();

    fdef.density = 0.1f;
    fdef.friction = 0.2f;
    fdef.restitution = 0.5f;

    ///////////////Most likely coming from here////////
    // Create the shape.
    PolygonShape shape = new PolygonShape();
    shape.set(new float[] {
            -cc.scale / 2f, -cc.scale / 2f, -cc.scale / 2f, cc.scale / 2f,
            cc.scale / 2f, -cc.scale / 2f, cc.scale / 2f, cc.scale / 2f
    });
    fdef.shape = shape;
    ///////////////Most likely coming from here////////

    // Create the fixture.
    cc.fixture = cc.body.createFixture(fdef);

    // dispose of the bad shape.
    shape.dispose();
}

1 个答案:

答案 0 :(得分:3)

将计时器定义为类中的var:

var timer = NSTimer()

在viewDidLoad中创建计时器:

timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("update"), userInfo: nil, repeats: true)

更新比每0.4秒调用一次(或多或少):

func update() {

if(count > 0)
{
    countLabel1.text = String(count--)
} else {
 timer.invalidate()
}}

编辑:[如果你想让每秒更新一次,而不是0.4当然。]