Draw an Object to the Scene
render.object.RdCalls render.sprite() using an object's $spritename (the name of the desired sprite in RAM$ROM$sprites).
The object is drawn at position (obj$x, obj$y) on layer obj$layer if specified (see below).
Objects with no $spritename will not be drawn, unless they have custom drawing behavior defined in obj$draw():
If the object has a function set for obj$draw(), it will be run instead of render.sprite(). This allows the game dev to create custom drawing behavior.
Value
Returns the scene object with sprite drawn. This function is called by render.ram() for each object in RAM$objects.
Details
An object only needs a $spritename to be drawn. Objects can also have the following properties which influence how they're drawn: otherwise, the default value (second column) will be used.
$spritename | Name of sprite in ROM$sprites. | ||
$x | | 1 | X-coordinate at which to draw the object's sprite in the scene. |
$y | 1 | Y-coordinate. | |
$offset.x | 0 | Pixels to offset sprite horizontally from obj$x. | |
$offset.y | 0 | Vertical offset. | |
$layer | 2 | Layer on which to draw the sprite. See render.scene; high layers are drawn on top. | |
$timer | RAM$timer | Tick count (ascending) for animations. | |
$palette | NULL | Vector to swap the colors of the object's sprite around, e.g. c(0,2,1) swaps values of 2 and 1. Index starts at 0. Defaults to no swapping. | |
$draw() | NULL | Overwrites the default drawing behavior for the sprite; see above. |
Offsets
Both the obj and sprite can have optional offset.x/offset.y properties to offset the draw location of their sprite from their position. This can be useful for animation and aligning an object's sprite with its actual hitbox.
If both object and sprite have an offset property, the two are added.
Examples
smileysprite = matrix(c(0,0,1,0,0,0,0,1,1,1,0,1,0,0,0,1,1,1,0,1,0,0,0,1,0,0,1,0), ncol = 7)
RAM = ram.init(rom.init(16,8,sprites=list(smiley=smileysprite)))
RAM$objects$smiley = list(x = 3, y = 3, spritename = 'smiley')
scene = list(width=16,height=8)
scene = render.object(scene,RAM$objects$smiley,RAM)
render.scene(scene)
#>
#>
#> [] []
#> [] []
#> [] []
#> [][][][][]
#>
#>