TM.Interval

View source code: https://github.com/a-mean-blogger/text-game-maker-js/blob/master/src/base-objects/interval.js

Type

Class (extends TM.IObject)

Purpose

A class that creates instances repeats a function with interval. The object that loops the function in TM.ILoopObject is also the instance of this class.

Constructor Parameters

new TM.Interval(speed, func)
  • speed: Interval time(unit: milliseconds)
  • func: The function that will be repeated

Important Properties

  • isActive: It will be set true after calling init function, false after calling inactivate. The Object status(active/inactive) can be checked by this property.
  • speedthis.func will be repeated with this value as the interval(unit: milliseconds) starting from init function
  •  func: The function that is repeated

Important Functions

  • init(): Starts looping this.func with this.speed as the interval(unit: milliseconds)
  • inactivate(): Stops looping this.func. the loop can be started again by calling init function
  • setSpeed(speed): Changes loop interval(unit: milliseconds)

Code Example

**View example source code: interval-tutorial.js

We will make MyWatch class that prints current time using TM.Interval class.

First, create an instance of TM.ScreenManager and assign to var TMS(Text Game Maker Screen).

var TMS = new TM.ScreenManager();

Create an instance of TM.Interval that creates current time and print on the screen  with 1000 milliseconds interval.

var myWatch = new TM.Interval(1000,function(){
  var date = new Date();
  TMS.insertTextAt(1,1,date.toLocaleTimeString());
})
myWatch.init()

Run Example

Type these commands into the browser console on this page to test.

//inactivate myWatch
myWatch.inactivate(); //reactivate(initialize) myWatch
myWatch.init(); //reset loop interval speed of myWatch myWatch.setSpeed(2000);

Comments

Add Comment

Login with SNS account to write comments. see details

UP