2012-05-28 73 views
0

我想添加平移和縮放,以我的應用程序已經寫在processing.js,這裏是我的代碼:添加平移和縮放誤差

boolean surfaceTouchEvent(MotionEvent event) { 
pointNum=event.getPointerCount(); 
switch (event.getAction() & MotionEvent.ACTION_MASK) { 
case MotionEvent.ACTION_DOWN: 
//User is pressing on finger 
float x0=event.getX(0); 
float y0=event.getY(0); 
mode = false; //DRAG 
break; 
case MotionEvent.ACTION_POINTER_DOWN: 
x1=event.getX(0);x2=event.getX(1); 
y1=event.getY(0);y2=event.getY(1); 
float z4 = dist(x1,y1,x2,y2); 
mode = true; // pinch 
break; 
case MotionEvent.ACTION_POINTER_UP: 
// User is released one of the fingers. 

break; 
case MotionEvent.ACTION_MOVE: 
if (mode = false){ 
x1=event.getX(0)-x0; 
y1=event.getY(0)-y0; 
} 
if (mode = true){ 
x1=event.getX(0);x2=event.getX(1); 
y1=event.getY(0);y2=event.getY(1); 
float z3 = dist(x1,y1,x2,y2); 
if (z3 > z4){ 
zoom += 0.1; 
} 
else if (z3 < z4){ 
zoom -= 0.1; 
} 
} 
break; 
} 
return super.surfaceTouchEvent(event); 
} 

我萬一MotionEvent.ACTION_POINTER_DOWN保存舊的位置爲縮放並在大小寫的情況下使用MotionEvent.ACTION_DOWN。我需要這個舊的位置比較舊距離新的距離在放大例如

這是錯誤的:

compile: 
    [javac] Compiling 2 source files to C:\DOCUME~1\T0P4568\LOCALS~1\Temp\android893850823480544580.pde\bin\classes 
    [javac] C:\DOCUME~1\T0P4568\LOCALS~1\Temp\android893850823480544580.pde\src\changethispackage\beforesubmitting\tothemarket\sketch_may28a\sketch_may28a.java:118: variable x0 might not have been initialized 
    [javac] x1=event.getX(0)-x0; 
    [javac]    ^
    [javac] C:\DOCUME~1\T0P4568\LOCALS~1\Temp\android893850823480544580.pde\src\changethispackage\beforesubmitting\tothemarket\sketch_may28a\sketch_may28a.java:119: variable y0 might not have been initialized 
    [javac] y1=event.getY(0)-y0; 
    [javac]    ^
    [javac] C:\DOCUME~1\T0P4568\LOCALS~1\Temp\android893850823480544580.pde\src\changethispackage\beforesubmitting\tothemarket\sketch_may28a\sketch_may28a.java:125: variable z4 might not have been initialized 
    [javac] if (z3 > z4){ 
    [javac]   ^
    [javac] 3 errors 

請幫助!

回答

0

好的,你很明顯正在使用processing.org,它將代碼編譯爲java。

解決方案很簡單:預初始化x0,y0z4爲默認值,例如, 0

這只是爲了安撫java編譯器,它不夠聰明,無法理解它們被讀取的時間,它們已經被初始化了。