So I wrote some code that gets the x and y from an ontouch event, passes it to a WebView javascript function and then from there (for the sake of this explanation) puts the box where it was touched...
After a battle and a half, I worked out that if I put x/2 and y/2 the co-ordinates would be perfect.. questions:
1) Why on earth is this happening?! I tried getting the width and height comparison from js and android, and it is the same width and height for both..
2) Is there any way to tidy this up?
3) Will it work like this on any other device (with different dimensions)?
Code:
HTML
function menuClick(x, y){
document.getElementById("jumprequested").innerHTML = "X: "+x+", Y: "+y;
document.getElementById("jumprequested").style.top = y/2;
document.getElementById("jumprequested").style.left = x/2;
theclick = document.elementFromPoint(x/2, y/2);
if(theclick !== "" && theclick.className == "number"){
loadLevel(theclick.outerText);
}
}
Android Code
myWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
gettheY = event.getY();
gettheX = event.getX();
if(event.getAction() == MotionEvent.ACTION_DOWN){
if(menuToggle == true){
myWebView.loadUrl("javascript:menuClick("+gettheX+","+gettheY+")");
}
else {
if(gettheY<halfWidth){
myWebView.loadUrl("javascript:touchDown(1)");
}
else {
myWebView.loadUrl("javascript:touchDown(0)");
}
}
}
if(event.getAction() == MotionEvent.ACTION_UP){
if(menuToggle == false){
if(gettheX - jumpHeight > event.getX()){
//jump
}
else if (event.getY()<halfWidth){
myWebView.loadUrl("javascript:touchUp(1)");
}
else{
myWebView.loadUrl("javascript:touchUp(0)");
}
}
}
return true;
}
});