Warping screen? [RESOLVED]

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.
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Warping screen? [RESOLVED]

Post 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...
Last edited by Evil_Bartek on Tue Dec 27, 2011 10:29 pm, edited 2 times in total.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Warping screen?

Post 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.
Last edited by Ryne on Mon Dec 26, 2011 7:44 pm, edited 1 time in total.
@rynesaur
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Warping screen?

Post 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?
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Warping screen?

Post 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.
User avatar
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

Re: Warping screen?

Post 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!
@rynesaur
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Warping screen?

Post 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...
Last edited by Evil_Bartek on Mon Dec 26, 2011 7:53 pm, edited 1 time in total.
Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Warping screen?

Post 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

Evil_Bartek
Prole
Posts: 43
Joined: Sun Dec 25, 2011 5:13 pm

Re: Warping screen?

Post by Evil_Bartek »

No i made it as smooth!
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Warping screen?

Post by tentus »

Don't double post, and definitely don't triple post.
Kurosuke needs beta testers
User avatar
osgeld
Party member
Posts: 303
Joined: Sun Nov 23, 2008 10:13 pm

Re: Warping screen?

Post 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
Post Reply

Who is online

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