made a mistake when applying the library tween to all objects [SOLVED]

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
vsd
Prole
Posts: 2
Joined: Wed Jan 08, 2025 4:55 am

made a mistake when applying the library tween to all objects [SOLVED]

Post by vsd »

Hi, I'm new to using love2d, I made this project to practice understanding how love2d works.
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:
problem.png
problem.png (50.99 KiB) Viewed 254 times

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
2.Load, Update, and Draw

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
All answers are welcome ^^
Last edited by vsd on Thu Jan 09, 2025 9:03 am, edited 1 time in total.
MrFariator
Party member
Posts: 563
Joined: Wed Oct 05, 2016 11:53 am

Re: made a mistake when applying the library tween to all objects

Post by MrFariator »

You have a typo in your love.update. Notice the casing:

Code: Select all

-- "object" is all lowercase
for i,object in ipairs(AllObject) do
 -- ...inside the loop, when creating a tween, you've written "Object" instead
 object.Motion = tween.new(0.5, Object, {ScaleX = 1.5, ScaleY = 1.5}, 'outBounce')
Same goes for the other section in the same loop that calls tween.new(), too.

If tween.lua raises this particular error with new(), check that the second parameter is a non-nil value.
vsd
Prole
Posts: 2
Joined: Wed Jan 08, 2025 4:55 am

Re: made a mistake when applying the library tween to all objects

Post by vsd »

MrFariator wrote: Wed Jan 08, 2025 1:32 pm You have a typo in your love.update. Notice the casing:

Code: Select all

-- "object" is all lowercase
for i,object in ipairs(AllObject) do
 -- ...inside the loop, when creating a tween, you've written "Object" instead
 object.Motion = tween.new(0.5, Object, {ScaleX = 1.5, ScaleY = 1.5}, 'outBounce')
Same goes for the other section in the same loop that calls tween.new(), too.

If tween.lua raises this particular error with new(), check that the second parameter is a non-nil value.
I get it, when I was offline I tried some proper and efficient ways, and I got it. :D

Library:

Code: Select all

local tween = require "tween"
local love = require("love")
Variabel and function:

Code: Select all

local objects = {}
local tweens = {}

local function createObject(img,x, y)
    img = love.graphics.newImage(img)
    return {
        img = img,
        x = x,
        y = y,
        width = img:getWidth(),
        height = img:getHeight(),
        scalex = 1,
        scaley = 1,
        targetSize = 100,
    }
end

local function isMouseOver(obj, mouseX, mouseY)
    local a_right = obj.x + obj.width/2
    local a_left = obj.x - obj.width/2
    local a_top = obj.y - obj.height/2
    local a_down = obj.y + obj.height/2
    return a_right > mouseX and a_left < mouseX and a_top < mouseY and a_down > mouseY
end
Load, Update, and Draw

Code: Select all

function love.load()
    local w_widht = love.graphics.getWidth()
    local w_height = love.graphics.getHeight()

    local many_object = 10
    for i = 1, many_object do
        local x = (w_widht/(many_object+2))*(i+0.5)
        local y = w_height/2
        table.insert(objects, createObject("asset/line.png",x, y))
    end
end

function love.update(dt)
    local mouseX, mouseY = love.mouse.getPosition()

    for i, obj in ipairs(objects) do
        if isMouseOver(obj, mouseX, mouseY) then
            if obj.targetSize ~= 150 then
                obj.targetSize = 150
                tweens[i] = tween.new(0.5, obj, { scalex = 1.5, scaley = 1.5 }, "outBounce")
            end
        else
            if obj.targetSize ~= 100 then
                obj.targetSize = 100
                tweens[i] = tween.new(0.5, obj, { scalex = 1, scaley = 1 }, "outBounce")
            end
        end
        if tweens[i] then
            tweens[i]:update(dt)
        end
    end
end

function love.draw()
    for _, obj in ipairs(objects) do
        love.graphics.draw(obj.img,obj.x,obj.y,0,obj.scalex,obj.scalex,obj.width/2,obj.height/2)
    end
end
thanks for the comment ^^
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests