0

当在屏幕上向右或向左按下时,我正在尝试移动精灵以将帧停止在中心(或将它们移动到某个 x 位置)。视图中有 3 个使用 box.java 创建的精灵,一个接一个地放置,带有填充,存储在 arraylist 中。

问题:没有平滑的移动,并且在移动开始后不会在每个帧的中心停止,有时所有的框都在彼此之上移动,填充完全丢失。请让我知道我做错了什么,非常感谢!

//BEGINING OF BOX.JAVA >> The problem is in this class!

//This goes in Update();
private void boxMove()
{
           int get_moved_pos = getMovedPos();   //get moved pos
           int sprite_size = view.getSpriteSize(); //get sprite arraylist size
           currentDirection = view.getDirection(); //get direction "left" or "right" from view

                if(currentDirection == "right" && isMoving == false)
                {
                        setSpriteMovedNext();
                }else
                if(currentDirection == "left" && isMoving == false)
                {
                        setSpriteMovedPrev();
                }

                if(currentDirection != lastDirection)
                {
                            lastDirection = currentDirection;

                            //MOVE RIGHT
                            if(currentDirection == "right" && get_moved_pos > 0)    //move left and make sure that moved pos isn't overlapping / or moving to empty space
                            {

                                //Animate left until it reaches the new x position                  
                                if(x > get_new_pos_left)
                                {
                                    x -= pSpeedX;
                                }

                                Log.d("RIGHT","POS: " + get_moved_pos);

                            }else

                            //MOVE LEFT
                            if(currentDirection == "left" && get_moved_pos < sprite_size-1) //move left and make sure that moved pos isn't overlapping / or moving to empty space
                            {
                                //Animate right until it reaches the new x position                 
                                if(x < get_new_pos_right)
                                {
                                    x += pSpeedX;
                                }

                            }
                }


}

        //Call when screen is touched (in View.java), to set a new position to move to.
       public void resetMoving()
       {
           isMoving = false;
            this.lastDirection      = "";
           Log.d("RESET", "MOVING RESET");
       }

       public int getMovedPos()
       {
           return this.smoved_pos;
       }


       private void setSpriteMovedNext()
       {
           int get_max_moved = getMovedPos();
           int s_size = view.getSpriteSize();

           if (isMoving == false) //take a break between movements
           {
                if(get_max_moved < s_size-1)
                {
                    Log.d("NEXT", "CALLED");
                    this.get_new_pos_right  = x + view.getNextPosX();   //current x and next stop position
                    this.smoved_pos     += 1;
                    this.isMoving       = true; //set to avoid double touch
                    Log.d("NEXT", "X POS SET: " + get_max_moved);
                }
           }
       }

       private void setSpriteMovedPrev()
       {
           int get_max_moved = getMovedPos();

           if (isMoving == false) //take a break between movements
           {
               if(get_max_moved > 0)
               {
                   Log.d("PREV", "CALLED");
                   this.get_new_pos_left    = x - view.getNextPosX(); //get current x pos and prev stop position
                   this.smoved_pos          -= 1;   //to limit the movements
                   this.isMoving            = true; //set to avoid double touch
                   Log.d("PREV", "X POS SET: " + get_max_moved);
               }
           }
       }

//BOX.JAVA结束

//看法

   //Add boxes
   public void addBox()
   {
       int TOTAL_BOXES = 3;
       int padding_left = 200;
       int padding_tmp =  this.getWidth()/2;

       box.clear(); //clear old

       //Box 1
       box.add(new Boxes(box, this, "box1", 
               padding_tmp,
               this.getHeight()/2, 
               boxSpriteImage, 1, 2, 0, 0));       

       padding_tmp += boxSpriteImage.getWidth()/TOTAL_BOXES + padding_left;

       //Box 2
       box.add(new Boxes(box, this, "box2", 
               padding_tmp, 
               this.getHeight()/2, 
               boxSpriteImage, 1, 2, 1, 1));       

       padding_tmp += boxSpriteImage.getWidth()/TOTAL_BOXES + padding_left;

       //Box 3
       box.add(new Boxes(box, this, "box3", 
               padding_tmp, 
               this.getHeight()/2, 
               boxSpriteImage, 1, 2, 2, 1));       

   }

public boolean onTouchEvent(MotionEvent event){
     if (System.currentTimeMillis() - lastClick > 100){
            lastClick = System.currentTimeMillis();
            float x = event.getX();
            float y = event.getY();
            synchronized (getHolder()) 
            {

            if(isBoxWindow() == true)
            {

                if(x >= this.getWidth()/2)
                {
                    Direction = "right";
                }else
                {
                    Direction = "left";
                } 
    }
        }
 }

//called in box.java to get next x pos to move
public float getNextPosX()
{
       int PADDING = 200; //padding between frames
       next_pos_x = boxSprite.getWidth()/TOTAL_COLUMNS + PADDING;
       return next_pos_x;
}
4

2 回答 2

0

我认为您的错误出在if语句中,您使用运算符将currentDirection​​ and lastDirection(我假设lastDirection是 a String)与其他Strings 进行比较==。当==您想比较Objects 是否相等时,几乎运算符永远不会起作用。您应该使用该equals()方法。

例如。

if(currentDirection != lastDirection)

应该写成:

if(!currentDirection.equals(lastDirection)

在您的代码中进行此类更改(很多地方都需要它们!)我认为您的问题应该得到解决。

一个好的调试实践是从每个 if 块中记录有关您的应用程序的数据,以查看它们是否都已执行。您可能已经发现您的if语句是否正在执行。

编辑:你为什么放这个代码?

if (System.currentTimeMillis() - lastClick > 100)

这意味着onTouchEvents 仅在 100 毫秒后被解释。删除它并检查,可能这就是导致问题的原因。

于 2012-07-05T16:46:54.363 回答
0

Alrite 决定使用 onFling() 方法并通过 View 调用而不是将动画单独添加到类本身中,当在所有添加的框的循环中调用 box.get(i).update() 时效果非常好,所有这些框动画一样。谢谢 udiboy。

于 2012-07-05T21:59:20.053 回答