Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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  
  [solved] [libgdx] ClickListener fails  (Read 1980 times)
0 Members and 1 Guest are viewing this topic.
Offline ReBirth
« Posted 2012-08-25 13:47:43 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
public class MyClass extends Image{
   
   private TextureRegion[] regions = new TextureRegion[2];
   
   private Chromosome chrome;
   private int value;
   
   public MyClass(Chromosome chromosome, TextureRegion region1, TextureRegion region2, int value) {
      super(region1);
      this.chrome = chromosome;
      this.value = value;
      this.regions[0] = region1;
      this.regions[1] = region2;
      this.setRegion(regions[value]);
      this.setClickListener(new ClickListener(){
         @Override
         public void click(Actor actor, float x, float y) {
            // TODO Auto-generated method stub
           System.out.println(x + " " + y);
         }
      });
   }
}

The println doesn't triggered. This class already added to screen's stage. Any idea?

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #1 - Posted 2012-08-26 04:45:30 »

Give the actor a size.

Offline ReBirth
« Reply #2 - Posted 2012-08-26 05:03:29 »

So it appears that I have to give the region's dimension (width and height) to Image's ImageWidth, ImageHeight, width, and height. Thanks!

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #3 - Posted 2012-08-27 07:19:28 »

That is one way of setting the size, yes.

An actor doesn't set its own size, instead the size is always set externally. If an actor implements Layout (which Image and all of scene2d.ui do) then the actor provides a minimum, preferred, and maximum size. These are hints that can be used by something external to size the actor. Often "something external" is a Table. Tables make everything super easy.

When not using tables or other groups that size and position children, the pack method is very useful. This method sets the width and height of the widget to the preferred width and height, calls invalidate if the widget's size was changed, and then calls validate so that the widget adjusts itself to the new size.

You could just call pack, since the preferred size of an Image is the size of the texture region (or ninepatch, sprite, drawable, etc). Often this is what you want to do to adjust an actor's size when not using Table, Stack, VerticalGroup, etc. However, there is a small gotcha if you need to do this in an actor's constructor. From the Layout#pack() javadocs: "Generally this method should not be called in a constructor because it calls {@link #layout()}, which means a subclass would have layout() called before the subclass' constructor. Instead, in a constructor simply sets the actors width and height to {@link #getPrefWidth()} and {@link #getPrefHeight()}. This allows the actor to have a size at construction time for more convenient use outside of a {@link Table}." Because of this, all scene2d.ui widgets do this in their constructor:

1  
2  
setWidth(getPrefWidth());
setHeight(getPrefHeight());

Normally Image does this very thing and you don't have to worry about it, but since you are using setRegion, you will have to do it yourself. Calling pack in your constructor is fine if you don't subclass your actor.

Separate from all that, note you can pass null to the Image constructor if you don't want to specify an Drawable there.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (35 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org 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‑gaming.org
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!