TimerEvent class

This page explains the TimerEvent class.

What class is this?

The TimerEvent class is the event class that a timer passes to a timer event handler function, such as the Timer class constructor or the timer_complete function.

Basic usage

Each timer event handler’s first argument becomes the TimerEvent class instance.

from typing_extensions import TypedDict

import apysc as ap


class _RectOptions(TypedDict):
    rectangle: ap.Rectangle


def on_timer(e: ap.TimerEvent, options: _RectOptions) -> None:
    """
    The handler that the timer calls.

    Parameters
    ----------
    e : ap.TimerEvent
        Event instance.
    options : dict
        Optional arguments dictionary.
    """
    rectangle: ap.Rectangle = options["rectangle"]
    rectangle.rotation_around_center += 1


def on_timer_complete(e: ap.TimerEvent, options: dict) -> None:
    """
    The handler that the timer calls when completed.

    Parameters
    ----------
    e : ap.TimerEvent
        Event instance.
    options : dict
        Optional arguments dictionary.
    """
    ap.trace("Timer complete!")


ap.Stage(
    stage_width=150,
    stage_height=150,
    background_color=ap.Color("#333"),
    stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#0af"))
rectangle: ap.Rectangle = sprite.graphics.draw_rect(x=50, y=50, width=50, height=50)

options: _RectOptions = {"rectangle": rectangle}
timer: ap.Timer = ap.Timer(handler=on_timer, delay=33.3, options=options)
timer.start()
timer.timer_complete(handler=on_timer_complete)

ap.save_overall_html(dest_dir_path="timer_event_basic_usage/")

this attribute

The TimerEvent instance’s this attribute becomes the target Timer instance, and you can use each timer instance interface from it.

from typing_extensions import TypedDict

import apysc as ap


class _RectOptions(TypedDict):
    rectangle: ap.Rectangle


def on_timer(e: ap.TimerEvent, options: _RectOptions) -> None:
    """
    The handler that the timer calls.

    Parameters
    ----------
    e : ap.TimerEvent
        Event instance.
    options : dict
        Optional arguments dictionary.
    """
    rectangle: ap.Rectangle = options["rectangle"]
    rectangle.rotation_around_center += 1
    ap.trace("Current timer count: ", e.this.current_count)


ap.Stage(
    stage_width=150,
    stage_height=150,
    background_color=ap.Color("#333"),
    stage_elem_id="stage",
)
sprite: ap.Sprite = ap.Sprite()
sprite.graphics.begin_fill(color=ap.Color("#0af"))
rectangle: ap.Rectangle = sprite.graphics.draw_rect(x=50, y=50, width=50, height=50)

options: _RectOptions = {"rectangle": rectangle}
timer: ap.Timer = ap.Timer(handler=on_timer, delay=16.6, options=options)
timer.start()

ap.save_overall_html(dest_dir_path="timer_event_this_attribute/")

TimerEvent constructor API

Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.

[Interface signature] __init__(self, *, this: 'timer.Timer') -> None


[Interface summary]

Timer event class.


[Parameters]

  • this: Timer

    • Target timer instance.


[Examples]

>>> from typing_extensions import TypedDict
>>> import apysc as ap
>>> class RectOptions(TypedDict):
...     rectangle: ap.Rectangle
...
>>> def on_timer(e: ap.TimerEvent, options: RectOptions) -> None:
...     rectangle: ap.Rectangle = options["rectangle"]
...     rectangle.x += 1
>>> stage: ap.Stage = ap.Stage()
>>> sprite: ap.Sprite = ap.Sprite()
>>> sprite.graphics.begin_fill(color=ap.Color("#0af"))
>>> rectangle: ap.Rectangle = sprite.graphics.draw_rect(
...     x=50, y=50, width=50, height=50
... )
>>> options: RectOptions = {"rectangle": rectangle}
>>> ap.Timer(
...     on_timer,
...     delay=ap.FPS.FPS_60,
...     options=options,
... ).start()

this attribute API

Note: the document build script generates and updates this API document section automatically. Maybe this section is duplicated compared with previous sections.

[Interface summary]

Get a timer instance of listening to this event.


[Returns]

  • this: TImer

    • Instance of listening to this event.


[Examples]

>>> from typing_extensions import TypedDict
>>> import apysc as ap
>>> class RectOptions(TypedDict):
...     rectangle: ap.Rectangle
...
>>> def on_timer(e: ap.TimerEvent, options: RectOptions) -> None:
...     rectangle: ap.Rectangle = options["rectangle"]
...     rectangle.x += 1
...     with ap.If(rectangle.x >= 100):
...         timer: ap.Timer = e.this
...         timer.stop()
>>> stage: ap.Stage = ap.Stage()
>>> sprite: ap.Sprite = ap.Sprite()
>>> sprite.graphics.begin_fill(color=ap.Color("#0af"))
>>> rectangle: ap.Rectangle = sprite.graphics.draw_rect(
...     x=50, y=50, width=50, height=50
... )
>>> options: RectOptions = {"rectangle": rectangle}
>>> ap.Timer(
...     on_timer,
...     delay=ap.FPS.FPS_60,
...     options=options,
... ).start()