【问题标题】:Sliding chess pieces in an Android game在 Android 游戏中滑动棋子
【发布时间】:2015-02-28 06:43:03
【问题描述】:

我在 Android 中编写了一个类似国际象棋的益智游戏,大部分已完成。我现在想做的是添加一些图形效果来改善我的游戏的感觉和外观,首先是改善棋子的移动。

我已将我的主要游戏棋盘(棋盘)构造为 GridLayoutTextViews,以便将棋子表示为国际象棋字体中的字母(并且通过调用将正方形着色为黑色或白色) setBackgroundResource TextView)。每个TextView 都有一个onClickListener,这样当用户点击相应的方格时,TextViews 上的字母就会根据国际象棋规则发生变化。这意味着图形感觉有些不自然:移动的棋子从原来的方格消失,并立即重新出现在它移动到的方格上。我想通过让棋子从原来的方格滑到新方格来改变这一点。有什么方法可以在保持 GridLayout-of-TextViews 逻辑的同时做到这一点 - 还是我需要大幅改变我的代码结构?无论如何,如果有人知道如何开始,我很想听听!

【问题讨论】:

    标签: android textview android-gridlayout


    【解决方案1】:

    试试这样的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtOne = (TextView)findViewById(R.id.textone);
        txtTwo = (TextView)findViewById(R.id.text_two);
        relative = (RelativeLayout)findViewById(R.id.relative);
    
    
        txtTwo.setOnClickListener(new View.OnClickListener() {
    
            @SuppressLint("NewApi")
            @Override
            public void onClick(View v) {
    
    
    
                float to_x = txtOne.getX();
                float to_y = txtOne.getY();
    
    
                float x_from = txtTwo.getX();
                float y_from = txtTwo.getY();
                //txtTwo.getLocationInWindow(fromLoc);   
                txtOne.getLocationOnScreen(toLoc);
                Animations anim = new Animations();
    
                Animation a = anim.fromAtoB(x_from, y_from, -to_x, -to_y, animL,1000);
                txtTwo.startAnimation(a);
    
            }
        });
    }
    
    
    
    public class Animations {
        public Animation fromAtoB(float fromX, float fromY, float toX, float toY, AnimationListener l, int speed){
    
    
            Animation fromAtoB = new TranslateAnimation(
    
                    fromX, 
    
                    toX, 
    
                    fromY, 
    
                    toY
                    );
    
            fromAtoB.setDuration(speed);
            //fromAtoB.setInterpolator(new );
    
    
            if(l != null)
                fromAtoB.setAnimationListener(l);               
            return fromAtoB;
        }
    }
    
    
    
    AnimationListener animL = new AnimationListener() {
    
        @Override
        public void onAnimationStart(Animation animation) {     
        }
    
        @Override
        public void onAnimationRepeat(Animation animation) {        
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
            //this is just a method to delete the ImageView and hide the animation Layout until we need it again.
            //clearAnimation();       
        }
    };
    

    【讨论】:

    • 谢谢。现在我正在尝试理解这段代码。 “toLoc”是什么变量?
    • 这将是您将作品移动到的文本视图。
    • 好的,但是我们如何使用它呢?上面代码中唯一调用变量的地方是txtOne.getLocationOnScreen(toLoc); 行,它返回一个位置,但我们似乎没有对它做任何事情。还是我在这里误解了什么?
    • 我还想知道:如果我正确理解了代码块,我们在这里所做的是将TextView 从一个位置移动到另一个位置,而不是TextView 中包含的文本。这意味着整个方格被移动,但方格中的棋子没有移动。这是正确的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    相关资源
    最近更新 更多