Friday 15 August 2014

libgdx table-layout enter event fires excessively -



libgdx table-layout enter event fires excessively -

i'm trying add together sound plays when user hovers mouse cursor on button. problem if there within button, sound plays twice: 1 time when cursor enters button, , 1 time when enters (or exits) thing within button. there way can create come in event fire when cursor enters button outside , not inside?

example code:

package com.mygdx.game; import com.badlogic.gdx.applicationadapter; import com.badlogic.gdx.gdx; import com.badlogic.gdx.audio.sound; import com.badlogic.gdx.graphics.gl20; import com.badlogic.gdx.graphics.texture; import com.badlogic.gdx.graphics.g2d.textureatlas; import com.badlogic.gdx.scenes.scene2d.actor; import com.badlogic.gdx.scenes.scene2d.inputevent; import com.badlogic.gdx.scenes.scene2d.inputlistener; import com.badlogic.gdx.scenes.scene2d.stage; import com.badlogic.gdx.scenes.scene2d.ui.button; import com.badlogic.gdx.scenes.scene2d.ui.image; import com.badlogic.gdx.scenes.scene2d.ui.skin; import com.badlogic.gdx.scenes.scene2d.ui.table; public class mygdxgame extends applicationadapter { textureatlas atlas; skin skin; image img; sound entersound; stage stage; table table; button button; public void create () { atlas = new textureatlas("ui.pack"); skin = new skin(gdx.files.internal("skin.json"), atlas); img = new image(new texture("badlogic.jpg")); entersound = gdx.audio.newsound(gdx.files.internal("sound.wav")); stage = new stage(); table = new table(skin); table.setfillparent(true); stage.addactor(table); button = new button(skin); button.addlistener(new inputlistener() { @override public void enter(inputevent event, float x, float y, int pointer, actor fromactor) { entersound.play(); } }); button.add(img).size(64); table.add(button).size(128); gdx.input.setinputprocessor(stage); } @override public void render () { gdx.gl.glclearcolor(1, 0, 0, 1); gdx.gl.glclear(gl20.gl_color_buffer_bit); stage.act(gdx.graphics.getdeltatime()); stage.draw(); } }

i've found similar problem cells contain element on mouse-enter listened.. event mouse come in has been triggering 3 times (2 times on parent cells , on element).

you such trick:

// update in listener button.addlistener(new inputlistener() { final button thisbutton = button; @override public void enter(inputevent event, float x, float y, int pointer, actor fromactor) { if (currentmouseoveredbutton == null || !currentmouseoveredbutton.equals(thisbutton)){ // mark button has made sound currentmouseoveredbutton= thisbutton; entersound.play(); } }); // somewhere in ui class button currentmouseoveredbutton;

libgdx

No comments:

Post a Comment