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
Branches
No related tags found
1 merge request!2Resolve "Design buttons where user jumps/slides"
Pipeline #212416 passed
...@@ -3,6 +3,7 @@ package tdt4240.netrunner ...@@ -3,6 +3,7 @@ package tdt4240.netrunner
import com.badlogic.gdx.ApplicationAdapter import com.badlogic.gdx.ApplicationAdapter
import com.badlogic.gdx.Gdx import com.badlogic.gdx.Gdx
import com.badlogic.gdx.Input import com.badlogic.gdx.Input
import com.badlogic.gdx.InputMultiplexer
import com.badlogic.gdx.graphics.Texture import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.BitmapFont import com.badlogic.gdx.graphics.g2d.BitmapFont
import com.badlogic.gdx.graphics.g2d.SpriteBatch import com.badlogic.gdx.graphics.g2d.SpriteBatch
...@@ -20,18 +21,27 @@ class Netrunner : ApplicationAdapter() { ...@@ -20,18 +21,27 @@ class Netrunner : ApplicationAdapter() {
private lateinit var img: Texture private lateinit var img: Texture
private var position: Vector2 = Vector2(300f, 0f) private var position: Vector2 = Vector2(300f, 0f)
private var speed: Float = 10.0f private var speed: Float = 10.0f
private lateinit var buttonUp: SimpleButton
private lateinit var buttonDown: SimpleButton
public var inputMultiplexer = InputMultiplexer()
override fun create() { override fun create() {
Gdx.input.inputProcessor = inputMultiplexer
batch = SpriteBatch() batch = SpriteBatch()
img = Texture("whiteball.png") 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() { override fun render() {
ScreenUtils.clear(1f, 0f, 0f, 1f) ScreenUtils.clear(1f, 0f, 0f, 1f)
batch.begin() batch.begin()
batch.draw(img, position.x, position.y) batch.draw(img, position.x, position.y)
move(1.0f) buttonUp.update(batch)
buttonDown.update(batch)
batch.end() batch.end()
} }
...@@ -57,5 +67,12 @@ class Netrunner : ApplicationAdapter() { ...@@ -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 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.Texture
import com.badlogic.gdx.graphics.g2d.Sprite import com.badlogic.gdx.graphics.g2d.Sprite
import com.badlogic.gdx.graphics.g2d.SpriteBatch 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){ init {
skin = Sprite(texture) sprite.setPosition(x, y)
skin.setPosition(x, y) sprite.setSize(width, height)
skin.setSize(width, height) inputMultiplexer.addProcessor(this)
} }
public fun update(batch: SpriteBatch, input_x: Float, input_y: Float){ public fun update(batch: SpriteBatch) {
checkIfClicked(input_x, input_y); sprite.draw(batch);
skin.draw(batch);
} }
private fun checkIfClicked(ix: Float, iy: Float) { override fun touchDown(screenX: Int, screenY: Int, pointer: Int, button: Int): Boolean {
if (ix > skin.x && ix < skin.x + skin.width) { val fixedScreenY = Gdx.graphics.height - screenY
if (iy > skin.y && iy < skin.y + skin.height) { println("$screenX,$screenY,$fixedScreenY")
// the button was clicked, perform an action if (screenX >= sprite.x && screenX <= sprite.x + sprite.width
println("Button clicked !") && 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