Missing texture after add new actors

by Alcadur   Last Updated May 15, 2019 11:13 AM - source

I want to add two types of characters multiple times but when I add a new actor to stage texture in "old" actors stops render.

I can add multiple KnightActor (without RatActor) or multiple RatActor (without KnightActor). When I try to add multiple KnightActor and RatActor on one stage only textures from newest actors are rendered (one RatActor and one KnightActor)

GameScreen.kt

override fun render(delta: Float) {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)

    actorManager.addNewActors(stage)

    viewPort.camera.update()

    stage.act(delta)
    stage.draw()

    world.step(delta, 8, 3)
    debugger.render(world, viewPort.camera.combined)
}

ActorManager.kt

fun markToAdd(actor: BaseActor<*>) {
    toAdd.add(actor)
}

fun addNewActors(stage: Stage) {
    toAdd.forEach { actor: BaseActor<*> ->
        stage.addActor(actor)
        actor.addToWorld(worldService.getWorld())
        Gdx.app.log("afterAdd", (++charactersOnStage).toString())
    }
    toAdd.clear()
}

KnightActor.kt

override fun draw(batch: Batch, parentAlpha: Float) {
    super.draw(batch, parentAlpha)

    if (isMoving) {
        frameStateTime += Gdx.graphics.deltaTime
    }

    // DRAW TEXTURE
    val frame = animationManager.getFrameAndChangeSpeed(AnimationType.KNIGHT_WALK_RIGHT, frameStateTime, getSpeed())
    val frameX: Float = position.x - (size.width / 2)
    val frameY: Float = position.y - (size.height / 2)

    batch.draw(frame, frameX, frameY, size.width, size.height)

    // END DRAW
}

RatActor.kt

override fun draw(batch: Batch, parentAlpha: Float) {
    super.draw(batch, parentAlpha)

    if (isMoving) {
        frameStateTime += Gdx.graphics.deltaTime
    }

    // DRAW TEXTURE
    val frame = animationManager.getFrameAndChangeSpeed(AnimationType.RAT_WALK_LEFT, frameStateTime, getSpeed())
    val frameX: Float = position.x - (size.width / 2)
    val frameY: Float = position.y - (size.height / 2)

    batch.draw(frame, frameX, frameY, size.width, size.height)

    // END DRAW
}

AnimationManager.kt

fun getAnimation(animationType: AnimationType): Animation<TextureRegion> {
    return AssetsManager.loadSprite(animationType.spriteFile, animationType.alias).getAnimationForSprite(animationType.alias, animationType.width, animationType.height)
}
fun getFrameAndChangeSpeed(animationType: AnimationType, stateTime: Float, speed: Float, looping: Boolean = true): TextureRegion {
    val animation = getAnimation(animationType)
    animation.frameDuration = 1 / speed
    return animation.getKeyFrame(stateTime, looping)
}


Related Questions


Inner shadow of a polygon in libgdx

Updated September 09, 2018 07:13 AM

How to manage the z-index in two Stage?

Updated December 14, 2017 15:13 PM

Do I need to dispose Pixmap in Actor?

Updated August 14, 2018 12:13 PM

What is wrong with intersectLinePolygon method?

Updated August 16, 2018 08:13 AM

Is there any better way to interact with HUD?

Updated September 08, 2018 07:13 AM