您如何检测用户何时将手指拖过特定对象
How do you detect when the user drags their finger over a specific object
我需要检测用户何时将手指拖过特定对象,并在发生这种情况时 运行 编写代码。我有一个由许多正方形组成的网格,用户将拖动他们的角色,一个 ImageView,穿过网格,有点像迷宫,但会带来中断路径的后果。我需要检测用户的手指或字符何时越过这些方块中的任何一个,然后 运行 基于该方块颜色的函数。当用户与那个方块碰撞时,该函数应该只 运行 一次,只要角色在它上面,就会 而不是连续 。希望这张应用程序和网格的图像也能有所帮助。 The grid for the app is shown here
我已经解决了这个问题并创建了我自己的碰撞盒系统。这是我的功能。每次您想检查碰撞时都应该重复调用该函数,或者循环该函数并在代码开头调用它一次。但是,还有一个问题没有解决。我仍然无法弄清楚如何检测该图块的颜色以及基于该颜色(可以更改)的 运行 特定功能。而且我可能需要这个函数的 91 个副本,因为我有 91 个图块需要这些 hitboxes。无论如何,这是我想出的:
private fun collideDetect2() {
val warningColor = "#FF8C00" // part of a testing tool to help test my hardcoded hitboxes
val fixColor = "#00ED8E"
val playerX = player.x
val playerY = player.y
val tileX = tile69.x // tile69 is one of the objects I need to detect collision for on the grid of squares
val tileY = tile69.y
if (playerX <= (tileX + 155f)) {
if(playerX >= (tileX - 85f)) {
println("X collision detected.") //helped with debugging and testing
println("player's X is $playerX and tile's X is $tileX") //helped with debugging and testing
if (playerY >= (tileY + 75f)) {
if(playerY <= (tileY + 275f)) {
println("Y collision detected.")
println("player's Y is $playerY and tile's Y is $tileY")
if (!activated) { // activated was defined at the top of the file, "var activated = false" This variable allows the function to only run once when the collision is detected until the objects are no longer overlapping, instead of running every moment they are overlapping while it is being checked
activated = true
highlightRandomTiles() // the function I want it to run when the collision is detected for this specific tile
when ((1..2).random()) { // testing tool to tell me when the player image has overlapped. without the "activated" variable, it would tell me every single moment the hitbox was overlapping
2 -> {
tile69.setBackgroundColor(warningColor.toColorInt())
}
1 -> {
tile69.setBackgroundColor(fixColor.toColorInt())
}
}
}
}
else {
activated = false // allows the code to know the player has left the hitbox so the function will run again next time the collision occurs. Might not need this in all 4 places tho...
}
}
else {
activated = false
}
}
else {
activated = false
}
}
else {
activated = false
}
}
我需要检测用户何时将手指拖过特定对象,并在发生这种情况时 运行 编写代码。我有一个由许多正方形组成的网格,用户将拖动他们的角色,一个 ImageView,穿过网格,有点像迷宫,但会带来中断路径的后果。我需要检测用户的手指或字符何时越过这些方块中的任何一个,然后 运行 基于该方块颜色的函数。当用户与那个方块碰撞时,该函数应该只 运行 一次,只要角色在它上面,就会 而不是连续 。希望这张应用程序和网格的图像也能有所帮助。 The grid for the app is shown here
我已经解决了这个问题并创建了我自己的碰撞盒系统。这是我的功能。每次您想检查碰撞时都应该重复调用该函数,或者循环该函数并在代码开头调用它一次。但是,还有一个问题没有解决。我仍然无法弄清楚如何检测该图块的颜色以及基于该颜色(可以更改)的 运行 特定功能。而且我可能需要这个函数的 91 个副本,因为我有 91 个图块需要这些 hitboxes。无论如何,这是我想出的:
private fun collideDetect2() {
val warningColor = "#FF8C00" // part of a testing tool to help test my hardcoded hitboxes
val fixColor = "#00ED8E"
val playerX = player.x
val playerY = player.y
val tileX = tile69.x // tile69 is one of the objects I need to detect collision for on the grid of squares
val tileY = tile69.y
if (playerX <= (tileX + 155f)) {
if(playerX >= (tileX - 85f)) {
println("X collision detected.") //helped with debugging and testing
println("player's X is $playerX and tile's X is $tileX") //helped with debugging and testing
if (playerY >= (tileY + 75f)) {
if(playerY <= (tileY + 275f)) {
println("Y collision detected.")
println("player's Y is $playerY and tile's Y is $tileY")
if (!activated) { // activated was defined at the top of the file, "var activated = false" This variable allows the function to only run once when the collision is detected until the objects are no longer overlapping, instead of running every moment they are overlapping while it is being checked
activated = true
highlightRandomTiles() // the function I want it to run when the collision is detected for this specific tile
when ((1..2).random()) { // testing tool to tell me when the player image has overlapped. without the "activated" variable, it would tell me every single moment the hitbox was overlapping
2 -> {
tile69.setBackgroundColor(warningColor.toColorInt())
}
1 -> {
tile69.setBackgroundColor(fixColor.toColorInt())
}
}
}
}
else {
activated = false // allows the code to know the player has left the hitbox so the function will run again next time the collision occurs. Might not need this in all 4 places tho...
}
}
else {
activated = false
}
}
else {
activated = false
}
}
else {
activated = false
}
}