Native Sprites
Native Sprites are a way to add platform-specific features to your game which aren't supported in Replay itself.
For example, Replay has no text input. If you need a text input, you can use the TextInput Native Sprite.
#
Create a Native SpriteTo create your own Native Sprite you can use the makeNativeSprite
function from @replay/core
:
- JavaScript
- TypeScript
We've registered our MyWidget
Native Sprite with a "MyWidget"
name. This name will be used later on to lookup the platform-specific implementation.
Important
All Native Sprites must define an id
prop.
You can then call your Native Sprite within other Custom or Mutable Sprites:
#
Platform implementationAfter creating a Native Sprite, its implementation must be defined separately for each platform you want to support.
Native Sprite implementations are an object with a create
, loop
and destroy
method.
create
#
Called on initial creation of Sprite. Use this to run anything you need on setup. Returns the initial state
.
#
Parametersprops
: The props passed in by the parent Sprite.parentGlobalId
: A globally unique ID for the parent Sprite.getState
: A function which returns the current state of the Sprite.utils
: An object with fields:didResize
: A boolean to check if the device was just resized.scale
: Ratio of the game and platform's screen size.gameXToPlatformX
: Function to convert a local gamex
coordinate to platformx
coordinate.gameYToPlatformY
: Function to convert a local gamey
coordinate to platformy
coordinate.size
: Device size, see Game Size.
loop
#
Called 60 times a second. Mutate state
directly to update it.
#
Parametersprops
: The props passed in by the parent Sprite.state
: The current state of the Sprite.parentGlobalId
: A globally unique ID for the parent Sprite.spriteToGameCoords(x, y, out)
: Convert a localx, y
coordinate to game coordinates. Mutatesx
andy
fields onout
to set the result.utils
: An object with fields:didResize
: A boolean to check if the device was just resized.scale
: Ratio of the game and platform's screen size.gameXToPlatformX
: Function to convert a local gamex
coordinate to platformx
coordinate.gameYToPlatformY
: Function to convert a local gamey
coordinate to platformy
coordinate.size
: Device size, see Game Size.isLastFrame
: A boolean indicating if this call ofloop
was the last one before rendering.
cleanup
#
Called when Sprite is removed. Use this to clean up anything related to the Sprite.
#
Parametersstate
: The current state of the Sprite.parentGlobalId
: A globally unique ID for the parent Sprite.
#
Web example- JavaScript
- TypeScript
#
Platform usageYou must import each Native Sprite implementation into its respective platform. See the nativeSpriteMap
field in the Web platform.
Important
For example: