Menu skipping

Discuss using and improving Lua and the Lua Player specific to the PSP.

Moderators: Shine, Insert_witty_name

Post Reply
Koba
Posts: 59
Joined: Thu Sep 29, 2005 10:57 am

Menu skipping

Post by Koba »

hey, i was just creating a basic menu for my game, and for some odd reason while on the first menu item, when i press down, it skips to the last menu item... there are 3 menu items total, here is my code for the up and down buttons.

Code: Select all

if pad:down() then
      if hl1 == 1 then
        hl1 = 2
        screen.waitVblankStart(10)
      end
      if hl1 == 2 then
        hl1 = 3
        screen.waitVblankStart(10)
      end
    end
    if pad:up() then
      if hl1 == 2 then
        hl1 = 1
        screen.waitVblankStart(10)
      end
      if hl1 == 3 then
        hl1 = 2
        screen.waitVblankStart(10)
      end
    end
hl1 is tracking which menu item is currently selected. i'v tryed a few option to fixing this but maybe i need an outsider's view. its probably somthing simplle that i'm just not seeing.
Ferrero
Posts: 15
Joined: Mon Dec 19, 2005 7:26 pm
Contact:

Post by Ferrero »

Hi,

I had the same problem with my menu to, but I found that when you hit a button, the event is raised many times (generaly 16 times) because of the speed of the loop of catching the events (each 1/60 ms).

So I've solved the problem with this code (my menu is a class) :

Code: Select all

        if ((self.up==1)and(control:up()==false)) then
		self.selectedText = self.selectedText - 1;
		if &#40;self.selectedText <= 0&#41; then 
			self.selectedText = 0; 
		end
	end
	
	if &#40;&#40;self.down==1&#41;and&#40;control&#58;down&#40;&#41;==false&#41;&#41;  then
		self.selectedText = self.selectedText + 1;
		if &#40;self.selectedText >= self.nbText&#41;  then 
			self.selectedText = self.nbText-1; 
		end
	end
	
	if &#40;control&#58;right&#40;&#41;&#41; then
		return self.selectedText;
	end
	
	if &#40;control&#58;up&#40;&#41;&#41; then
		self.up = 1;
	else
		self.up = 0;
	end
	
	if &#40;control&#58;down&#40;&#41;&#41; then
		self.down = 1;
	else
		self.down = 0;
	end
	
	return nil;
self.down and self.top are the states of the buttons.
selft.selectedText is the menu selected item

So, when you press the up button I put the button on, and when the button is pressed up, I change the selected menu line.

I've planned to manage a timer, to send a pressed up event when the user maintained the button pressed down while x ms
Slopey
Posts: 24
Joined: Sun Jul 31, 2005 8:13 pm

Re: Menu skipping

Post by Slopey »

Koba- your code is working correctly - if hl1 == 1 and you then set it to 2 then on the next if statement hl1 == 2 evaluates to true.

You'll need to use elseif rather than a new if statement ie:

Code: Select all

if pad&#58;down&#40;&#41; then
      if hl1 == 1 then
        hl1 = 2
        screen.waitVblankStart&#40;10&#41;
      elseif hl1 == 2 then
        hl1 = 3
        screen.waitVblankStart&#40;10&#41;
      end
    end
    if pad&#58;up&#40;&#41; then
      if hl1 == 2 then
        hl1 = 1
        screen.waitVblankStart&#40;10&#41;
      elseif hl1 == 3 then
        hl1 = 2
        screen.waitVblankStart&#40;10&#41;
      end
    end

Would also be worth adding some code so the menu "wraps" round on itself (ie down from 3 goes to 1), and just increment the hl1 variable rather than assigning it directly

Code: Select all

if pad&#58;down&#40;&#41; then
      hl1 = hl1 + 1
      if hl1 > 3 then
         hl1 = 1
      end
end
if pad&#58;up&#40;&#41; then
      hl1 = hl1 - 1
      if hl1 < 1 then
         hl1 = 3
      end
end
screen.waitVblankStart&#40;10&#41;
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

For an event driven Input system. (Does not toggle any repeats in input)
http://forums.ps2dev.org/viewtopic.php?t=4458

It can be located within the first page of the forum please learn to use the search engine.
Koba
Posts: 59
Joined: Thu Sep 29, 2005 10:57 am

Post by Koba »

romero126 wrote:For an event driven Input system. (Does not toggle any repeats in input)
http://forums.ps2dev.org/viewtopic.php?t=4458

It can be located within the first page of the forum please learn to use the search engine.
First of all, thank you very much you guys, for your help, and romero126, i use search before all my questions, and come up with absolutly nothing regarding to my topic. so sorry for bothering you, but apparently there ARE others on this board who are willing to help out a fellow programmer...
romero126
Posts: 200
Joined: Sat Dec 24, 2005 2:42 pm

Post by romero126 »

I was letting you know of a post not about a similar problem but about a fix with a library that was created to limit input presses to up and down functions only. Its not that Im trying to be negative towards new people but if the solution is within the grasp of somebody pushing them in the right direction is whats needed not repition of the same code over and over again. That is why the topic posted was within 4 spots away from the main board.

You just need to understand thats your problem which is why you where refrenced to that section vs me writing out the same code over and over again.

And in regards to the fellow programmer remark I find it fairly idiotic to take what I said as an insult to you as much as a clear statement on how to find the subject matter you are searching for. Fellow programmers dont write eachothers code regardless only offer simple solutions for common problems/mistakes.

Not trying to flame just giving you my thoughts on your comments.
Koba
Posts: 59
Joined: Thu Sep 29, 2005 10:57 am

Post by Koba »

ok, i realize you were trying to help me, and not flame me, but did i ever once ask for someone to rewrite my code? all i needed from it was the elseif, one tiny code niplet... but anyways, i'll try even harder to find what i am looking for in the forums next time. oh and btw, when i said "thank you very much you guys" that included you too, i only singled you out on the next comment.
Last edited by Koba on Wed Jan 11, 2006 4:05 pm, edited 1 time in total.
jimbo
Posts: 6
Joined: Thu Mar 31, 2005 2:30 am
Location: Sunny Southern CA
Contact:

Post by jimbo »

Try these:
The splashscreen just displays an image until you hit start.
The menuscreen has some movable components: a solid backdrop over the image to provide contrast and a movable selection bar controlled by up/down under a layer of text on transparency. Saving the last state of controls allows it to wait for distinct button presses, rather than just the state which can still be depressed from a previous menu.

Code: Select all

----------------------------------
-- Splash Screen
-- Just display an image until start is pressed
function splashScreen&#40;&#41;
	splashimg = Image.load&#40;"backgrounds/background1.png"&#41;  -- 480 x 272
	while not Controls.read&#40;&#41;&#58;cross&#40;&#41; do
		screen&#58;blit&#40;0, 0, splashimg, 0, 0, splashimg&#58;width&#40;&#41;, splashimg&#58;height&#40;&#41;, false&#41;
		screen.waitVblankStart&#40;&#41;
		screen.flip&#40;&#41;
	end
end

----------------------------------
-- Menu Screen
-- Display a menu of different options selectable using controls
-- Returns the index of the selected option
-- Possible parameters&#58;
--   background image
--   number of parameters
--   names of parameters
function menuScreen&#40;&#41;
	local numoptions  = 2
	local optionnames = &#123;&#125;
	optionnames&#91;1&#93; = "Execute"
	optionnames&#91;2&#93; = "Edit Mode"

	local bgoffx      = 100
	local bgoffy      = 80
	local baroffx     = 30
	local baroffy     = 20
	local baroffskip  = 30
	local optiontxtoffx = 5
	local optiontxtoffy = 5

	-- imaging
	local green1      = Color.new&#40;0, 64, 0&#41;
	local optionbgcol = Color.new&#40;64, 64, 64&#41;
	local optionfgcol = Color.new&#40;255,255,255&#41;
	local splashimg   = Image.load&#40;"backgrounds/background2.png"&#41;
	local selectbar   = Image.createEmpty&#40;100,20&#41;
	local optionbg    = Image.createEmpty&#40;200,100&#41;
	local optiontxt   = Image.createEmpty&#40;200,100&#41;

	-- paint components colors to their images
	optionbg&#58;fillRect&#40;0, 0, optionbg&#58;width&#40;&#41;,optionbg&#58;height&#40;&#41;, optionbgcol&#41;
	selectbar&#58;fillRect&#40;0, 0, selectbar&#58;width&#40;&#41;,selectbar&#58;height&#40;&#41;, green1&#41;
	-- Paint Menu Options to a clear image
	for i =	1, numoptions do
		optiontxt&#58;print&#40;baroffx + optiontxtoffx, baroffy + baroffskip*&#40;i-1&#41; + optiontxtoffy,  optionnames&#91;i&#93;, optionfgcol&#41;
	end

	-- add offsets for moving selection bar
	local baroffx     = baroffx + bgoffx
	local baroffy     = baroffy + bgoffy

	-- State variables
	local selopt     = 0
	local keep_going = true
	local menuPush   = Controls.read&#40;&#41; -- state of controls
	local LastPush   = menuPush
	while keep_going do
		LastPush = menuPush
		menuPush = Controls.read&#40;&#41;

		if menuPush&#58;up&#40;&#41;    and not LastPush&#58;up&#40;&#41;    then  selopt = math.mod&#40; selopt - 1 + numoptions, numoptions&#41;  end
		if menuPush&#58;down&#40;&#41;  and not LastPush&#58;down&#40;&#41;  then  selopt = math.mod&#40; selopt + 1 + numoptions, numoptions&#41;  end
		if menuPush&#58;cross&#40;&#41; and not LastPush&#58;cross&#40;&#41; then  keep_going = false                                       end

		screen&#58;blit&#40;0, 0, splashimg, 0, 0, splashimg&#58;width&#40;&#41;, splashimg&#58;height&#40;&#41;, false&#41;
		-- draw option selector bar under text
		screen&#58;blit&#40;bgoffx, bgoffy, optionbg, 0, 0, optionbg&#58;width&#40;&#41;,optionbg&#58;height&#40;&#41;, true&#41;
		screen&#58;blit&#40;baroffx, baroffy + selopt * baroffskip, selectbar, 0, 0, selectbar&#58;width&#40;&#41;,selectbar&#58;height&#40;&#41;, true&#41;
		-- draw option text layer
		screen&#58;blit&#40;bgoffx, bgoffy, optiontxt, 0, 0, optiontxt&#58;width&#40;&#41;,optiontxt&#58;height&#40;&#41;, true&#41;

		screen.waitVblankStart&#40;&#41;
		screen.flip&#40;&#41;
	end
	return selopt
end

Post Reply