Slots offset problem

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
Mateus
Prole
Posts: 42
Joined: Sat Apr 04, 2015 1:02 pm

Slots offset problem

Post by Mateus »

Hello, I am making a video slot machine game, but I have a weird problem.

I have 5 boxes going from y: 0 to y: 100 for example.
the size of the box is 10.

So, when the box y is equal or greater then 100 - 10, its position is set to 0.
BUT, the faster it spins, there are some weird offsets between the boxes.

Take a look :

WITHOUT VSYNC (1000 FPS)
https://love2d.org/imgmirrur/yNtfLEt.png


WITH VSYNC (75 FPS)
https://love2d.org/imgmirrur/WwdovWc.png



Any help please ?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Slots offset problem

Post by pgimeno »

Can you post the .love file?
Mateus
Prole
Posts: 42
Joined: Sat Apr 04, 2015 1:02 pm

Re: Slots offset problem

Post by Mateus »

Code: Select all

require "mathlib"

items = {}
items[1] = love.graphics.newImage("icons/bell.png")
items[2] = love.graphics.newImage("icons/diamond.png")
items[3] = love.graphics.newImage("icons/emerald.png")
items[4] = love.graphics.newImage("icons/goldbar.png")
items[5] = love.graphics.newImage("icons/goldclover.png")
items[6] = love.graphics.newImage("icons/goldenseven.png")
items[7] = love.graphics.newImage("icons/grape.png")
items[8] = love.graphics.newImage("icons/greenbar.png")
items[9] = love.graphics.newImage("icons/greenclover.png")
items[10] = love.graphics.newImage("icons/heart.png")
items[11] = love.graphics.newImage("icons/horseshoe.png")
items[12] = love.graphics.newImage("icons/cherry.png")
items[13] = love.graphics.newImage("icons/lemon.png")
items[14] = love.graphics.newImage("icons/plum.png")
items[15] = love.graphics.newImage("icons/redbar.png")
items[16] = love.graphics.newImage("icons/redseven.png")
items[17] = love.graphics.newImage("icons/ryby.png")
items[18] = love.graphics.newImage("icons/watermelon.png")

sounds = {}
sounds.spin1 = love.audio.newSource("sounds/spin1.mp3", "static")
sounds.stop1 = love.audio.newSource("sounds/stop1.mp3", "static")
sounds.stop2 = love.audio.newSource("sounds/stop2.mp3", "static")
sounds.win1 = love.audio.newSource("sounds/win1.mp3", "static")
sounds.win2 = love.audio.newSource("sounds/win2.mp3", "static")
sounds.win3 = love.audio.newSource("sounds/win3.mp3", "static")
sounds.win4 = love.audio.newSource("sounds/win4.mp3", "static")
sounds.win5 = love.audio.newSource("sounds/win5.mp3", "static")

column1 = {}
column1.x = 151
column1.y = 151
column1.yMove = 0
column1.winItem = items[1]
column1.winItemY = 0

spin = false
stop = false

spinTimer = 0


function love.load()

	rnd = love.math.newRandomGenerator(os.time())
	love.window.setMode(800,600,{vsync=true})

	for i = 1, 7 do
		box={}
		box.y = -120+i*60
		box.item = items[1]

		table.insert(column1,box)
	end
end

function love.update()

	love.window.setTitle("FPS: "..love.timer.getFPS())

	if love.keyboard.isDown("return") and not spin then
		spin = true
		stop = false
		column1.yMove = -2

		column1.winItem = items[rnd:random(1,18)]

		spinTimer = 1
	end

	if spinTimer>0 then
		spinTimer = spinTimer - 1
	else
		--spin = false
	end

	if spin then
		for k,v in ipairs(column1) do
			v.y = v.y + column1.yMove

			if v.y > 360 then
				v.y = -60 + column1.yMove/2
				v.item = items[rnd:random(1,18)]
			end

			if v.y >= 260 and v.item == column1.winItem then
				stop = true

			end

			if v.item == column1.winItem then
				column1.winItemY = v.y
			end
		end

		if not stop then
			column1.yMove = math.clamp(column1.yMove + 0.2,-5,10) 	
		end
	end

	if stop then
		column1.yMove = math.clamp(column1.yMove - 1,-5,15)
		if column1.winItemY <= 240 then
			spin = false
		end
	end

end

function love.draw()


	for k,v in ipairs(column1) do

		love.graphics.setColor(150,150,150)
		love.graphics.rectangle("fill",column1.x,column1.y+v.y,60,60)

		love.graphics.setColor(255,255,255)
		love.graphics.draw(v.item,column1.x,column1.y+v.y,0,0.116)

	end 

	love.graphics.setColor(100,100,100)
	love.graphics.rectangle('line', 150, 150, 500, 300)

	love.graphics.draw(column1.winItem,400,50,0,0.166)

end
User avatar
Davidobot
Party member
Posts: 1226
Joined: Sat Mar 31, 2012 5:18 am
Location: Oxford, UK
Contact:

Re: Slots offset problem

Post by Davidobot »

You need to use dt in love.update: (Note the dt passed in love.update)

Code: Select all

function love.update(dt)
	love.window.setTitle("FPS: "..love.timer.getFPS())

	if love.keyboard.isDown("return") and not spin then
		spin = true
		stop = false
		column1.yMove = -2

		column1.winItem = items[ rnd:random(1, 18) ]

		spinTimer = 1
	end

	if spinTimer>0 then
		spinTimer = spinTimer - (1 * dt)
	else
		--spin = false
	end

	if spin then
		for k,v in ipairs(column1) do
			v.y = v.y + (column1.yMove * dt)

			if v.y > 360 then
				v.y = -60 + column1.yMove / 2
				v.item = items[ rnd:random(1, 18) ]
			end

			if v.y >= 260 and v.item == column1.winItem then
				stop = true
			end

			if v.item == column1.winItem then
				column1.winItemY = v.y
			end
		end

		if not stop then
			column1.yMove = math.clamp(column1.yMove + 0.2,-5,10)    
		end
	end

	if stop then
		column1.yMove = math.clamp(column1.yMove - 1, -5, 15)

		if column1.winItemY <= 240 then
			spin = false
		end
	end

end
PM me on here or elsewhere if you'd like to discuss porting your game to Nintendo Switch via mazette!
personal page and a raycaster
Mateus
Prole
Posts: 42
Joined: Sat Apr 04, 2015 1:02 pm

Re: Slots offset problem

Post by Mateus »

It still happens.
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Slots offset problem

Post by pgimeno »

I think your problem is here:

Code: Select all

         if v.y > 360 then
            v.y = -60 + column1.yMove/2
Try changing it to:

Code: Select all

         if v.y >= 360 then
            v.y = v.y - (360+60)
Mateus
Prole
Posts: 42
Joined: Sat Apr 04, 2015 1:02 pm

Re: Slots offset problem

Post by Mateus »

pgimeno wrote:I think your problem is here:

Code: Select all

         if v.y > 360 then
            v.y = -60 + column1.yMove/2
Try changing it to:

Code: Select all

         if v.y >= 360 then
            v.y = v.y - (360+60)
Youp, seems like it fixed it, THANKS! But why it does that ? Isn't it engine problem ?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Slots offset problem

Post by pgimeno »

The problem is that it doesn't always go out of range (>=360) when it's exactly a multiple of 60, which means that the other images are not aligned to a multiple of 60 either, so when you put it on top, the next one does not start where this one ends. That changes the spacing, and doing it several times creates gaps over time.

To keep them aligned (the bottom of the image touching the top of the next), in my suggested version I just subtracted the distance from the bottom to the top. Another option would be to use v.y % 60 to determine the vertical offset, but a subtraction is faster so I opted for that.

I don't see a difference in behaviour with vsync on or off, apart from speed.

It was a bit difficult to set up a working version so I could run it to understand the problem. Please consider supplying a self-contained .love that exhibits the problem the next time.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 68 guests