Heya !
I'm working on a 'lil watch app (device-app) and I'm encountering an issue when the user drags on the screen.
I want to find out the direction of the drag ( x or y ) and change some variables according to it while the user is dragging. The code I have so far works perfectly, but if the user drags multiple times or in quick succession, the watch crashes with no debug output; just a blue triangle.
My code is below -- I've tried to keep it as simple as possible, but it is a bit nested. Is it the case that the watch can't process fast enough or is it an issue with my code ?
Thanks !!
[ P.S. It's not in a code block because whenever inserting one, the website tells me that my IP has been blocked, even though that is not the case. Maybe it's too much code ? ]
Delegate.mc:
class Delegate extends WatchUi.BehaviorDelegate {
// ...
function onDrag(dragEvent) {
if(!gameStarted) {
switch(dragEvent.getType()) {
case 0: // New tap
startX = dragEvent.getCoordinates()[0];
startY = dragEvent.getCoordinates()[1];
System.println(dragEvent.getCoordinates()[0] + ", " + dragEvent.getCoordinates()[1]);
break;
case 1: // Dragging
deltaX = (dragEvent.getCoordinates()[0] - startX);
deltaY = (dragEvent.getCoordinates()[1] - startY) * -1; // Y-Axis is inverted.
switch(axis) {
case "N":
/*
.abs function doesn't work with "Number/Method" variables -- deltaX.toNumber().abs doesn't work either.
absX and absY are just placeholder variables representing the absolute value. This is my solution until I find something more elegant
*/
if(deltaX < 0) {
absX = deltaX * -1;
} else {
absX = deltaX;
}
if(deltaY < 0) {
absY = deltaY * -1;
} else {
absY = deltaY;
}
if(absX > swipeThreshold || absY > swipeThreshold) {
System.println(deltaY + ", " + deltaX);
if(absX > absY) {
axis = "X";
} else {
axis = "Y";
}
}
break;
case "X":
System.println("x: " + dragEvent.getCoordinates()[0]);
// TODO: Add Increment for every N units in the x-axis
break;
case "Y":
System.println("y: " + dragEvent.getCoordinates()[1]);
// TODO: Add Time for every N units in the y-axis
break;
}
break;
case 2: // End drag
axis = "N";
break;
}
} else if(dragEvent.getType() == 0) { // Ease of use; An accidental drag while the game is going shouldn't disrupt the game. [ The watch screen is already hard enough to tap . . ]
changeSidesAndAddIncrement();
}
return true;
}
}
// ...