Home Game Development libgdx – Pass enter occasion from stage to added actor

libgdx – Pass enter occasion from stage to added actor

0
libgdx – Pass enter occasion from stage to added actor

[ad_1]

I’m diving into libgdx and used their pattern recreation as a place to begin.

I additionally added a touchpad to maneuver the bucket. That was form of simple and labored flawless.
My further concept was to make the touchpad disappear when not touching/clicking the display and be proven once more when you contact it. Basically to provide the participant the liberty to determine the place he desires to have the touchpad situated on the display.

Watching this recording it really works fairly good however clearly I do not need to make it comply with the mouse consistently. So I attempted to make the touchpad (which is an actor on my stage) invisible. As quickly as I do this, I’m dropping the contact occasion being forwarded to the touchpad. See this recording.

My preliminary concept was to not have the pad consistently comply with the mouse pointer (as I need to help contact screens in some unspecified time in the future too). So the best method I attempted however did not implement was so as to add the touchpad on any “down” occasion and and “transfer” occasion afterwards is dealt by the touchpad however that doesn’t work. The stage retains the first goal of the enter occasion and the touchpad will not be knowledgeable about something.
I attempted it with an InputMultiplexer however failed with it as nicely…

Any concepts?

My foremost class:

import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.OrthographicCamera
import com.badlogic.gdx.graphics.Texture
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.Touchpad
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable
import com.badlogic.gdx.utils.ScreenUtils


class MySport : ApplicationAdapter() {
    non-public lateinit var pad: Touchpad
    non-public lateinit var stage: Stage
    non-public lateinit var digicam: OrthographicCamera

    override enjoyable create() {
        // create the digicam and the SpriteBatch
        digicam = OrthographicCamera().apply {
            setToOrtho(false, 800f, 480f)
        }

        stage = Stage()
        stage.addListener(object : InputListener() {
            override enjoyable touchDown(occasion: InputEvent?, x: Float, y: Float, pointer: Int, button: Int): Boolean {
                println("tdown")
                pad.isVisible = true
                return tremendous.touchDown(occasion, x, y, pointer, button)
            }

            override enjoyable mouseMoved(occasion: InputEvent?, x: Float, y: Float): Boolean {
                println("mmoved")
                if (!pad.isVisible) {
                    pad.x = x - pad.width / 2f
                    pad.y = y - pad.peak / 2f
                }
                return tremendous.mouseMoved(occasion, x, y)
            }

            override enjoyable touchUp(occasion: InputEvent?, x: Float, y: Float, pointer: Int, button: Int) {
                println("tup")
                pad.isVisible = false
                tremendous.touchUp(occasion, x, y, pointer, button)
            }
        })
        Gdx.enter.inputProcessor = stage;

        val padBackground = TextureRegionDrawable(Texture(Gdx.recordsdata.inside("pad_circle.png")))
        val padThumb = TextureRegionDrawable(Texture(Gdx.recordsdata.inside("pad_thumb.png")))
        pad = Touchpad(10f, Touchpad.TouchpadType(padBackground, padThumb))
        pad.addListener(object : InputListener() {
            override enjoyable enter(occasion: InputEvent?, x: Float, y: Float, pointer: Int, fromActor: Actor?) {
                println("pad enter")
                pad.isVisible = true
                tremendous.enter(occasion, x, y, pointer, fromActor)
            }

            override enjoyable exit(occasion: InputEvent?, x: Float, y: Float, pointer: Int, toActor: Actor?) {
                pad.isVisible = false
                println("pad exit")
                tremendous.exit(occasion, x, y, pointer, toActor)
            }
        })
        pad.isVisible = false
        stage.addActor(pad)
    }

    override enjoyable render() {
        ScreenUtils.clear(0f, 0f, 0.2f, 1f)
        digicam.replace()

        stage.act(Gdx.graphics.deltaTime)
        stage.draw()
    }

    override enjoyable dispose() {
    }
}

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here