IsInRadius (CMS)
From The Official Visionaire Studio: Adventure Game Engine Wiki
Name | Type | By |
---|---|---|
isInRadius(obj, rad) | Definition | AFRLme |
This small function allows you to quickly query if the mouse cursor is currently inside of a specified radius based on current position of a character, or center of an object. Visionaire Studio 4.1 is required for this function to work.
Instructions
1. Add the main script to the Visionaire Studio Script Editor & set the script as a definition script.
2a. To query if cursor is inside of a specified radius based on a characters position, create an execute a script containing...
isInRadius(Characters["character_name"], 50) -- this queries if cursor is inside of a radius of 50 pixels from characters current position
2b. To query if cursor is inside of a specified radius based on the center of an objects sprite (image), create an execute a script containing...
isInRadius(Objects["object_name"], 100) -- this queries if cursor is inside of a radius of 100 pixels from objects center (center of the image)
3. Here is a quick example of how to use this function...
if isInRadius(Characters["Tom"], 50) then
-- do something
else
-- do something else
end
Main Script
local x, y -- empty variables which the objects position or center will be stored in
local pos = {} -- empty table which we will store the mouse cursor positions in
-- check if cursor is in radius (circle)
function isInRadius(obj, rad)
-- + cursor position + --
pos.x = game.ScrollPosition.x + getCursorPos().x; pos.y = game.ScrollPosition.y + getCursorPos().y
-- + object position & radius + --
if obj:getId().tableId == eCharacters then
x = obj.Position.x; y = obj.Position.y
rad = math.floor(obj.Size * rad) / 100
elseif obj:getId().tableId == eObjects then
x = obj.ObjectSprite.SpriteSprite:getPosition().x + (obj.ObjectSprite.SpriteSprite:getSize().x / 2)
y = obj.ObjectSprite.SpriteSprite:getPosition().y + (obj.ObjectSprite.SpriteSprite:getSize().y / 2)
rad = math.floor(obj.Scale * rad)
end
-- + calculate distance from circle center + --
if x > pos.x then x = math.floor(x - pos.x) else x = math.floor(pos.x - x) end
if y > pos.y then y = math.floor(y - pos.y) else y = math.floor(pos.y - y) end
if math.floor( math.pow(x, 2) + math.pow(y, 2) ) <= math.pow(rad, 2) then return true end
return false -- if not in radius then it's false
end
Syntax Breakdown
Name | Type | Description |
---|---|---|
obj | link | This should be a link to a scene object or a character. The function automatically detects if obj type is a character or object. |
rad | integer | This should be an integer (number) value containing the radius value of the circle. Radius = circle diameter ÷ 2. |