Newbie has a few basic questions

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.
radianc3
Prole
Posts: 3
Joined: Thu Jan 06, 2011 8:07 pm

Newbie has a few basic questions

Post by radianc3 »

Hi guys and girls!

I'm new to LÖVE/LUA and started yesterday trying a few things. Now i have some questions because i couldn't find anything on the forum (may i haven't tried hard enough). I hope this is the right forum place for my thread.

Question 1:
I've a picture and can move it with the up, down, left, right keys. Thats fine, but how can i avoid it running out of the screen (e.g. its 640x480 resolution) when its moving?

Question 2:
How can i change the image depending on which direction the "player" moves? I tried some things but it didn't work (well).

Question 3:
Simple Animations. How do they work? Is there any Tutorial you know or some small code examples?

Question 4:
Simple collision detection. Same as above. Is there any Tutorial you guys know or some small code examples?

Ah yes, heres my little code and thx in advance! :ultraglee:

Code: Select all

function love.load()
	image = love.graphics.newImage("cake.png")
   
	music = love.audio.newSource("polka.mp3")
	love.audio.play(music)

	playerX = "190"
	playerY = "190"
   
	love.graphics.setBackgroundColor(255,255,255)
   
end

function love.draw()
	love.graphics.draw(image, playerX, playerY)
end


function love.update(dt)
	
	if love.keyboard.isDown("right") then
		playerX = playerX + 0.5
	end
	
	if love.keyboard.isDown("left") then
		playerX = playerX - 0.5
	end
	
	if love.keyboard.isDown("up") then
		playerY = playerY - 0.5
	end
	
	if love.keyboard.isDown("down") then
		playerY = playerY + 0.5
	end
end
User avatar
bartoleo
Party member
Posts: 118
Joined: Wed Jul 14, 2010 10:57 am
Location: Savigliano

Re: Newbie has a few basic questions

Post by bartoleo »

Question 1:
I've a picture and can move it with the up, down, left, right keys. Thats fine, but how can i avoid it running out of the screen (e.g. its 640x480 resolution) when its moving?
before you change X or Y in love.update
test them against 0 and love.graphics.getWidth( ) or love.graphics.getHeight( )
Question 2:
How can i change the image depending on which direction the "player" moves? I tried some things but it didn't work (well).
load one image for each direction
have a variable for direction
use it on love.draw
change it on love.update when changing x or y
Question 3:
Simple Animations. How do they work? Is there any Tutorial you know or some small code examples?
switch images
or use http://love2d.org/wiki/AnAL
Question 4:
Simple collision detection. Same as above. Is there any Tutorial you guys know or some small code examples?
there are many topics in forum...
search
Bartoleo
radianc3
Prole
Posts: 3
Joined: Thu Jan 06, 2011 8:07 pm

Re: Newbie has a few basic questions

Post by radianc3 »

Thanks so far. I'm a lil step further now but i still have some problems with the running out of the window thing.

I tried to get rid of the problem by setting the playerSpeed to Zero, but that didn't work good.

Then i tried the method to set the player to the love.graphics.getWidth position but i also failed at it. Just got some errors on that cuz my syntax was wrong or something, dunno exactly.

At last i tried a third method by just putting the player on the opposite side (e.g. he runs out right and comes back from the left). It worked, but only for one side. If i move the player out on the left side to appear on the right he gets stuck cuz of the check if he is running out on the right.

:(

Maybe someone can help me with that or show me an simple example, please?


And the AnAl thing for animations didn't work for me. I tried the code snippet shown there with the same image but i only received an error message. Something with (nil), whatever it is. Do i have to use the AnAl library and integrate it as resource in my main.lua?

Thanks in advance and here is my 'updated' code again.

Code: Select all

function love.load()
	
	-- Spieler Grafiken
	image1 = love.graphics.newImage("cake_right.png")
    image2 = love.graphics.newImage("cake_left.png")
	
	-- Den Spieler als Variable definieren um die Spielergrafiken 
	-- ändern zu können
	player = image1
	
   -- Die Hintergrundmusik
	music = love.audio.newSource("polka.mp3")
	love.audio.play(music)
	
	-- Startposition des Spielers auf X und Y Achse
	playerX = "288"
	playerY = "202"
	playerSpeed = "0.5"
	
	-- Spielfeldgröße zum abfragen
	screenX = love.graphics.getWidth()
	screenY = love.graphics.getHeight()
	-- Hintergrundfarbe "weiss"
	love.graphics.setBackgroundColor(255,255,255)
   
end

	
function love.draw()
	-- laden der Spielergrafik
	love.graphics.draw(player, playerX, playerY)
end


function love.update(dt)
	
	-- Festlegen der Bewegungstasten und ändern des Spielerbilds 
	-- je nach Laufrichtung durch die Variable player
	if love.keyboard.isDown("right") then
		playerX = playerX + playerSpeed
		player = image1
	end
	
	if love.keyboard.isDown("left") then
		playerX = playerX - playerSpeed
		player = image2
	end
	
	if love.keyboard.isDown("up") then
		playerY = playerY - playerSpeed
	end
	
	if love.keyboard.isDown("down") then
			playerY = playerY + playerSpeed
	end
	
	-- Abfragen der Position des Spielers um nicht aus dem 
	-- Bild laufen zu können
	
	if playerX == screenX - 64 then
		playerX = 0 - 64 elseif
			playerY == screenY - 64 then
		playerY = 0 - 64 
	end
end
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Newbie has a few basic questions

Post by tentus »

I'm just eyeballing your code, but it seems like this should help:

Code: Select all

function love.load()

	-- Spieler Grafiken
	image1 = love.graphics.newImage("cake_right.png")
	image2 = love.graphics.newImage("cake_left.png")

	-- Den Spieler als Variable definieren um die Spielergrafiken
	-- ändern zu können
	player = image1

	-- Die Hintergrundmusik
	music = love.audio.newSource("polka.mp3")
	love.audio.play(music)

	-- Startposition des Spielers auf X und Y Achse
	playerX = "288"
	playerY = "202"
	playerSpeed = "50"

	-- Spielfeldgröße zum abfragen
	screenX = love.graphics.getWidth()
	screenY = love.graphics.getHeight()
	-- Hintergrundfarbe "weiss"
	love.graphics.setBackgroundColor(255,255,255)

end

function love.draw()
	-- laden der Spielergrafik
	love.graphics.draw(player, playerX, playerY)
end

function love.update(dt)

	-- Festlegen der Bewegungstasten und ändern des Spielerbilds
	-- je nach Laufrichtung durch die Variable player
	if love.keyboard.isDown("right") then
		playerX = playerX + (playerSpeed * dt)
		player = image1
	end

	if love.keyboard.isDown("left") then
		playerX = playerX - (playerSpeed * dt)
		player = image2
	end

	if love.keyboard.isDown("up") then
		playerY = playerY - (playerSpeed * dt)
	end

	if love.keyboard.isDown("down") then
		playerY = playerY + (playerSpeed * dt)
	end

	-- Abfragen der Position des Spielers um nicht aus dem
	-- Bild laufen zu können

	if playerX > screenX then
		playerX = -64
	elseif playerX < -64 then
		playerX = screenX
	end
	if playerY > screenY then
		playerY = -64
	elseif playerY < -64 then
		playerY = screenY - 64
	end
end
I split the X and Y if statements apart and also broke each one into a greather than and less than statement. If the player goes past the width the screen, he'll switch to -64, meaning that in a second he'll loop back around. Same idea for the Y axis.

I also multiplied your speed by dt, which means that you'll move at a constant rate independent of framerate. Get familiar with dt, it's very useful!
Kurosuke needs beta testers
radianc3
Prole
Posts: 3
Joined: Thu Jan 06, 2011 8:07 pm

Re: Newbie has a few basic questions

Post by radianc3 »

Uh nice, thx for that info. I totally forgot about the bigger and smaller comparisons. But now another problem appears.

Error
Attempt to compare number with string.

But there isn't any string there`?! :x

Ah i got it now. When i defined the screen vars i didi it as string.*facepalm*
Working pretty fine now! :crazy:
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Newbie has a few basic questions

Post by tentus »

Ooh, be sure to make your speed not a string too, I completely overlooked that. :(

Something else you may want to look into is how you are handling the player pressing both left and right at once. You'll probably need a nested if statement to create a logical behavior. Food for thought.
Kurosuke needs beta testers
obur
Prole
Posts: 11
Joined: Sun Feb 20, 2011 2:03 pm

Re: Newbie has a few basic questions

Post by obur »

thanks for help, mate, but i have another question about that.
i figured out the above solution, and used it. however, i thought about adding a third image when there's no motion(action). like when the character is not moving. to clarify:

image1 -> when the object is moving right / pressed to "right"
image2 -> when the object is moving left / pressed to "left"
image3 -> when the object is standing still / pressed to nothing / no action / no movement

so, i thought that i can assign a variable for that. something like a sentinel(flag). i created a variable in love.load(), called motion. and assigned 0 into it. when there's no movement, my sentinel will be 0, whereas if there's a movement (user presses right, left, up, or down) the sentinel will be 1.

and i checked the sentinel variable in love.update(). however, i failed. i even tried "if love.keypressed("") then obj = img3; end" kind of stuff, and failed again.

so, what must i do? now i'm thinking about adding a timer and checking whether if there's a movement for let's say 10ms, then change my object's image as image3.

thanks for everything. :)
User avatar
nevon
Commander of the Circuloids
Posts: 938
Joined: Thu Feb 14, 2008 8:25 pm
Location: Stockholm, Sweden
Contact:

Re: Newbie has a few basic questions

Post by nevon »

obur wrote:so, what must i do? now i'm thinking about adding a timer and checking whether if there's a movement for let's say 10ms, then change my object's image as image3.
There are plenty of ways to solve this. One simple way would be to give your player a state variable and then do something like this:

Code: Select all

function love.load()
    --First we initialize all the state images that we need
    images = {
        idle = love.graphics.newImage("images/idle.png"),
        left = love.graphics.newImage("images/left.png"),
        right = love.graphics.newImage("images/right.png)
    }

    --And here's our player object, complete with a state variable and a variable that holds the current image.
    player = {
        state = "idle",
        image = images["idle"],
        x = 500,
        y = 300,
        speed = 100
    }
end

function love.update(dt)
    --Now what we do is we check for a button to be pressed, and if it is we set the player state and move the player.
    if love.keyboard.isDown("a") then
        player.state = "left"
        player.x = player.x - player.speed*dt
    elseif love.keyboard.isDown("d") then
        player.state = "right"
        player.x = player.x + player.speed*dt
    else
        --If no button is being pressed, we just set the player state to idle
        player.state = "idle"
    end

    --Now we update the player image with whatever image corresponds to the current state.
    player.image = images[player.state]
end

function love.draw()
    --And finally we draw the player.
    love.graphics.draw(player.image, player.x, player.y)
end
obur
Prole
Posts: 11
Joined: Sun Feb 20, 2011 2:03 pm

Re: Newbie has a few basic questions

Post by obur »

i löve you! i really löve you! :nyu:

oh, but there's a problem. i'm making 2 different if statements for walking in 2 directions at the same time. like going up and right at the same time. however, if i make 2 different if statements in here, one of them will drop to else all the time.

Code: Select all

if (up)
 go up;
else if (down)
 go down;

if (right)
 go right;
else if (left)
 go left;
now, if i make only 1 if statement, i can't go in 2 directions at the same time.

by the way, do you know why my previous idea didn't work? i mean that sentinel stuff. thanks again :)
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Newbie has a few basic questions

Post by Robin »

I'm sorry, what is your question?
Help us help you: attach a .love.
Post Reply

Who is online

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