如何在两个视图不正确时自动翻转它们
Posted
技术标签:
【中文标题】如何在两个视图不正确时自动翻转它们【英文标题】:How to flip two views when their incorrect automatically 【发布时间】:2021-09-15 18:28:08 【问题描述】:我正在寻找创建一个记忆游戏,我现在正在研究这个功能。我已经大致了解了项目的逻辑,但我想补充一点细节。我想补充的细节之一是,当两张选择的卡片不正确时,它们会在 1 秒后自行转动。我尝试使用 Handler().postDelay 但它似乎没有用。
这里是代码
class GamePlay1Fragment : Fragment()
// lateinit var front_anim: AnimatorSet
// lateinit var back_anim: AnimatorSet
private lateinit var pieces: List<ImageView>
private lateinit var gameCards: List<GameCard>
private var indexOfSelectedPiece: Int? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
val binding: Gameplay1FragmentBinding = DataBindingUtil.inflate(
inflater,
R.layout.gameplay1_fragment,
container, false)
binding.backButtonView.setOnClickListener v: View ->
v.findNavController().navigate(GamePlay1FragmentDirections.actionGamePlay1FragmentToLobbyFragment())
val images = mutableListOf(memorybatcardfront, memorycatcardfront, memorycowcardfront,
memorydragonfront, memorygarbagemancardfront, memoryghostdogcardfront)
images.addAll(images)
images.shuffle()
pieces = listOf(binding.card1back, binding.card2back, binding.card3back, binding.card4back, binding.card5back,binding.card6back, binding.card7back,
binding.card8back, binding.card9back, binding.card10back, binding.card11back, binding.card12back)
gameCards = pieces.indices.map index ->
GameCard(images[index])
pieces.forEachIndexed index, piece ->
piece.setOnClickListener
updatingModels(index)
updatingViews()
return binding.root
private fun updatingViews()
gameCards.forEachIndexed index, gameCard ->
val piece = pieces[index]
piece.setImageResource(if (gameCard.isFacedUp) gameCard.id else allcardbacks)
private fun updatingModels(position: Int)
val gameCard = gameCards[position]
if (gameCard.isFacedUp) return
if (indexOfSelectedPiece == null)
restoreGameCards()
indexOfSelectedPiece = position
else
checkingForMatch(indexOfSelectedPiece!!, position)
indexOfSelectedPiece = null
gameCard.isFacedUp = !gameCard.isFacedUp
private fun restoreGameCards()
val handler = Handler()
for (gameCard in gameCards)
if (!gameCard.isMatched)
handler.postDelayed(runnable, 1000)
gameCard.isFacedUp = false
private fun checkingForMatch(position1: Int, position2: Int)
if (gameCards[position1].id == gameCards[position2].id)
gameCards[position1].isMatched = true
gameCards[position2].isMatched = true
private val runnable = Runnable()
kotlin.run
如果有其他方法可以做到这一点,我会全力以赴。欣赏它。
【问题讨论】:
【参考方案1】:问题是您在调用处理程序时同步翻转卡片(即它们同时执行),但您需要在处理程序延迟结束时翻转卡片。
要解决此问题,您需要在Handler
的Runnable
内传输卡片翻转:
private fun restoreGameCards()
val handler = Handler(Looper.getMainLooper())
for (gameCard in gameCards)
if (!gameCard.isMatched)
handler.postDelayed(
gameCard.isFacedUp = false // Executes after 1000 millisec
, 1000)
【讨论】:
它确实有效,但不是我想要的。延迟确实会发生,但现在您可以在所有被点击的卡片面朝下之前点击多张卡片。 所以,你需要绑定用户点击多张卡片后的延迟..以及用户完成的时候;我猜你需要翻转这些卡片? 对。翻转第二张牌后,如果没有匹配,则在两张牌翻回之前有 1 秒的延迟。以上是关于如何在两个视图不正确时自动翻转它们的主要内容,如果未能解决你的问题,请参考以下文章