0

I'm trying to move sprites to stop the frames in center(or move them to certain x position) when right or left pressed on screen. There are 3 sprites created using box.java in the view, placed one after another with padding, stored in arraylist.

The problem: No smooth movement and doesn't stop in the center of each frames after movement has begun, sometimes all boxes are moving on top of each others, padding is totally lost. Please let me know what I'm doing wrong, thanks a lot!

//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);
               }
           }
       }

//END OF BOX.JAVA

//VIEW

   //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

Alrite は、アニメーションをクラス自体に個別に追加する代わりに、onFling() メソッドを使用して View 経由で呼び出すことにしました。追加されたすべてのボックスのループで box.get(i).update() を呼び出すと、非常にうまく機能します。均等にアニメーション化されます。ありがとうウディボーイ。

于 2012-07-05T21:59:20.053 に答える
0

ifエラーはステートメントにあると思います. currentDirectionand lastDirection(私はそれlastDirectionが aであると仮定していますString) を演算子を使用して他Stringの sと比較し==ます. s が等しい==かどうかを比較する場合は、ほぼ演算子は機能しません。メソッドObjectを使用する必要があります。equals()

たとえば。

if(currentDirection != lastDirection)

次のように記述します。

if(!currentDirection.equals(lastDirection)

コードにそのような変更を加えてください(多くの場所で必要です!)、問題は解決するはずです。

適切なデバッグ プラクティスは、各 if ブロックからアプリに関するデータをログに記録し、それぞれが実行されているかどうかを確認することです。ifステートメントが実行されているかどうかを確認できたはずです。

編集:なぜこのコードを入れたのですか?

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

これは、onTouchEvents が 100ms 後にのみ解釈されることを意味します。それを削除して確認してください。おそらくそれが問題の原因です。

于 2012-07-05T16:46:54.363 に答える