Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (755)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (842)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Own buttons with events - is this ok?  (Read 2254 times)
0 Members and 1 Guest are viewing this topic.
Offline dime26

JGO Ninja

Medals: 65
Projects: 7
Exp: 12 years

Should traffic wardens be armed?

« Posted 2014-02-24 15:39:32 »

I have created some simple buttons for an Android game and wanted to run the idea/logic by the forum to get feedback. I have classes which have vars like:

Button Class
float x, y, width, height;
Rectanlge hitbox;
String event;

Touch Class
float touch_x, touch+y;
Rectanlge hitbox;
boolean handled;

A simple screen might have 4 buttons that are put into an array, when the screen is pressed the x and y are captured and handled set to false, I then loop the button array checking for intersects and if true set menu_action to the button.event and set handled back to true.

I would handle this event and set some other boolean or string to false or "" so I know that event has been handled. When handled is true there is no need to loop the button array.

I could use scene2D but it felt overkill for such a simple task and I should imagine it deals with inputs in some similar way, checking each button that is listening?

This does work in my app I would just like some pro's or con's of doing this, is this bad practice?

Offline JVallius
« Reply #1 - Posted 2014-02-24 15:53:29 »

Once you learn scene2d it is easy to use. Do not reinvent the wheel if you do not have to.

Offline BurntPizza

« JGO Bitwise Duke »

Medals: 485
Exp: 7 years

« Reply #2 - Posted 2014-02-24 16:03:30 »

The x, y, width, and height variables are redundant if you store a rectangle.
All you need is:
rect.contains(x, y);
rect.contains(Point p);

Touch could either have an x and y or extend Point, adding the 'handled' field. No reason for a rectangle or intersects().

You are correct in using a 'handled' flag, it is used to prevent gui elements from handling events when they are underneath another element. This only works if the elements are collision-checked in order.

If that is guaranteed to never be a scenario, it is 'overkill.'

Also, don't use "" strings (or null objects) to represent a boolean state. Just use a boolean.

Past this much functionality, you might as well use scene2d, or utilize Swing if applicable, i.e. using JComponent/JPanel and MouseEvent/MouseListener.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline dime26

JGO Ninja

Medals: 65
Projects: 7
Exp: 12 years

Should traffic wardens be armed?

« Reply #3 - Posted 2014-02-25 01:38:09 »

Thanks for the tips, the rectangle on they touched area is pointless so can remove that. I generally use booleans but will make a mental note not to dp otherwise.

Scene2D does look easy I was just wondering if its needed for such simple menus. I will give it a go at some point. 
Pages: [1]
  ignore  |  Print  

DesertCoockie (52 views)
2018-05-13 18:23:11

nelsongames (83 views)
2018-04-24 18:15:36

nelsongames (74 views)
2018-04-24 18:14:32

ivj94 (759 views)
2018-03-24 14:47:39

ivj94 (87 views)
2018-03-24 14:46:31

ivj94 (643 views)
2018-03-24 14:43:53

Solater (102 views)
2018-03-17 05:04:08

nelsongames (184 views)
2018-03-05 17:56:34

Gornova (426 views)
2018-03-02 22:15:33

buddyBro (1086 views)
2018-02-28 16:59:18
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!