| Title: | An Interface to the 'Phaser.js' Game Framework |
| Version: | 0.1.0 |
| Description: | An API to build and control 2D games using the 'Phaser' 'JavaScript' engine. It enables integration with 'shiny' applications, allowing to create interactive games and simulations. |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| RoxygenNote: | 7.2.0 |
| URL: | https://github.com/maciekbanas/shinyphaser |
| BugReports: | https://github.com/maciekbanas/shinyphaser/issues |
| Imports: | htmltools, R6, rlang, shiny |
| Suggests: | knitr, rmarkdown, shinyalert, testthat (≥ 3.0.0), shinytest2 (≥ 0.5.1) |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| NeedsCompilation: | no |
| Packaged: | 2026-06-01 14:40:01 UTC; banas |
| Author: | Maciej Banas [aut, cre] |
| Maintainer: | Maciej Banas <banasmaciek@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-06-07 19:00:02 UTC |
Group
Description
Create and manage groups of sprites in the Phaser scene. Created with PhaserGame$add_group() method.
Methods
Public methods
Method new()
Create a dynamic group in the Phaser scene.
Usage
Group$new(name, session = shiny::getDefaultReactiveDomain())
Arguments
nameCharacter. Unique name of the group.
sessionShiny session object.
Method add_animation()
Add an animation that can be used by members of this group.
Usage
Group$add_animation( suffix, url, frame_width, frame_height, frame_count, frame_rate )
Arguments
suffixCharacter. Animation suffix/key.
urlCharacter. URL or path to spritesheet.
frame_widthNumeric. Width of each frame.
frame_heightNumeric. Height of each frame.
frame_countNumeric. Number of frames.
frame_rateNumeric. Frames per second.
Method create()
Create one group member at a coordinate.
Usage
Group$create(x, y)
Arguments
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
Method clone()
The objects of this class are cloneable with this method.
Usage
Group$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Image
Description
Create and manage images in the Phaser scene. Created with PhaserGame$add_image() method.
Methods
Public methods
Method new()
Add an image object to the Phaser scene.
Usage
Image$new( name, url, x, y, visible, clickable, session = getDefaultReactiveDomain() )
Arguments
nameCharacter. Unique name of the image.
urlCharacter. URL or path to image file.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
visibleLogical. Whether image is initially visible.
clickableLogical. Whether image emits click events.
sessionShiny session object.
Method show()
Show a previously added image.
Usage
Image$show()
Method hide()
Hide a previously added image.
Usage
Image$hide()
Method click()
Add a click event listener to the image that triggers an R function when clicked.
Usage
Image$click(event_fun, input)
Arguments
event_funA function.
inputShiny input object.
Method clone()
The objects of this class are cloneable with this method.
Usage
Image$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
PhaserGame
Description
R6 class to create and manage a Phaser game within a Shiny application. Provides methods for adding sprites, animations, images, backgrounds, controls, and collision handling.
Public fields
idCharacter. ID of the Game container. Used as the HTML element ID where the game canvas will be rendered.
Methods
Public methods
Method new()
Create a PhaserGame object with the given configuration.
Usage
PhaserGame$new(id = "phaser_game", width = 800, height = 600)
Arguments
idCharacter. ID of the Game container (defaults to "phaser_game").
widthNumeric. Width of the Phaser canvas in pixels (defaults to 800).
heightNumeric. Height of the Phaser canvas in pixels (defaults to 600).
Returns
A new PhaserGame object.
Examples
game <- PhaserGame$new(id = "my_game", width = 1024, height = 768)
Method set_shiny_session()
Set the Shiny session used to send Phaser custom messages.
Usage
PhaserGame$set_shiny_session(session = shiny::getDefaultReactiveDomain())
Arguments
sessionShiny session object (default: shiny::getDefaultReactiveDomain()).
Method use_phaser()
Load dependencies and initialize the Phaser game in the UI.
Usage
PhaserGame$use_phaser()
Returns
HTML tag list containing dependencies and initialization script.
Examples
game$use_phaser()
Method add_text()
Add a text object to the Phaser scene.
Usage
PhaserGame$add_text(text, id, x, y, style = list(font_size = "22px"))
Arguments
textCharacter. Text value to display.
idCharacter. Unique ID for the text object.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
styleNamed list. Styling options passed to Phaser text rendering.
Method add_rectangle()
Add a rectangle object to the Phaser scene.
Usage
PhaserGame$add_rectangle( name, x, y, width, height, color, visible = TRUE, clickable = FALSE )
Arguments
nameCharacter. Unique name for the rectangle.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
widthNumeric. Rectangle width in pixels.
heightNumeric. Rectangle height in pixels.
colorCharacter. Fill color in Phaser-compatible format.
visibleLogical. Whether rectangle is initially visible.
clickableLogical. Whether rectangle emits click events.
Method add_image()
Adds a static image to the Phaser scene.
Usage
PhaserGame$add_image(name, url, x, y, visible = TRUE, clickable = FALSE)
Arguments
nameCharacter. Unique key to reference this image.
urlCharacter. URL or path to the image file.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
visibleLogical. Whether the image is initially visible (default: TRUE).
clickableLogical. Whether the image should emit click events (default: FALSE).
Method add_map()
Add a background (tilemap) layer from Tiled JSON + tileset image(s).
Usage
PhaserGame$add_map(map_key, map_url, tileset_urls, tileset_names, layer_name)
Arguments
map_keyCharacter. Key for the tilemap JSON.
map_urlCharacter. URL of the Tiled JSON file (relative to www/assets/).
tileset_urlsCharacter vector. URLs of tileset image files.
tileset_namesCharacter vector. Names of tilesets as defined in Tiled.
layer_nameCharacter. Name of the layer to render from Tiled.
Returns
Invisible; sends a custom message to the client.
Method enable_terrain_collision()
Enable terrain collision for a player sprite.
Usage
PhaserGame$enable_terrain_collision(name)
Arguments
nameCharacter. Name of the player sprite (as added via add_player_sprite).
Method add_sprite()
Load a base spritesheet and create an "idle" animation.
Usage
PhaserGame$add_sprite( name, url, x, y, frame_width, frame_height, frame_count = 1, frame_rate = 1 )
Arguments
nameCharacter. Unique key for the sprite and its idle animation.
urlCharacter. URL or path to the spritesheet image.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
frame_widthNumeric. Width of each frame.
frame_heightNumeric. Height of each frame.
frame_countNumeric. Number of frames in the spritesheet.
frame_rateNumeric. Frames per second for the idle animation.
Method add_group()
Adds a dynamic group from a spritesheet.
Usage
PhaserGame$add_group(name)
Arguments
nameCharacter. Unique name of the group.
Method add_static_sprite()
Adds a static sprite to the scene (non-animated).
Usage
PhaserGame$add_static_sprite(name, url, x, y)
Arguments
nameCharacter. Unique name of the sprite.
urlCharacter. URL or path to the image file.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
Method add_static_group()
Adds a static group to the scene (non-animated).
Usage
PhaserGame$add_static_group(name, url)
Arguments
nameCharacter. Unique name of the group.
urlCharacter. URL or path to the image file.
Method add_collider()
Adds a collider between two game objects.
Usage
PhaserGame$add_collider( object_one, object_two = NULL, group = NULL, callback_fun = NULL, input )
Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
groupCharacter. Name of the group to compare against.
callback_funA function to be run when collision occurs.
inputShiny input list.
Method add_overlap()
Adds a collider between two game objects.
Usage
PhaserGame$add_overlap( object_one, object_two = NULL, group = NULL, callback_fun, input )
Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
groupCharacter. Name of the group.
callback_funA function to be run when overlap occurs.
inputShiny input list.
Method are_overlap()
Create a reactive expression for overlap state between two objects.
Usage
PhaserGame$are_overlap(object_one, object_two, input)
Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
inputShiny input list.
Method add_overlap_end()
Register a callback fired when overlap between objects ends.
Usage
PhaserGame$add_overlap_end( object_one, object_two = NULL, group = NULL, callback_fun, input, session = shiny::getDefaultReactiveDomain() )
Arguments
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
groupCharacter. Name of the group to compare against.
callback_funFunction. Callback executed when overlap ends.
inputShiny input list.
sessionShiny session object.
Method add_control()
Register a callback fired when a specific key is pressed.
Usage
PhaserGame$add_control(key, action, input)
Arguments
keyA character, accepts Javascript key events (they need to align with event.code).
actionA function to be run after key is pressed.
inputShiny input list.
Method clone()
The objects of this class are cloneable with this method.
Usage
PhaserGame$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
## ------------------------------------------------
## Method `PhaserGame$new`
## ------------------------------------------------
game <- PhaserGame$new(id = "my_game", width = 1024, height = 768)
## ------------------------------------------------
## Method `PhaserGame$use_phaser`
## ------------------------------------------------
game$use_phaser()
Rectangle
Description
Create and manage rectangles in the Phaser scene. Created with PhaserGame$add_rectangle() method.
Methods
Public methods
Method new()
Add a rectangle object to the Phaser scene.
Usage
Rectangle$new( name, x, y, width, height, color, visible, clickable, session = getDefaultReactiveDomain() )
Arguments
nameCharacter. Unique name of the rectangle.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
widthNumeric. Rectangle width in pixels.
heightNumeric. Rectangle height in pixels.
colorCharacter. Fill color in Phaser-compatible format.
visibleLogical. Whether rectangle is initially visible.
clickableLogical. Whether rectangle emits click events.
sessionShiny session object.
Method show()
Show a previously added rectangle.
Usage
Rectangle$show()
Method hide()
Hide a previously added rectangle.
Usage
Rectangle$hide()
Method click()
Add a click event listener to the rectangle that triggers an R function when clicked.
Usage
Rectangle$click(event_fun, input)
Arguments
event_funA function.
inputShiny input object.
Method clone()
The objects of this class are cloneable with this method.
Usage
Rectangle$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Sprite
Description
Create and manage animated sprites in the Phaser scene. Created with PhaserGame$add_sprite() method.
Methods
Public methods
Method new()
Usage
Sprite$new( name, url, x, y, frame_width, frame_height, frame_count = NULL, frame_rate, session = getDefaultReactiveDomain() )
Arguments
nameCharacter. Unique key for the sprite and its idle animation.
urlCharacter. URL or path to the spritesheet image.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
frame_widthNumeric. Width of each frame.
frame_heightNumeric. Height of each frame.
frame_countNumeric. Number of frames in the spritesheet. If NULL, auto-detect from spritesheet dimensions.
frame_rateNumeric. Frames per second for the idle animation.
sessionShiny session object.
Method add_animation()
Load a custom animation for any sprite previously added.
Usage
Sprite$add_animation( suffix, url, frame_width, frame_height, frame_count = NULL, frame_rate )
Arguments
suffixCharacter. Identifier for this animation (e.g. "move_left").
urlCharacter. URL or path to the spritesheet.
frame_widthNumeric. Width of each frame.
frame_heightNumeric. Height of each frame.
frame_countNumeric. Number of frames in the spritesheet. If NULL, auto-detect from spritesheet dimensions.
frame_rateNumeric. Frames per second for playback.
Returns
Invisible; sends a custom message to the client.
Method play_animation()
Play a loaded animation for the sprite.
Usage
Sprite$play_animation(anim_name, duration = Inf)
Arguments
anim_nameCharacter. Identifier for the animation to play (e.g. " move_left").
durationNumeric. Optional duration in milliseconds to play the animation before reverting to idle (defaults to Inf, which loops indefinitely until another animation is played).
Method add_player_controls()
Enable movement controls (arrow keys) for a player sprite.
Usage
Sprite$add_player_controls(
directions = c("left", "right", "down", "up"),
speed = 200
)Arguments
directionsCharacter vector. Directions to enable (defaults to c("left","right","down","up")).
speedNumeric. Movement speed in pixels/second (default: 200).
Method set_velocity_x()
Set the sprite's velocity in the x direction.
Usage
Sprite$set_velocity_x(x = 100)
Arguments
xNumeric. Velocity in pixels/second (positive = right, negative = left).
Method set_velocity_y()
Set the sprite's velocity in the y direction.
Usage
Sprite$set_velocity_y(x = 100)
Arguments
xNumeric. Velocity in pixels/second (positive = down, negative = up).
Method set_gravity()
Set the sprite's velocity in both x and y directions.
Usage
Sprite$set_gravity(x = 100, y = 100)
Arguments
xNumeric. Velocity in pixels/second (positive = right, negative = left).
yNumeric. Velocity in pixels/second (positive = down, negative = up).
Method set_bounce()
Set the sprite's bounce factor.
Usage
Sprite$set_bounce(x)
Arguments
xNumeric. Bounce factor.
Method destroy()
Remove sprite from the scene.
Usage
Sprite$destroy()
Method set_in_motion()
Move sprite along a vector for a set distance.
Usage
Sprite$set_in_motion(dir_x, dir_y, speed, distance, lag = distance/speed)
Arguments
dir_xNumeric. Horizontal direction (-1 = left, +1 = right, 0 = none).
dir_yNumeric. Vertical direction (-1 = up, +1 = down, 0 = none).
speedNumeric. Speed in pixels/second.
distanceNumeric. Distance in pixels to travel before stopping.
lagNumeric. Optional delay before sending the command (defaults to distance/speed).
Method clone()
The objects of this class are cloneable with this method.
Usage
Sprite$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Static Group
Description
Create and manage groups of static sprites in the Phaser scene. Created with PhaserGame$add_static_group() method.
Methods
Public methods
Method new()
Create a static group from a base image.
Usage
StaticGroup$new(name, url, session = shiny::getDefaultReactiveDomain())
Arguments
nameCharacter. Unique name of the group.
urlCharacter. URL or path to image file.
sessionShiny session object.
Method create()
Create one static group member at a coordinate.
Usage
StaticGroup$create(x, y)
Arguments
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
Method disable()
Disable a static group member body based on overlap event payload.
Usage
StaticGroup$disable(evt)
Arguments
evtList-like event payload containing x2 and y2 values.
Method clone()
The objects of this class are cloneable with this method.
Usage
StaticGroup$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Static Sprite
Description
Create and manage non-animated sprites in the Phaser scene. Created with PhaserGame$add_static_sprite() method.
Methods
Public methods
Method new()
Add a non-animated static sprite to the scene.
Usage
StaticSprite$new(name, url, x, y, session = getDefaultReactiveDomain())
Arguments
nameCharacter. Unique name of the sprite.
urlCharacter. URL or path to image file.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
sessionShiny session object.
Method clone()
The objects of this class are cloneable with this method.
Usage
StaticSprite$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Text
Description
R6 class to represent a text object in the Phaser scene, allowing dynamic updates to its content. Created with PhaserGame$add_text() method.
Methods
Public methods
Method new()
Create a text object in the Phaser scene.
Usage
Text$new(text, id, x, y, style, session = shiny::getDefaultReactiveDomain())
Arguments
textCharacter. Text value to display.
idCharacter. Unique ID for the text object.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
styleNamed list. Styling options passed to Phaser text rendering.
sessionShiny session object.
Method set()
Update the text content of this object.
Usage
Text$set(text)
Arguments
textCharacter. New text value to display.
Method clone()
The objects of this class are cloneable with this method.
Usage
Text$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Run the packaged shinyphaser sample app
Description
Launches the sample Shiny application bundled with the package.
This is a quick way to see a working shinyphaser game setup.
Usage
run_sample_app()