| Title: | An Interface to the 'Phaser.js' Game Framework |
|---|---|
| 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. |
| Authors: | Maciej Banas [aut, cre] |
| Maintainer: | Maciej Banas <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-06-08 11:13:12 UTC |
| Source: | https://github.com/maciekbanas/shinyphaser |
Create and manage groups of sprites in the Phaser scene. Created with PhaserGame$add_group() method.
new()
Create a dynamic group in the Phaser scene.
Group$new(name, session = shiny::getDefaultReactiveDomain())
nameCharacter. Unique name of the group.
sessionShiny session object.
add_animation()
Add an animation that can be used by members of this group.
Group$add_animation( suffix, url, frame_width, frame_height, frame_count, frame_rate )
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.
create()
Create one group member at a coordinate.
Group$create(x, y)
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
clone()
The objects of this class are cloneable with this method.
Group$clone(deep = FALSE)
deepWhether to make a deep clone.
Create and manage images in the Phaser scene. Created with PhaserGame$add_image() method.
new()
Add an image object to the Phaser scene.
Image$new( name, url, x, y, visible, clickable, session = getDefaultReactiveDomain() )
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.
show()
Show a previously added image.
Image$show()
hide()
Hide a previously added image.
Image$hide()
click()
Add a click event listener to the image that triggers an R function when clicked.
Image$click(event_fun, input)
event_funA function.
inputShiny input object.
clone()
The objects of this class are cloneable with this method.
Image$clone(deep = FALSE)
deepWhether to make a deep clone.
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.
idCharacter. ID of the Game container. Used as the HTML element ID where the game canvas will be rendered.
new()
Create a PhaserGame object with the given configuration.
PhaserGame$new(id = "phaser_game", width = 800, height = 600)
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).
A new PhaserGame object.
game <- PhaserGame$new(id = "my_game", width = 1024, height = 768)
set_shiny_session()
Set the Shiny session used to send Phaser custom messages.
PhaserGame$set_shiny_session(session = shiny::getDefaultReactiveDomain())
sessionShiny session object (default: shiny::getDefaultReactiveDomain()).
use_phaser()
Load dependencies and initialize the Phaser game in the UI.
PhaserGame$use_phaser()
HTML tag list containing dependencies and initialization script.
game$use_phaser()
add_text()
Add a text object to the Phaser scene.
PhaserGame$add_text(text, id, x, y, style = list(font_size = "22px"))
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.
add_rectangle()
Add a rectangle object to the Phaser scene.
PhaserGame$add_rectangle( name, x, y, width, height, color, visible = TRUE, clickable = FALSE )
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.
add_image()
Adds a static image to the Phaser scene.
PhaserGame$add_image(name, url, x, y, visible = TRUE, clickable = FALSE)
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).
add_map()
Add a background (tilemap) layer from Tiled JSON + tileset image(s).
PhaserGame$add_map(map_key, map_url, tileset_urls, tileset_names, layer_name)
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.
Invisible; sends a custom message to the client.
enable_terrain_collision()
Enable terrain collision for a player sprite.
PhaserGame$enable_terrain_collision(name)
nameCharacter. Name of the player sprite (as added via add_player_sprite).
add_sprite()
Load a base spritesheet and create an "idle" animation.
PhaserGame$add_sprite( name, url, x, y, frame_width, frame_height, frame_count = 1, frame_rate = 1 )
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.
add_group()
Adds a dynamic group from a spritesheet.
PhaserGame$add_group(name)
nameCharacter. Unique name of the group.
add_static_sprite()
Adds a static sprite to the scene (non-animated).
PhaserGame$add_static_sprite(name, url, x, y)
nameCharacter. Unique name of the sprite.
urlCharacter. URL or path to the image file.
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
add_static_group()
Adds a static group to the scene (non-animated).
PhaserGame$add_static_group(name, url)
nameCharacter. Unique name of the group.
urlCharacter. URL or path to the image file.
add_collider()
Adds a collider between two game objects.
PhaserGame$add_collider( object_one, object_two = NULL, group = NULL, callback_fun = NULL, input )
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.
add_overlap()
Adds a collider between two game objects.
PhaserGame$add_overlap( object_one, object_two = NULL, group = NULL, callback_fun, input )
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.
are_overlap()
Create a reactive expression for overlap state between two objects.
PhaserGame$are_overlap(object_one, object_two, input)
object_oneCharacter. Name of the first object.
object_twoCharacter. Name of the second object.
inputShiny input list.
add_overlap_end()
Register a callback fired when overlap between objects ends.
PhaserGame$add_overlap_end( object_one, object_two = NULL, group = NULL, callback_fun, input, session = shiny::getDefaultReactiveDomain() )
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.
add_control()
Register a callback fired when a specific key is pressed.
PhaserGame$add_control(key, action, input)
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.
clone()
The objects of this class are cloneable with this method.
PhaserGame$clone(deep = FALSE)
deepWhether to make a deep clone.
## ------------------------------------------------ ## Method `PhaserGame$new` ## ------------------------------------------------ game <- PhaserGame$new(id = "my_game", width = 1024, height = 768) ## ------------------------------------------------ ## Method `PhaserGame$use_phaser` ## ------------------------------------------------ game$use_phaser()## ------------------------------------------------ ## Method `PhaserGame$new` ## ------------------------------------------------ game <- PhaserGame$new(id = "my_game", width = 1024, height = 768) ## ------------------------------------------------ ## Method `PhaserGame$use_phaser` ## ------------------------------------------------ game$use_phaser()
Create and manage rectangles in the Phaser scene. Created with PhaserGame$add_rectangle() method.
new()
Add a rectangle object to the Phaser scene.
Rectangle$new( name, x, y, width, height, color, visible, clickable, session = getDefaultReactiveDomain() )
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.
show()
Show a previously added rectangle.
Rectangle$show()
hide()
Hide a previously added rectangle.
Rectangle$hide()
click()
Add a click event listener to the rectangle that triggers an R function when clicked.
Rectangle$click(event_fun, input)
event_funA function.
inputShiny input object.
clone()
The objects of this class are cloneable with this method.
Rectangle$clone(deep = FALSE)
deepWhether to make a deep clone.
Launches the sample Shiny application bundled with the package.
This is a quick way to see a working shinyphaser game setup.
run_sample_app()run_sample_app()
Create and manage animated sprites in the Phaser scene. Created with PhaserGame$add_sprite() method.
new()
Sprite$new( name, url, x, y, frame_width, frame_height, frame_count = NULL, frame_rate, session = getDefaultReactiveDomain() )
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.
add_animation()
Load a custom animation for any sprite previously added.
Sprite$add_animation( suffix, url, frame_width, frame_height, frame_count = NULL, frame_rate )
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.
Invisible; sends a custom message to the client.
play_animation()
Play a loaded animation for the sprite.
Sprite$play_animation(anim_name, duration = Inf)
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).
add_player_controls()
Enable movement controls (arrow keys) for a player sprite.
Sprite$add_player_controls(
directions = c("left", "right", "down", "up"),
speed = 200
)directionsCharacter vector. Directions to enable (defaults to c("left","right","down","up")).
speedNumeric. Movement speed in pixels/second (default: 200).
set_velocity_x()
Set the sprite's velocity in the x direction.
Sprite$set_velocity_x(x = 100)
xNumeric. Velocity in pixels/second (positive = right, negative = left).
set_velocity_y()
Set the sprite's velocity in the y direction.
Sprite$set_velocity_y(x = 100)
xNumeric. Velocity in pixels/second (positive = down, negative = up).
set_gravity()
Set the sprite's velocity in both x and y directions.
Sprite$set_gravity(x = 100, y = 100)
xNumeric. Velocity in pixels/second (positive = right, negative = left).
yNumeric. Velocity in pixels/second (positive = down, negative = up).
set_bounce()
Set the sprite's bounce factor.
Sprite$set_bounce(x)
xNumeric. Bounce factor.
destroy()
Remove sprite from the scene.
Sprite$destroy()
set_in_motion()
Move sprite along a vector for a set distance.
Sprite$set_in_motion(dir_x, dir_y, speed, distance, lag = distance/speed)
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).
clone()
The objects of this class are cloneable with this method.
Sprite$clone(deep = FALSE)
deepWhether to make a deep clone.
Create and manage groups of static sprites in the Phaser scene. Created with PhaserGame$add_static_group() method.
new()
Create a static group from a base image.
StaticGroup$new(name, url, session = shiny::getDefaultReactiveDomain())
nameCharacter. Unique name of the group.
urlCharacter. URL or path to image file.
sessionShiny session object.
create()
Create one static group member at a coordinate.
StaticGroup$create(x, y)
xNumeric. X-coordinate in pixels.
yNumeric. Y-coordinate in pixels.
disable()
Disable a static group member body based on overlap event payload.
StaticGroup$disable(evt)
evtList-like event payload containing x2 and y2 values.
clone()
The objects of this class are cloneable with this method.
StaticGroup$clone(deep = FALSE)
deepWhether to make a deep clone.
Create and manage non-animated sprites in the Phaser scene. Created with PhaserGame$add_static_sprite() method.
new()
Add a non-animated static sprite to the scene.
StaticSprite$new(name, url, x, y, session = getDefaultReactiveDomain())
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.
clone()
The objects of this class are cloneable with this method.
StaticSprite$clone(deep = FALSE)
deepWhether to make a deep clone.
R6 class to represent a text object in the Phaser scene, allowing dynamic updates to its content. Created with PhaserGame$add_text() method.
new()
Create a text object in the Phaser scene.
Text$new(text, id, x, y, style, session = shiny::getDefaultReactiveDomain())
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.
set()
Update the text content of this object.
Text$set(text)
textCharacter. New text value to display.
clone()
The objects of this class are cloneable with this method.
Text$clone(deep = FALSE)
deepWhether to make a deep clone.