Click.lua

From Moondust Wiki
Jump to navigation Jump to search

Download click.lua v1.2 BETA

A small library for the cursor. You can find its coordinates in the scene, find the speed's cursor, or set an image cursor.

Installation

Place the file click.lua in either your level's custom graphics folder for use in a level, along with a lunadll.lua file, or in the same directory as your .wld file along with a lunaworld.lua file for use throughout an entire episode.

How to Use

To enable the click.lua, add this line to lunadll.lua:

local click = API.load("click")

This will load the API.

Example

local click = API.load("click")
click.loadCursor({
  {Graphics.loadImage("click.png")}
  {{Graphics.loadImage("imageA.png"), Graphics.loadImage("imageB.png")}, 5, 0, 0, 10}
})
click.setCursorID(1)

function onTick()
  if click.click then 
    click.setCursorID(2)
  elseif click.released then
    click.setCursorID(1)
  end
  
  if click.box{x = player.x, y = player.y, width = player.width, height = player.height, scene = true} and click.click then
    player:kill()
  end
end

Documentation

Cursor Values

Name Description
click Returns true for the first frame the mouse was clicked.
hold Returns true if the mouse is being held.
released Returns true for the first frame the mouse was released.
state Returns the state of the cursor. Compatible with KEYS_UP, KEYS_DOWN, KEYS_PRESS, and KEYS_UNPRESSED. [Only for SMBX2 Beta 4 or higher]
x Returns the X-coordinate relative to the screen.
y Returns the Y-coordinate relative to the screen.
sceneX Returns the X-coordinate relative to the scene. Will return nil if the cursor is not inside any of the cameras.
sceneY Returns the Y-coordinate relative to the scene. Will return nil if the cursor is not inside any of the cameras.
speedX Returns the X-speed of the cursor.
speedY Returns the Y-speed of the cursor.
defaultImg The image that should be used when an image is not passed in loadCursor(). By default the image is hardcoded-42-2.png.

dragBox is a table containing information about the box created when the cursor is dragged.

Value Description
active If the mouse is being dragged. The next values won't update if this is false.
startX The starting X position of the box.
startY The starting Y position of the box.
diagonal The distance between the cursor and the starting point.
width The width of the box.
height The height of the box.
top The y coordinate of the top side of the box.
bottom The y coordinate of the bottom side of the box.
left The x coordinate of the left side of the box.
right The x coordinate of the right side of the box.


Cursor Functions

Function Description
loadCursor(table) A table containing all different cursors that will be used. You may also pass nothing and it will create a default table for you. Each cursor is defined in a table. The values for each table is:
  • The image of the cursor, or a table of images if the cursor is animated. If no image is used it will use defaultImg instead.
  • Priority of the cursor
  • X offset
  • Y offset
  • Animation Speed (Only use if the cursor is animated)
setCursorID(index) Set the cursor image using the index used in loadCursor(). Use 0 to disable drawing the cursor.
getCursorID() Will return the current index of the cursor that is currently drawn.


Misc. Functions

Function Description
box{} Will return true if the cursor is inside the specified box.
  • Required Parameters
    • x The X-coordinate left side of the box.
    • y The Y-coordinate top side of the box.
    • width The width of the box.
    • height The height of the box.
  • Optional Parameters
    • scene Set to true to use scene coordinates. [Default: False]
    • useXY2 Will instead use the parameters 'X2' 'Y2' to represent the bottom and right side of the box intead of using width and height. [Default: False]
circle{} Will return true if the cursor is inside the specified circle.
  • Required Parameters
    • x The X-coordinate center of the circle.
    • y The Y-coordinate center of the circle.
    • radius The radius of the circle.
  • Optional Parameters
    • scene Set to true to use scene coordinates. [Default: False]