Using variables from another file?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
arr5454
Prole
Posts: 5
Joined: Tue Oct 20, 2015 9:09 pm

Using variables from another file?

Post by arr5454 »

Hey all,

I am trying to make a Breakout clone involving an entity system, however I can't figure out how to check for collision between the ball and the paddle. I currently have a require statement at the top of ball.lua and the lines of code that should cause the ball to bounce off of the paddle commented out. When I run this, it causes the ball to spawn at the correct location (x and y values passed in the ents.Create() function in main.lua) but the ball does not bounce off the paddle.

When the require statement and the lines are uncommented and run, the ball will only spawn in the top right corner of the screen and will collide with what looks like an invisible "paddle" at the position where the x and y coordinates are passed into ents.Create() which should be the spawn position of the ball.

How would I be able to get the x and y position of the paddle from the paddle.lua file to check for collisions in the ball.lua file without using global variables?
Attachments
breakout.love
(3.26 KiB) Downloaded 83 times
User avatar
alberto_lara
Party member
Posts: 372
Joined: Wed Oct 30, 2013 8:59 pm

Re: Using variables from another file?

Post by alberto_lara »

You can access the bounds of the paddle just like this:

Code: Select all

function love.load()
    local sWidth = love.graphics.getWidth()
    local sHeight = love.graphics.getHeight()

    ents:Startup()
    local ballEnt = ents.Create("ball", 100, 100)
    local paddleEnt= ents.Create("paddle", sWidth/2 - 64, sHeight - 4)
    print(paddleEnt.x, paddleEnt.y, paddleEnt.width, paddleEnt.height)
end
Then you need to detect a circle <> rectangle collision, here's a good approach:
https://gist.github.com/vonWolfehaus/5023015
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests