Page 1 of 1

Very basic, 2 button app (for android eventually)

Posted: Mon Nov 14, 2011 12:52 pm
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

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

Posted: Mon Nov 14, 2011 2:02 pm
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

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

Posted: Mon Nov 14, 2011 2:19 pm
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.

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

Posted: Mon Nov 14, 2011 2:47 pm
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)...

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

Posted: Mon Nov 14, 2011 9:56 pm
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.

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

Posted: Tue Nov 15, 2011 5:13 am
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...

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

Posted: Tue Nov 15, 2011 5:41 am
by slime
Ogg sound files are currently the best supported format for the official love builds.

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

Posted: Tue Nov 15, 2011 9:31 am
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.

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

Posted: Sun Nov 27, 2011 11:20 am
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.

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

Posted: Mon Nov 28, 2011 6:46 am
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