我有Actor
这是ShapeRenderer
绘制的圆圈,但是当我拖动它时,它的边界移动错误。
其边界的范围(绿色调试行)也是错误的。我认为这是由Batch
ad ShapeRenderer
之间的错误转换矩阵引起的,但不确定如何修复它。
主要课程:
class Main : ApplicationAdapter() {
lateinit var stage: Stage
lateinit var sr: ShapeRenderer
lateinit var bubble: Bubble
override fun create() {
stage = Stage(ExtendViewport(800f,500f))
sr = ShapeRenderer()
Gdx.input.inputProcessor = stage
bubble = Bubble(sr)
bubble.debug = true
stage.addActor(bubble)
}
override fun render() {
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT)
stage.act()
stage.draw()
}
override fun resize(width: Int, height: Int) {
stage.viewport.update(width,height,true)
}
override fun dispose() {
sr.dispose()
stage.dispose()
}
}
演员&班级:
class Bubble(val sr: ShapeRenderer) : Actor() {
val defaultRadius: Float = 50f
var xPos: Float = 0f
var yPos: Float = 0f
var radius: Float = defaultRadius
init {
addListener(object: InputListener() {
override fun touchDown(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int): Boolean {
radius = defaultRadius * 1.2f
return true
}
override fun touchUp(event: InputEvent?, x: Float, y: Float, pointer: Int, button: Int) {
radius = defaultRadius / 1.2f
}
override fun touchDragged(event: InputEvent?, x: Float, y: Float, pointer: Int) {
xPos = x
yPos = y
}
})
}
override fun act(delta: Float) {
setBounds(xPos,yPos,radius*2,radius*2)
}
override fun draw(batch: Batch?, parentAlpha: Float) {
batch!!.end()
sr.projectionMatrix = batch.projectionMatrix
sr.transformMatrix = batch.transformMatrix
sr.translate(x, y, 0f)
sr.begin(ShapeRenderer.ShapeType.Filled)
sr.color = Color.RED
sr.circle(xPos, yPos, radius)
sr.end()
batch.begin()
}
}