Hello. This is my 1st week of playing with love2d. i recently learning making Pong game from cs50 course. i code my game 100% on my old samsung note 3 phone with help of love2d app from playstore and code editor from market.
since pong tutorial teach about keyboard press, so it's impossible to replicate it on android coz android using touch function right.. so i i google every source code related to love2d 2019 and finally figured that if i want things to happen in game, i need to invisibily create a touch region on screen, hard coded it that if i press that region, things happen ( for example, the paddle pong moves if this region x,y touched).
but i got this question in my mind, how to make things touchable? for example if i draw a "start button" on screen. i want the the game start after pressed that button. how to do it? i meant code for enable the startbutton.png is touched..
please guide me with example code. i really want to learn this
In need of example code of this matter
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: In need of example code of this matter
Maybe something like this?
Code: Select all
PointInBox = function( point, box )
local x, y, w, h = unpack( box )
local px, py = point[1], point[2]
if px>=x and px<=x+w and py>=y and py<=y+h then
return true
end
return false
end
touch = function( box )
local mx, my = love.mouse.getPosition( )
if love.mouse.isDown( 1 ) and PointInBox({mx,my},box) then
return true
end
return false
end
Re: In need of example code of this matter
Well since you're making a mobile game you might as well use mobile controls - touchscreen and gyroscope. It should be pretty easy to control paddle position directly using phone tilt or finger position on the screen.
To answer your question, you simply check if touch coordinates are within certain range, for example X > 100 and X < 200 and Y > 100 and Y < 200. If that resolves to true, then you activate the action that's meant to happen when you press that region of the screen. Draw a sprite in the same location and you got a button.
A side note - pong (and breakout) are meant to be played on an analog slider controller, on a PC you'd use a mouse and old consoles had special pong controllers.
To answer your question, you simply check if touch coordinates are within certain range, for example X > 100 and X < 200 and Y > 100 and Y < 200. If that resolves to true, then you activate the action that's meant to happen when you press that region of the screen. Draw a sprite in the same location and you got a button.
A side note - pong (and breakout) are meant to be played on an analog slider controller, on a PC you'd use a mouse and old consoles had special pong controllers.
Re: In need of example code of this matter
hello, sorry for really late replay as i only able to access office PC during weekdays. thank you so much for this code. not only it's a much better version than mine ( which is uncessesary long and complex), your code also made me realize i thinking too much just for a simple problem lol.. my version of code is bad i'd shame to share here xDRef wrote: ↑Fri Dec 20, 2019 7:43 pm Maybe something like this?Code: Select all
PointInBox = function( point, box ) local x, y, w, h = unpack( box ) local px, py = point[1], point[2] if px>=x and px<=x+w and py>=y and py<=y+h then return true end return false end touch = function( box ) local mx, my = love.mouse.getPosition( ) if love.mouse.isDown( 1 ) and PointInBox({mx,my},box) then return true end return false end
also i learn new knowledge about unpack which i can see myself using this a lot in future.
thanks for effort and time helping beginner like me
Re: In need of example code of this matter
yeah after studying the code shared by RF, i learn how to do it as short as possible without assigning to unnecessary global variable ( my version) . i planned to make paddle move by dragging the paddle with finger. will search more info regarding it . thanks for reply, the touch concept explained. i appreciate it!raidho36 wrote: ↑Fri Dec 20, 2019 9:27 pm Well since you're making a mobile game you might as well use mobile controls - touchscreen and gyroscope. It should be pretty easy to control paddle position directly using phone tilt or finger position on the screen.
To answer your question, you simply check if touch coordinates are within certain range, for example X > 100 and X < 200 and Y > 100 and Y < 200. If that resolves to true, then you activate the action that's meant to happen when you press that region of the screen. Draw a sprite in the same location and you got a button.
A side note - pong (and breakout) are meant to be played on an analog slider controller, on a PC you'd use a mouse and old consoles had special pong controllers.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 3 guests