Page 1 of 3
Warping screen? [RESOLVED]
Posted: Mon Dec 26, 2011 7:30 pm
by Evil_Bartek
This is my main.lua so far...
Code: Select all
function love.load()
char = {posx = 40,posy = 160,width = 40, length = 40, health = 100}
end
function love.draw()
love.graphics.rectangle("fill",char.posx,char.posy,char.width,char.length)
end
-- Don't change this, I like it that way ~Evil_Bartek
function love.update(dt)
if love.keyboard.isDown("right") then
char.posx = char.posx + 0.4
elseif love.keyboard.isDown("left") then
char.posx = char.posx - 0.4
end
end
Just some noob code i wrote, you can see what it does...
How can i make it so when hes out of screen in a horizontal position. Like if he went out of the right or left side of the screen, how can i make him appear on the other side?
EDIT: I did make a conf.lua but thats not needed here...
Re: Warping screen?
Posted: Mon Dec 26, 2011 7:38 pm
by Ryne
Evil_Bartek wrote:
Just some noob code i wrote, you can see what it does...
How can i make it so when hes out of screen in a horizontal position. Like if he went out of the right or left side of the screen, how can i make him appear on the other side?
you can do something like this:
Code: Select all
function love.update(dt)
if love.keyboard.isDown("right") then
char.posx = char.posx + 0.4
elseif love.keyboard.isDown("left") then
char.posx = char.posx - 0.4
end
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
if char.posx <= 0 then -- if the character "x" is equal less than "0" meaning it's outside the left side of the screen
char.posx = width - char.width -- set the character "x" to the width of the screen minus the character width, so he appears on the right side
elseif char.posx >= width - char.width then
char.posx = width - width
end
end
basically "love.graphics.getWidth, and getHeight" will grab the width and height of the screen. Using this you can just use some simple "if" statements to teleport the player if they go beyond the sides of the screen.
so this code is saying "if the characters "x" is less than the screen's x, then that means the character is going past the left side of the screen", and "if the characters "x" is MORE than the screen width then the character is going past the right side of the screen"
then you're just setting a new character.x, and character.y depending on what's happening.
Re: Warping screen?
Posted: Mon Dec 26, 2011 7:42 pm
by Evil_Bartek
It works but it's kinda fishy.
It looks like its teleporting.
Can you make its so that if you go out of screen on the right it will only teleport if the left edge touches it?
Is that possible?
Re: Warping screen?
Posted: Mon Dec 26, 2011 7:44 pm
by Evil_Bartek
I FIXED IT SO ITS BETTER:
Code: Select all
function love.load()
require "conf.lua"
char = {posx = 40,posy = 160,width = 40, length = 40, health = 100}
end
function love.draw()
love.graphics.rectangle("fill",char.posx,char.posy,char.width,char.length)
end
function love.update(dt)
if love.keyboard.isDown("right") then
char.posx = char.posx + 0.4
elseif love.keyboard.isDown("left") then
char.posx = char.posx - 0.4
end
local width = love.graphics.getWidth()
local lol = width + 40
local height = love.graphics.getHeight()
if char.posx <= -40 then
char.posx = width - char.width
elseif char.posx >= lol - char.width then
char.posx = width - width
end
end
Thatk you for the general template anyway, i improved your karma.
Re: Warping screen?
Posted: Mon Dec 26, 2011 7:48 pm
by Ryne
Evil_Bartek wrote:It works but it's kinda fishy.
It looks like its teleporting.
Can you make its so that if you go out of screen on the right it will only teleport if the left edge touches it?
Is that possible?
By "warp" I assumed you meant teleport. Also yeah, you can just do the same thing as I did with the opposite side. Just "- character.w".
Evil_Bartek wrote:I FIXED IT SO ITS BETTER:
Thatk you for the general template anyway, i improved your karma.
Great!
Re: Warping screen?
Posted: Mon Dec 26, 2011 7:52 pm
by Evil_Bartek
EVEN BETTER:
Code: Select all
function love.load()
require "conf.lua"
char = {posx = 40,posy = 160,width = 40, length = 40, health = 100}
end
function love.draw()
love.graphics.rectangle("fill",char.posx,char.posy,char.width,char.length)
end
function love.update(dt)
if love.keyboard.isDown("right") then
char.posx = char.posx + 0.4
elseif love.keyboard.isDown("left") then
char.posx = char.posx - 0.4
end
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
if char.posx <= -40 then
char.posx = (width + 35) - char.width
elseif char.posx >= (width + char.width) - char.width then
char.posx = (width - 35) - width
end
end
Its ass smooth as it could be...
Re: Warping screen?
Posted: Mon Dec 26, 2011 7:52 pm
by Evil_Bartek
EVEN BETTER:
Code: Select all
function love.load()
require "conf.lua"
char = {posx = 40,posy = 160,width = 40, length = 40, health = 100}
end
function love.draw()
love.graphics.rectangle("fill",char.posx,char.posy,char.width,char.length)
end
function love.update(dt)
if love.keyboard.isDown("right") then
char.posx = char.posx + 0.4
elseif love.keyboard.isDown("left") then
char.posx = char.posx - 0.4
end
local width = love.graphics.getWidth()
local height = love.graphics.getHeight()
if char.posx <= -40 then
char.posx = (width + 35) - char.width
elseif char.posx >= (width + char.width) - char.width then
char.posx = (width - 35) - width
end
end
Re: Warping screen?
Posted: Mon Dec 26, 2011 8:45 pm
by Evil_Bartek
No i made it as smooth!
Re: Warping screen?
Posted: Tue Dec 27, 2011 12:48 am
by tentus
Don't double post, and definitely don't triple post.
Re: Warping screen?
Posted: Tue Dec 27, 2011 12:51 am
by osgeld
whats it matter in his own thread, its not like there is 15 threads in the forum covering the same topic (which is what I have always considered to be double posting), and you are just derealing the thing anyway ... 2 evils do not make bacon
2 cents