Android - 移动单个图像

问题描述:

任何人都可以帮忙。Android - 移动单个图像

我写一个小的Android游戏,玩家能够选择一个“屏障”,将其拖动进行的跨用他们的手指在屏幕上。我在屏幕上绘制了屏障,我可以在屏幕上拖动它。 然而,我的问题是,当我添加多于1个屏障(例如3个屏障),并在屏幕上拖动屏障时,它们都会拖动并拖动到同一位置。也就是说,他们全都躺在彼此的顶部,看起来好像只有1个障碍。

这里是我的代码,任何人都可以请告诉我,我错了/解释我要去的地方错了。

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback, SensorEventListener { 

// Initialising the Barrier 
private Barrier barrier[] = new Barrier[3]; 


// The Main Game Panel 
public MainGamePanel(Context context) { 
    super(context); 

// Adding a call-back (this) to the surfaceHolder to intercept events 
getHolder().addCallback(this); 

// Creating the Game items 
    // The starting coordinates of the Barrier 
int x = 30; 
int y = 270; 
barrier[0] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.blue_barrier), x, y); 
barrier[1] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.green_barrier), x + 15, y); 
barrier[2] = new Barrier(BitmapFactory.decodeResource(getResources(), R.drawable.pink_barrier), x + 30, y); 

// Create the Game Loop Thread 
thread = new MainThread(getHolder(), this); 

// Make the GamePanel focusable so it can handle events 
setFocusable(true); 
} 

// Handles the touch events 
public boolean onTouchEvent(MotionEvent event) 
{ 
     int eventAction = event.getAction(); 

    int x = (int)event.getX(); 
    int y = (int)event.getY(); 

    switch (eventAction) 
    { 
    // Touch down so check if finger is on Barrier 
    case MotionEvent.ACTION_DOWN: 
     if (x > barrier[0].getX() && x < barrier[0].getX() + 8 
       && y > barrier[0].getX() && y < barrier[0].getY() + 8) 
     { 
      barrier[0].isTouched(); 
     } 
     else if (x > barrier[1].getX() && x < barrier[1].getX() + 8 
       && y > barrier[1].getX() && y < barrier[1].getY() + 8) 
     { 
      barrier[1].isTouched(); 
     } 
     else if (x > barrier[2].getX() && x < barrier[2].getX() + 8 
       && y > barrier[2].getX() && y < barrier[2].getY() + 8) 
     { 
      barrier[2].isTouched(); 
     } 

     break; 

    // Touch-drag with the Barrier 
    case MotionEvent.ACTION_MOVE: 

    // Move the Barrier the same as the finger 
     for (int i = 0; i < barrier.length; i++) 
     { 
      if (barrier[i] == barrier[0]) 
      { 
       barrier[0].setX(x); 
       barrier[0].setY(y); 
      } // end if 
      else if (barrier[i] == barrier[1]) 
      { 
       barrier[1].setX(x); 
       barrier[1].setY(y); 
      } 
      else if (barrier[i] == barrier[2]) 
      { 
       barrier[2].setX(x); 
       barrier[2].setY(y); 

      } // end else if 
     } // end for 
     break; 

    case MotionEvent.ACTION_UP: 
     // Finger no longer on Barrier - Do Nothing 
     break; 
    } 
    return true; 
} 


// Render - Draws the Game Item Bitmaps to the screen 
public void render(Canvas canvas) 
{ 
    // Set the background to white 
    canvas.drawColor(Color.WHITE); 

    barrier[0].draw(canvas); 
    barrier[1].draw(canvas); 
    barrier[2].draw(canvas); 
} 


// Update 
// This is the Game's update method 
// It iterates through all the Objects and calls their update() methods (if they have one) 
public void update() 
{ 
} // end update 

在你的屏障移动的代码,你不检查一个特定的屏障被触摸,让你在所有的人都移动到同一坐标。下面的循环不完全一样的东西作为当前代码:

// Move the Barrier the same as the finger 
for (int i = 0; i < barrier.length; i++) 
    { 
     barrier[i].setX(x); 
     barrier[i].setY(y); 
    } //end for 

要解决这个问题,你需要检查,如果在回路中的电流势垒是被感动的人,并且可以改变整个循环的东西像:

// Move the Barrier the same as the finger 
for (int i = 0; i < barrier.length; i++) 
    { 
     if (barrier[i].isTouched()) 
     { 
      barrier[i].setX(x); 
      barrier[i].setY(y); 
     } // end if 
    } // end for 

你会那么需要确保您未设置在ACTION_UP部分触摸性能。如果您发布Barrier类定义,我可能会帮助更多。

+0

嗨jozzas。好吧,我试过你的解决方案,但仍然没有运气某处我出错了,仍然不知道在哪里。也许它在我的Barrier类中。 IVE包括阻挡类这里:\t \t} \t \t别的 \t \t { \t \t \t setTouched(假); \t \t} //结束还有 \t}} – heyred 2011-03-04 18:27:44

我敢肯定你的问题可以通过将障碍,而不是三个独立的对象数组来解决。

+0

是的,我试过,但仍然有同样的问题。我以我的智慧结束了这一切。 – heyred 2011-03-03 23:28:36

+0

这没有意义......它们是3个类型的实例,只是存储在一个数组中。这根本不会改变行为。 – 2011-03-04 02:04:31

前面已经指出的那样,你在处理移动代码是错误的,因为它会将所有壁垒相同的X,Y,当它应该只能移动被触摸的一个。

而且,你永远不会重置在操作了对象isTouched。当用户抬起他们的手指时,你应该将他们全部设置为isTouched == false。如果你不这样做,那么一旦你触摸它,它总是会移动到X,Y。