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 Sprite#
To 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 implementation#
After 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.
Parameters#
props: 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 gamexcoordinate to platformxcoordinate.gameYToPlatformY: Function to convert a local gameycoordinate to platformycoordinate.size: Device size, see Game Size.
loop#
Called 60 times a second. Mutate state directly to update it.
Parameters#
props: 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, ycoordinate to game coordinates. Mutatesxandyfields onoutto 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 gamexcoordinate to platformxcoordinate.gameYToPlatformY: Function to convert a local gameycoordinate to platformycoordinate.size: Device size, see Game Size.isLastFrame: A boolean indicating if this call ofloopwas the last one before rendering.
cleanup#
Called when Sprite is removed. Use this to clean up anything related to the Sprite.
Parameters#
state: The current state of the Sprite.parentGlobalId: A globally unique ID for the parent Sprite.
Web example#
- JavaScript
- TypeScript
Platform usage#
You must import each Native Sprite implementation into its respective platform. See the nativeSpriteMap field in the Web platform.
Important
For example: