engine error or script error?

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
healLV
Prole
Posts: 4
Joined: Mon Jun 04, 2018 9:50 am

engine error or script error?

Post by healLV »

so I was messing around with code other day and tried to manually make vector like player. The drawing of it worked well and moving the character up down and right works like a charm aswell except when I try to move it to left it just goes to hell and flies away. It starts to add numbers out of nowhere since I checked through code many times still not seeing error. Could anyone point out whats wrong with it sincr Ive had no luck with it. Adding main.lua as attachment.
Attachments
main.lua
(2.58 KiB) Downloaded 323 times
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: engine error or script error?

Post by ivan »

Welcome to the forums.
No offence, but your code is a mess.
Variables have strange names, delta is not taken into account, loops don't make sense, if-else statements are not logically structured...
My suggestion is to go over the following - I've cleaned it up which is a good starting point:

Code: Select all

function love.load()
	lastFPS = os.time()
	FPS = love.timer.getFPS()
	
	polys = {
	right =  {0,0, 10,0, 10,10, 30,10, 30,0, 40,0, 40,10, 50,10, 50,20, 40,20, 40,30, 30,30, 30,20, 10,20, 10,30, 0,30},
	left = {50,0, 40,0, 40,10, 20,10, 20,0, 10,0, 10,10, 0,10, 0,20, 10,20, 10,30, 20,30, 20,20, 40,20, 40,30, 50,30},
	up =   {0,50, 30,50, 30,40, 20,40, 20,20, 30,20, 30,10, 20,10, 20,0, 10,0, 10,10, 0,10, 0,20, 10,20, 10,40, 0,40},
	down = {0,0, 30,0, 30,10, 20,10, 20,30, 30,30, 30,40, 20,40, 20,50, 10,50, 10,40, 0,40, 0,30, 10,30, 10,10, 0,10}
	}
	
	playerRotation = "left"
	playerX = 50
	playerY = 50
  playerSpeed = 40
end

function love.conf()
	t.console = true
end

function love.update(dt)
	--print("Pressed key "..tostring(key).." "..tostring(u).." "..type(u))

  local dx, dy = 0, 0
	if love.keyboard.isDown("d") then
		dx = dx + 1
		playerRotation = "right"
	elseif love.keyboard.isDown("a") then
		dx = dx - 1
		playerRotation = "left"
	elseif love.keyboard.isDown("w") then
		dy = dy - 1
		playerRotation = "up"
	elseif love.keyboard.isDown("s") then
		dy = dy + 1
		playerRotation = "down"
	end
  if love.keyboard.isDown("lshift") then
    dx = dx*5
    dy = dy*5
  end
  
	-- if player has moved recalculates the figures new position
  playerX = playerX + dx*playerSpeed*dt
  playerY = playerY + dy*playerSpeed*dt
end



function love.draw()
	if os.time() - lastFPS >= 1 then
		FPS = love.timer.getFPS()
	end
	
	love.graphics.print("FPS: "..FPS, 0, 0)
	love.graphics.print("X: "..playerX.." Y: "..playerY, 0, 10)
	love.graphics.translate(playerX, playerY) -- reference point
	love.graphics.polygon("line", polys[playerRotation])
end
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: engine error or script error?

Post by zorg »

If you want to get the average FPS, there's already a way to do that: 1 / love.timer.getAverageDelta (and floor it if you want it to be whole numbers only)
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: engine error or script error?

Post by ivan »

Additionally, if you want to rotate a polygon you can write:
love.graphics.rotate(playerAngle)
That makes a lot more sense than hard-coding different versions of the same polygon.
healLV
Prole
Posts: 4
Joined: Mon Jun 04, 2018 9:50 am

Re: engine error or script error?

Post by healLV »

oh dear didnt think my code is that much of a mess. that if statement fragmentation could have been avoided. thanks for helping.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 11 guests