Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (789)
Games in Android Showcase (234)
games submitted by our members
Games in WIP (864)
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 6568 times)
0 Members and 1 Guest are viewing this topic.
Offline ReBirth
« Posted 2012-08-25 13:47:43 »

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); = chromosome;
      this.value = value;
      this.regions[0] = region1;
      this.regions[1] = region2;
      this.setClickListener(new ClickListener(){
         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 Bitwise Duke »

Medals: 167
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 Bitwise Duke »

Medals: 167
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:


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  

hadezbladez (2617 views)
2018-11-16 13:46:03

hadezbladez (944 views)
2018-11-16 13:41:33

hadezbladez (2571 views)
2018-11-16 13:35:35

hadezbladez (509 views)
2018-11-16 13:32:03

EgonOlsen (3738 views)
2018-06-10 19:43:48

EgonOlsen (4136 views)
2018-06-10 19:43:44

EgonOlsen (2496 views)
2018-06-10 19:43:20

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

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

nelsongames (4376 views)
2018-04-24 18:14:32
Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08

Deployment and Packaging
by gouessej
2018-08-22 08:03:45 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!