Very basic, 2 button app (for android eventually)

Showcase your libraries, tools and other projects that help your fellow love users.
Post Reply
User avatar
Reenen
Prole
Posts: 44
Joined: Tue Nov 08, 2011 9:44 am

Very basic, 2 button app (for android eventually)

Post by Reenen »

Ok, for my first project, I wanted to create a 2 button application which with one button says "yes" (ja in my language) and another saying "no" (nee in my language).

This application is intended for android tablet, so my images are 128x128, and my sounds are .wav (I wanted .ogg, but the app I used kinda sucked).

My first question is:
Resizing, I'd like the code to be able to handle any resolution, half the screen the one image, and the other half the other one.

So:
  • How can I change the app to fill "client" with the 2 images, no matter what the size of the screen?
  • How do I make sure than the mouse click still happens on the right images?
  • How do I get an object ja with ja.button ja.sound ja.x ja.y, rather than the stand alone variables to work?

Code: Select all

function love.load()
	ja = love.graphics.newImage("ja.png")
	jasound = love.audio.newSource("ja.wav")
	jax = 0
	jay = 0
	
	nee = love.graphics.newImage("nee.png")
	neesound = love.audio.newSource("nee.wav")
	neex = 130
	neey = 0
end

function love.draw()
	love.graphics.draw(ja, jax, jay)
	love.graphics.draw(nee, neex, neey)
end

function love.mousepressed(x, y, button)
	if x > neex then
		love.audio.play(neesound)
	else
		love.audio.play(jasound)
	end
end
Attachments
JaNee.zip
(47.42 KiB) Downloaded 213 times
User avatar
ghoulsblade
Party member
Posts: 111
Joined: Sun Oct 31, 2010 6:11 pm

Re: Very basic, 2 button app (for android eventually)

Post by ghoulsblade »

you can access the screen resolution with

Code: Select all

local screen_width = love.graphics.getWidth( )
local screen_height = love.graphics.getHeight( )
to test if the mouse is in the left half of the screen you can use

Code: Select all

function love.mousepressed(x, y, button)
  local screen_width = love.graphics.getWidth( )
   if x > screen_width/2 then
      love.audio.play(neesound)
   else
      love.audio.play(jasound)
   end
end
to scale the images to the right size you can use the sx, sy params of
http://love2d.org/wiki/love.graphics.draw
e.g. if you want the image to be as big als half the screen and as high as the entire screen :

Code: Select all

local screen_width = love.graphics.getWidth( )
local screen_height = love.graphics.getHeight( )
local image_orig_w = 128
local image_orig_h = 128
local image_wanted_w = screen_width / 2
local image_wanted_h = screen_height
local sx = image_wanted_w / image_orig_w
local sy = image_wanted_h / image_orig_h

local L_x = 0  -- top left corner
local L_y = 0

local R_x = screen_width / 2 -- top middle
local R_y = 0

-- full draw syntax : love.graphics.draw( drawable, x, y, r, sx, sy, ox, oy )
   love.graphics.draw(ja, L_x, L_y, 0, sx,sy)
   love.graphics.draw(nee, R_x, R_y, 0, sx,sy)


to get variables into an object make a table using {}

Code: Select all

   ja = {}
   ja.img = love.graphics.newImage("ja.png")
   ja.sound = love.audio.newSource("ja.wav")
   ja.x = 0
   ja.y = 0
then you also need to adjust the draw call to use ja.img rather than ja
love-android - gamejams
User avatar
Reenen
Prole
Posts: 44
Joined: Tue Nov 08, 2011 9:44 am

Re: Very basic, 2 button app (for android eventually)

Post by Reenen »

Thanks!!! You are a good teacher.

I have one funny thing, not sure why it's like that, but once I've clicked both the buttons, I cannot press any again within about 2s of the last time a sound was played.

I don't think it's really a problem though.
User avatar
Reenen
Prole
Posts: 44
Joined: Tue Nov 08, 2011 9:44 am

Re: Very basic, 2 button app (for android eventually)

Post by Reenen »

Just want to say, it's working on my phone (will load it on the tablet tonight), and it's pretty hard to quit. Thank you very, very much.

This may be the start of a better life for my niece, and really, really thank you so very much. (I should however get some higher quality sound effects)...
User avatar
ghoulsblade
Party member
Posts: 111
Joined: Sun Oct 31, 2010 6:11 pm

Re: Very basic, 2 button app (for android eventually)

Post by ghoulsblade »

you're welcome, i'm glad it helps so much =)

2sounds : i think the love-android version is atm limited to max 4 sound buffers at the same time,
maybe they still count when there's long silence at the end of the sound ?
didn't really test it well yet, but i'll add more audio config api stuff later.
love-android - gamejams
User avatar
Reenen
Prole
Posts: 44
Joined: Tue Nov 08, 2011 9:44 am

Re: Very basic, 2 button app (for android eventually)

Post by Reenen »

No Actually, on the phone the sounds work beautifully. It's on the PC that it doesn't want to play too frequently. I haven't got it running on the tablet yet, but working on that...
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Very basic, 2 button app (for android eventually)

Post by slime »

Ogg sound files are currently the best supported format for the official love builds.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Very basic, 2 button app (for android eventually)

Post by bartbes »

There's two things that are causing this, probably.
  1. You're mistaking Source objects for sounds, a Source can't play twice simultaneously.
  2. There's a bug with wav sounds that makes them 'play' for about 2 seconds after they have finished.
Oxin
Prole
Posts: 1
Joined: Sun Nov 27, 2011 11:07 am

Re: Very basic, 2 button app (for android eventually)

Post by Oxin »

Hi. I just found out about LOVE and I wanted to dive in and play around with some code and working on your simple app sounded like a good fit.

I used most of ghoulsblade suggestions to get started and then I used the TESound library along with a timer to get the sounds playing correctly.
Attachments
JaNee-edit.zip
(48.5 KiB) Downloaded 178 times
User avatar
Reenen
Prole
Posts: 44
Joined: Tue Nov 08, 2011 9:44 am

Re: Very basic, 2 button app (for android eventually)

Post by Reenen »

Hi there, thank you very much for the edit, it fixes it on windows!

Welcome to the community (much of which I am still very new to myself), but this is definitely an awesome place to be! :-D
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Bing [Bot] and 0 guests