How do you make stuff move by itself?

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
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

How do you make stuff move by itself?

Post by BEANSFTW »

I made my game so when you press the spacebar, a boulder would drop from my character. I got a boulder to appear where my character is, but now I want it to fall. (This game is like those games like pacman where there's no gravity.

Here's my code if you need it. The boulder thing is in the love.update inside the if love.keyboard.isDown(" ")

Code: Select all

function love.load()

	require ("Rock")
	require ("anal")
	
	RockMoveS = 180
	RockMoveX = 355
	RockMoveY = 255
	
	Boulder = love.graphics.newImage("Rock.png")
	Rockmonster = love.graphics.newImage("Rockmonster.png")
	backround = love.graphics.newImage("backround.png")

	RockWalkR = love.graphics.newImage("RockMonsterR.png") 
	RockWalkR:setFilter("nearest", "nearest")             
	MonsterR = newAnimation(RockWalkR, 45, 45, 0.1, 2)

	BoulderSpawn = true
	
	music = love.audio.newSource("doop.ogg", "steam")
	music:setLooping(true)
	love.audio.play(music)
	
end

function love.update(dt)

	
	if love.keyboard.isDown("w") then
		RockMoveY = RockMoveY - RockMoveS*dt
		MonsterR:update(dt)
	end	
	
	if love.keyboard.isDown("s") then
		RockMoveY = RockMoveY + RockMoveS*dt
		MonsterR:update(dt)
	end

	if love.keyboard.isDown("d") then
		RockMoveX = RockMoveX + RockMoveS*dt
		MonsterR:update(dt)
	end

	if love.keyboard.isDown("a") then
		RockMoveX = RockMoveX - RockMoveS*dt
		MonsterR:update(dt)
	end	

	if love.keyboard.isDown(" ") and BoulderSpawn == true then
		Rocks_spawn(RockMoveX, RockMoveY, Boulder)
		BoulderSpawn = false
	end	
	
	if love.keyboard.isDown("d") and love.keyboard.isDown("s") then
		RockMoveS = 210
	end	

	if love.keyboard.isDown("a") and love.keyboard.isDown("s") then
		RockMoveS = 210
	end
	
	if love.keyboard.isDown("a") and love.keyboard.isDown("w") then
		RockMoveS = 205
	end	
	
	if love.keyboard.isDown("d") and love.keyboard.isDown("w") then
		RockMoveS = 205
	end
	
	

	
	if RockMoveY > 555 then
		RockMoveY = 555
	end
	
	if RockMoveX > 755 then
		RockMoveX = 755
	end	
	
	if RockMoveY < 0 then
		RockMoveY = 0
	end
	
	if RockMoveX < 0 then
		RockMoveX = 0
	end
	
end

function love.draw()



	love.graphics.draw(backround, 0, 0)
	
	Rocks_draw()
	
	MonsterR:draw(RockMoveX, RockMoveY, 0, 1)

end
User avatar
Roland_Yonaba
Inner party member
Posts: 1563
Joined: Tue Jun 21, 2011 6:08 pm
Location: Ouagadougou (Burkina Faso)
Contact:

Re: How do you make stuff move by itself?

Post by Roland_Yonaba »

You can create a table collecting al lexisting boulders.
So, when you hit spacebar, you just spawn the new boulder, adding it to the table.
Each update cycle, you basically loop through all boulders registered in this table and apply some "hardcoded gravity" on each one.
In the meantime, you keep deleting boulders that are supposed to disappear from the game environment.
Of course, it means that boulders are treated like distinct objects.

Code: Select all

local all_boulders= {}
-- ...

-- Spawning a new boulder will result in something like
function spawn_boulder(x,y,image)
   local boulder = {x= x, y = y, img = img, fallSpeed = 50}
  all_boulders[#all_boulders+1] = boulder -- register the new boulder in all_boulders
end

 -- ...

function love.update(dt)
   ...
   for i = #all_boulders,1,-1 do
      local boulder = all_boulders[i]
     -- for instance, the boulder is dead if it goes out of the window
     if boulder.y > love.graphics.getHeight() then 
       table.remove(all_boulders,i)
    end
    -- faking gravity
    boulder.y = boulder.y + dt*boulder.fallSpeed
   ...
end
That's just the general idea, you'll have to adjust it to your code.
User avatar
BEANSFTW
Prole
Posts: 28
Joined: Sun Apr 15, 2012 2:58 pm

Re: How do you make stuff move by itself?

Post by BEANSFTW »

In the love.update, every time I type "do" it will think love.draw is a part of love.update. I don't know if its the code, or just a glitch. Either way, this code doesn't work :(
User avatar
paritybit
Citizen
Posts: 53
Joined: Thu Sep 06, 2012 3:52 am

Re: How do you make stuff move by itself?

Post by paritybit »

For the 'do', you need a corresponding 'end'. I'm not sure if this is the problem you're having, but it sounds like it.

If you post the full file (updated), or a .love, more people are likely to help.
Post Reply

Who is online

Users browsing this forum: Yolwoocle and 25 guests