Skip to content
UnbelievableFlavour edited this page Aug 22, 2022 · 6 revisions

An entity in Cotton is an interactable sprite that you can write custom code for.

Init function

Usually you would want to setup some stuff when initialising the Entity. Here is an example of a basic Floppy, initialised with a collision type "overlap", and an image.

class("Floppy", {}).extends(EntityTemplate)

function Floppy:init(ldtk_entity)
    -- Calls the default initialization functions.
    Floppy.super.init(self, ldtk_entity)

    -- Set the collision to overlap.
    self:setCollisionType(collisionTypes.overlap)

    -- Set the image to a floppy.
    self:setImage("images/floppy")
end

Setting collision type

Cotton supports all Playdate SDK collision types. By default an entity will collide with the player by freeze.

Collision type can be set like so:

self:setCollisionType(collisionTypes.overlap)

Possible options:

collisionTypes.freeze
collisionTypes.slide
collisionTypes.overlap
collisionTypes.bounce

Setting an image

The image can be set like so:

self:setImage("images/floppy")

Setting an animated image

An animated image can be set like so:

self:setImage("images/floppy", { animated = true, delay = 500, loop = true })

Removing an entity

An entity can be removed like so:

self:remove()