The project I made this time involves a tween library that is used to resize the selected object by touching the collision between the object and the mouse.
I'm still new to the library and need some documentation and sample projects.
This is the error result when the mouse touches the collision of the object:
And this is the code:
1.function and library:
Code: Select all
local love = require("love")
local tween = require("tween")
function CreateObject(image,location_x,location_y)
local object = {}
object.Image = love.graphics.newImage(image)
object.LocationX = location_x
object.LocationY = location_y
object.ScaleX = 1
object.ScaleY = 1
object.Widht = object.Image:getWidth()
object.Height = object.Image:getHeight()
object.CollisionIsTrue = true
object.CollisionIsFalse = false
object.Motion = nil
table.insert(AllObject,object)
end
function love.keypressed(key)
love.graphics.print(key,25,50)
if key == "escape" then
love.event.quit()
end
if key == "f" then
if love.window.getFullscreen() == true then
love.window.setFullscreen(false,"desktop")
else
love.window.setFullscreen(true,"desktop")
end
end
end
function CheckCollisionMouse(object,mouse)
local a_right = object.LocationX + object.Widht/2
local a_left = object.LocationX - object.Widht/2
local a_top = object.LocationY - object.Height/2
local a_down = object.LocationY + object.Height/2
local b_right = mouse.LocationX
local b_left = mouse.LocationX
local b_top = mouse.LocationY
local b_down = mouse.LocationY
if a_right > b_left and a_left < b_right and a_top < b_down and a_down > b_top then
return true
else
return false
end
end
Code: Select all
function love.load()
Mouse = {}
Mouse.LocationX , Mouse.LocationY = love.mouse.getPosition()
Check = false
local getscreenwidth= love.graphics.getWidth()
local getscreenheight= love.graphics.getHeight()
AllObject = {}
for i = 1, 5 do
CreateObject("asset/line.png",(getscreenwidth/7)*(i+0.5),getscreenheight/2)
end
for idx,object in ipairs(AllObject) do
object.Motion = tween.new(1, object, {ScaleX = 1, ScaleY = 1}, 'inBounce')
end
end
function love.update(dt)
Mouse.LocationX , Mouse.LocationY = love.mouse.getPosition()
for i,object in ipairs(AllObject) do
if CheckCollisionMouse(object,Mouse) then
if object.CollisionIsTrue then
object.Motion = tween.new(0.5, Object, {ScaleX = 1.5, ScaleY = 1.5}, 'outBounce')
object.CollisionIsTrue = false
object.CollisionIsFalse = true
end
else
if object.CollisionIsFalse then
object.Motion = tween.new(0.5, Object, {ScaleX = 1, ScaleY = 1}, 'outBounce')
object.CollisionIsFalse = false
object.CollisionIsTrue = true
end
end
end
for index,object in ipairs(AllObject) do
object.Motion:update(dt)
end
end
function love.draw()
for idx,object in ipairs(AllObject) do
love.graphics.draw(object.Image,object.LocationX,object.LocationY,0,object.ScaleX,object.ScaleY,object.Widht/2,object.Height/2)
end
end