Problem with Libraries

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.
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Problem with Libraries

Post by trubblegum »

You would need to check for collisions with platforms, rather than just the floor.

Code: Select all

local floor = false
for i, platform in pairs(platforms) do
   floor = collide(ball, platform)
end
if floor then ball.y = floor.y else ball.y = ball.y + (256 * dt) end
Wisdom regarding how to check collisions is manyfold and plentious, but here's a really simple procedure :

Code: Select all

function withinrect(pos, rect)
  return pos.x > rect.x and pos.x < rect.x + rect.w and pos.y > rect.y and pos.y < rect.y + rect.h
end
function collide(object, platform)
   if withinrect(object, platform) then return platform else return false end
end
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Re: Problem with Libraries

Post by YGOFreak1997 »

Oh yes, thats a good time to ask i think. What is a for-loop?
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Problem with Libraries

Post by tentus »

YGOFreak1997 wrote:Oh yes, thats a good time to ask i think. What is a for-loop?
http://www.lua.org/pil/4.3.4.html
Essentially, you repeat a section of code until a condition is true.

Code: Select all

x = 0
for i=1, 50 do
   x = x + 1
end
print(x)   -- you'll get 50, because the above loop happened 50 times.
Kurosuke needs beta testers
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Re: Problem with Libraries

Post by YGOFreak1997 »

@tentus:

I tried it out, but something weird happens, it doesnt stop counting ^^ Here's my code:

Code: Select all

function love.load()
	x = 0
end


function love.update(dt)
	for i=1, 50 do
		x = x + 1
	end
end

function love.draw()
	love.graphics.print(x, 100, 100)
end

@trubblegum:

In the last two functions you posted, you define "object" and "platform" as arguments. What "Type" are they?
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Problem with Libraries

Post by Nixola »

love.update(), like love.draw(), is run every frame, so the loop will run once per frame, so you'll have X increasing by 50 every frame
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Problem with Libraries

Post by trubblegum »

YGOFreak1997 wrote:In the last two functions you posted, you define "object" and "platform" as arguments. What "Type" are they?
First of all, and very importantly, I never defined object or platform in my code.
A definition defines something (a variable or function), and because I never declared either variable, neither are defined. You don't know what they are, and neither does the interpreter (the program, if you like)

It is assumed from the indices (or keys) used in withinrect that :
- object is of type table and contains at least numbers x, y as in :

Code: Select all

object = {x = 256, y = 256}
- platform is of type table and contains at least numbers x, y, w, h as in :

Code: Select all

platform= {x = 128, y = 0, w = 256, h = 32}
And platforms is an array (a base-1 linear-number-indexed table, sometimes called non-dictionary or auto-indexed) containing instances of the platform table, or at least tables which contain platform's minimum set of values.
It could be initialized with :

Code: Select all

platforms = {} -- global platforms, an empty table

function newplatform() return {x = 128, y = 0, w = 256, h = 32} end -- a function which defines a new table and returns it

love.load = function()
   i = 1
   while i <= 10 do
      platforms[i] = newplatform() -- an index for a new platform in platforms, and a new platform as its value
      i = i + 1
   end
end
Of course, all the platforms returned by newplatform() have the same position and dimensions, so you will want to pass it some arguments to get the platforms you want.
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Re: Problem with Libraries

Post by YGOFreak1997 »

And, for example, i could randomly spawn new platforms with the help of hump.timer and call the newPlatform function for example ever 0.1 seconds at a random position.

And also, if there are many platforms, and that is the case in a platformer ^^, how can i look if ANY platform collides with my chraracter? Can i check the whole array/table if it collides with my character?
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Problem with Libraries

Post by trubblegum »

YGOFreak1997 wrote:And, for example, i could randomly spawn new platforms with the help of hump.timer and call the newPlatform function for example ever 0.1 seconds at a random position.
I don't see why you wouldn't make them all at the same time, but sure.
And also, if there are many platforms, and that is the case in a platformer ^^, how can i look if ANY platform collides with my chraracter? Can i check the whole array/table if it collides with my character?
How did you go through platforms and assign its elements?

You should probably learn a little about programming before trying to write a program.
Check out the tutorial section on the wiki : http://love2d.org/wiki
And click the link in the pink at the top of this page.
User avatar
YGOFreak1997
Party member
Posts: 103
Joined: Sun Jan 22, 2012 4:29 pm
Location: Germany, Baden-Württemberg
Contact:

Re: Problem with Libraries

Post by YGOFreak1997 »

Why does everybody say things like this to me? Am I so bad? I just wanted to male little theory how to approach my programming in this game. With my first thought, i would randomly place platforms in front of my character, but still outside the screen, so that it looks like they randomly spawn there. And whats wrong with my second question? I think you have to look if your character collides with any platform, don't you? I understand the basics of lua and LÖVE and I also read a few Tutorials. And now I just wanna make a little game ^^
Also look at my LÖVE-Tutorial-Youtube-Channel (German ^^)
GitHub Repo: Click Me!
Website: No, Me!
IndieDB: Or Maybe Me?

ÖBEY!
User avatar
trubblegum
Party member
Posts: 192
Joined: Wed Feb 22, 2012 10:40 pm

Re: Problem with Libraries

Post by trubblegum »

Post Reply

Who is online

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