Skip to content
Snippets Groups Projects
Commit d5218f04 authored by karinlie's avatar karinlie
Browse files

fikset knapp og slik at den bevegde seg

parent 8eb8ead5
No related branches found
No related tags found
1 merge request!2Resolve "Design buttons where user jumps/slides"
Pipeline #212416 passed
......@@ -3,6 +3,7 @@ package tdt4240.netrunner
import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch
......@@ -20,18 +21,27 @@ class Netrunner : ApplicationAdapter() {
private lateinit var img: Texture
private var position: Vector2 = Vector2(300f, 0f)
private var speed: Float = 10.0f
private lateinit var buttonUp: SimpleButton
private lateinit var buttonDown: SimpleButton
public var inputMultiplexer = InputMultiplexer()
override fun create() {
Gdx.input.inputProcessor = inputMultiplexer
batch = SpriteBatch()
img = Texture("whiteball.png")
buttonUp = SimpleButton(inputMultiplexer, Texture("up.png"), 550f, 100f,60f,60f)
{ moveUp() }
buttonDown = SimpleButton(inputMultiplexer, Texture("down.png"), 550f, 20f,60f,60f)
{ moveDown() }
}
override fun render() {
ScreenUtils.clear(1f, 0f, 0f, 1f)
batch.begin()
batch.draw(img, position.x, position.y)
move(1.0f)
buttonUp.update(batch)
buttonDown.update(batch)
batch.end()
}
......@@ -57,5 +67,12 @@ class Netrunner : ApplicationAdapter() {
}
}
}
fun moveUp() {
position.y += speed
}
fun moveDown() {
position.y -= speed
}
}
\ No newline at end of file
package tdt4240.netrunner
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.InputProcessor
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.Sprite
import com.badlogic.gdx.graphics.g2d.SpriteBatch
class SimpleButton() {
class SimpleButton(inputMultiplexer: InputMultiplexer, texture: Texture, x: Float, y: Float, width: Float, height: Float, callback: () -> Unit) : InputProcessor {
private lateinit var skin: Sprite
private var sprite = Sprite(texture)
private var callback = callback
fun SimpleButton(texture: Texture, x: Float, y: Float, width: Float, height: Float){
skin = Sprite(texture)
skin.setPosition(x, y)
skin.setSize(width, height)
init {
sprite.setPosition(x, y)
sprite.setSize(width, height)
inputMultiplexer.addProcessor(this)
}
public fun update(batch: SpriteBatch, input_x: Float, input_y: Float){
checkIfClicked(input_x, input_y);
skin.draw(batch);
public fun update(batch: SpriteBatch) {
sprite.draw(batch);
}
private fun checkIfClicked(ix: Float, iy: Float) {
if (ix > skin.x && ix < skin.x + skin.width) {
if (iy > skin.y && iy < skin.y + skin.height) {
// the button was clicked, perform an action
println("Button clicked !")
override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
val fixedScreenY = Gdx.graphics.height - screenY
println("$screenX,$screenY,$fixedScreenY")
if (screenX >= sprite.x && screenX <= sprite.x + sprite.width
&& fixedScreenY >= sprite.y && fixedScreenY <= sprite.y + sprite.height) {
callback()
return true
}
return false
}
override fun touchUp(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
return false
}
override fun touchDragged(screenX: Int, screenY: Int, pointer: Int): Boolean {
return false
}
override fun keyDown(keycode: Int): Boolean {
return false
}
override fun keyUp(keycode: Int): Boolean {
return false
}
override fun keyTyped(character: Char): Boolean {
return false
}
override fun mouseMoved(screenX: Int, screenY: Int): Boolean {
return false
}
override fun scrolled(amountX: Float, amountY: Float): Boolean {
return false
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment