为什么Image()在屏幕上移动,跳到默认位置,每次单击屏幕,然后从默认位置开始移动?

时间:2019-06-03 18:11:03

标签: animation kotlin libgdx actor scene2d

我有一个简单的演示项目。图像沿着屏幕移动并跟随鼠标指针。

LibGDX Image实例每次我在屏幕上单击时,都会在MainMenuScreen.kt show方法内部确定的情况下,沿x轴跳转到默认的x坐标,并从默认位置开始移动。但我希望Image在单击屏幕之前会从上一个位置继续/开始新的移动。如何解决,什么问题?

代码简单,简短,我不明白有什么问题。

pastebin链接到完整的项目代码: https://github.com/3dformortals/demo-libgdx/tree/master/DemoMovingImageOnScreen

github链接到项目: enter image description here

完整的项目代码:

//-------
//KDA.kt
//-------

package com.kda

import com.badlogic.gdx.Game
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.GL20

import gui.AnimationSkin as AniSkin

class KDA : Game() {
    internal var screenWidth:Float = 0.0f
    internal var screenHeight:Float = 0.0f
    internal val aniskin:AniSkin = AniSkin() //incuded human.atlas TextureAtlas for animation

    override fun create() {
        screenWidth = Gdx.graphics.width.toFloat()
        screenHeight = Gdx.graphics.height.toFloat()
        aniskin.prepare() //call preparing method for connect human.atlas for later using for animation
    }

    override fun render() {
        Gdx.gl.glClearColor(1f, 0f, 0f, 1f)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

        if (Gdx.input.justTouched()){
            setScreen(MainMenuScreen(this))
        }
        super.render()
    }
}


//-------------------
//AnimationSkin.kt
//-------------------

package gui

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.g2d.TextureAtlas
import com.badlogic.gdx.scenes.scene2d.ui.Skin

class AnimationSkin : Skin() {
    fun prepare(){
        addRegions(TextureAtlas(Gdx.files.internal("animation/human.atlas")))
    }
}


//----------------------
//MainMenuScreen.kt
//----------------------

package com.kda

import animated.ImageMoving
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.ScreenAdapter
import com.badlogic.gdx.graphics.GL20
import com.badlogic.gdx.scenes.scene2d.Stage

import com.badlogic.gdx.utils.viewport.FitViewport

class MainMenuScreen(private val game: KDA) : ScreenAdapter() {
    private val stage: Stage = Stage(FitViewport(game.screenWidth, game.screenHeight))
    private val player = ImageMoving(game)
    private val sprite = player.viewBox()

    override fun show() {
        Gdx.input.inputProcessor = stage
        stage.isDebugAll = true //turn on frames around objects
        sprite.x = 500f
        //------------------------------------------------------------------------------------
        //later, every mouse click on screen sprite jump to x500 position, and i can't fix it
        //if i don't execute `sprite.x = 500f` , then sprite jump to x0 position, every click on screen
        //--------------------------------------------------------------------------------------------
        stage.addActor(sprite)
    }

    override fun resize(width: Int, height: Int) {
        stage.viewport.update(width, height, true)
    }

    override fun render(delta: Float) {
        super.render(delta)
        Gdx.gl.glClearColor(0f, 0.5f, 0.5f, 1f)
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
        if(Gdx.input.justTouched()) println("before calculateAction box.x= "+sprite.x.toString()) //500 always
        player.calculateAction(delta) //call player method for calculation moving on screen
        println(sprite.x) //print normal as expected
        stage.act(delta)
        stage.draw()

    }
}


//-----------------
//ImageMoving.kt
//-----------------

package animated

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.scenes.scene2d.ui.Image
import com.kda.KDA

class ImageMoving(game: KDA) {
    fun viewBox() = img
    private val img = Image(game.aniskin.getDrawable("move-skin-male-back-R-0"))
    fun calculateAction(delta:Float){
        if (img.x > Gdx.input.x) img.x-=(100*delta).toInt().toFloat()
        else if (img.x < Gdx.input.x) img.x+=(100*delta).toInt().toFloat()
    }
}


//----------------------
//DesktopLauncher.kt
//---------------------

package com.kda.desktop

import com.badlogic.gdx.backends.lwjgl.LwjglApplication
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration
import com.kda.KDA

object DesktopLauncher {
    @JvmStatic
    fun main(arg: Array<String>) {
        val config = LwjglApplicationConfiguration()
        config.height = 720
        config.width = 1280
        LwjglApplication(KDA(), config)
    }
}

在点击屏幕后将图像跳转到默认位置x=500的gif动画演示
web resource

0 个答案:

没有答案