- Home /
Android - Normalize touch position based on mobile screen size
Hello there,
What I am trying to do for the latest hours is to normalize the position I get from touch.position to make it work on multiple devices with different aspect ratios and resolutions.
The code I have (doesn't work) is here:
function TouchYWorld (touch : Touch) {
touch.position.y = (touch.position.y / Screen.height) * frustumHeight; //height is 320
return touch.position.y;
}
However I get a warning that tells me assignment is temporary, which I can't really explain why it happens.
I have also tried using a temporary value:
var temp = touch.position;
temp.x = (touch.position.x / Screen.width) * frustumWidth;
touch.position.x = temp.x;
Answer by hvilela · Aug 27, 2012 at 04:22 PM
You're not suposed to change the original values from the "touch" variable.
Save the value to a new variable or return it directly:
function TouchYWorld (touch : Touch) {
var y = (touch.position.y / Screen.height) * frustumHeight;
return y;
}
function TouchYWorld (touch : Touch) {
return (touch.position.y / Screen.height) * frustumHeight;
}
Your answer
Follow this Question
Related Questions
Best Way to make Swipe to Look function via touch. (Android) 0 Answers
Swipe control for circle (Can do swipe left/right) 2 Answers
Help with touchscreen controls 2 Answers
touch position to world position 2D 2 Answers
Limit Android Screen Aspect Ratio 0 Answers