android - JavaScript 代码在 Android 上无法正常工作
问题描述
我正在用 QML 编写一个跳棋游戏,我有这个组件,它包含玩家部分和它的鼠标区域。
当按下鼠标并且用户移动鼠标时,下面的单元格会以黄色突出显示。这和捕捉在 Ubuntu 上有效,但是当我在 Android 上测试它时,似乎只有拖动部分有效。
这是播放器部分的代码:
Component {
id: playerPiece
Image {
id: image
property int cx: 0
property int cy: 0
x: board.x + (cx * window.cellSize)
y: board.y + (cy * window.cellSize)
property int pieceIndex: -1
property int pieceType: 0
MouseArea {
id: ma
anchors.fill: parent
enabled: GameState.playerTurnId == 1 && (pieceType & 1) == 1
drag.target: parent
drag.axis: Drag.XAxis | Drag.YAxis
property int targetCellX: -1
property int targetCellY: -1
onReleased: {
// console.log("released")
if (GameState.lastHighlightedIndex != -1
&& GameState.lastHighlightedY != -1) {
board.children[GameState.lastHighlightedY].children[GameState.lastHighlightedX].children[0].visible = false
if (targetCellX != -1 && targetCellY != -1) {
console.log(targetCellX)
var piece = GameState.playerPieceQMLItems[image.pieceIndex]
console.log(`cx ${targetCellX} cy ${targetCellY}`)
piece.cx = targetCellX
piece.cy = targetCellY
//hack to recalculate positions
board.x++
board.x--
board.y++
board.y--
targetCellX = -1
targetCellY = -1
}
}
}
onPositionChanged: {
if (drag.active) {
// console.log("dragging")
var mousePos = NativeFunctions.globalMousePos()
targetCellX = Math.floor(
(mousePos.x - window.x - board.x) / window.cellSize)
targetCellY = Math.floor(
(mousePos.y - window.y - board.y) / window.cellSize)
if ((targetCellX > -1 && targetCellX < 8) && (targetCellY > -1
&& targetCellY < 8)) {
GameState.tileState[targetCellX + (targetCellY * 8)] = 3
//update the board row column child item
var item = board.children[targetCellY].children[targetCellX]
if (item instanceof Rectangle) {
//remove the highlight from the last highlighted cell
if (GameState.lastHighlightedIndex != -1
&& GameState.lastHighlightedY != -1) {
board.children[GameState.lastHighlightedY].children[GameState.lastHighlightedX].children[0].visible = false
}
item.children[0].visible = true
//console.log(`selected ${GameState.playerPieceQMLItems[image.pieceIndex]}, pieceIndex ${image.pieceIndex}`)
//console.log(`${item.children[1].id}`)
GameState.lastHighlightedX = targetCellX
GameState.lastHighlightedY = targetCellY
}
}
}
}
}
sourceClipRect: {
if (GameState.cf(pieceType, GameState.TS_P1)) {
//king flag set
if (GameState.cf(pieceType, GameState.TS_PK)) {
return Qt.rect(0, 326, 338, 338)
} else {
return Qt.rect(0, 0, 338, 338)
}
} else if (GameState.cf(pieceType, GameState.TS_P2)) {
//king flag set
if (GameState.cf(pieceType, GameState.TS_PK)) {
return Qt.rect(0, 326, 338, 338)
} else {
return Qt.rect(534, 0, 338, 338)
}
}
}
source: "qrc:/pieces.png"
}
}
这是https://github.com/ben-cottrell-nz/checkers/blob/master/main.qml的一部分。
当我在 Android 上测试我的应用程序时,为什么突出显示和捕捉功能不起作用?
解决方案
我发现我的方法从 C++ 中暴露出来,这NativeMethods::globalMousePos
是问题所在:在 Android 上它为 x 和 y 返回 -2147483648,这是从QCursor::pos
.
我在 QML 代码中更改了 mousePos 以使用内置函数 mapToGlobal:
var mousePos = mapToGlobal(mouse.x, mouse.y)
突出显示和对齐矩形现在可以在 Android 上使用。
推荐阅读
- java - 以有效方式导出 XML 中的特殊字符
- python - 为什么spritecillide在pygame中工作不正确
- javascript - 关于使用递归和使用变量的问题
- python - 在运行时将包目录显式添加到 sys.path 后找不到模块
- python - 当 RMSLE 是评估指标时,lightgbm 的早期停止不起作用
- python - 撤消或重做选项创建opencv图像处理(Python)
- node.js - 均衡。GetOne 查询文本中具有 hasMany-association 的未知列
- ios - 解码后改变图像的方向
- c# - 如何在 Azure Web App 中禁用 web.config 重写规则?
- java - 为什么我的第二种方法的模拟会影响第一种方法?