繁体   English   中英

我如何使我的代码更短

[英]how do i make my code shorter

嗨,我做了一个小形状的游戏,我的if语句开始失控,所以我做了一个更简单的测试场景游戏,试图简化代码,但是我对Java和android的体验不像id那样,所以尝试一些我以为id的问题,我该如何使代码更小? 目前,我正在将onTouchListeners和onDragListeners用于一些形状,atm 3种彩色形状和3种空白“空形状”,并且当它们通过将一个放置在另一个之上而连接时,空的形状就会变成彩色。 但是需要大量的代码

@Override
public boolean onTouch(View v, MotionEvent e) {
    if (e.getAction() == MotionEvent.ACTION_DOWN) {
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
        v.startDrag(null, shadowBuilder, v, 0);
        sp.play(dragSound, 1, 1, 0, 0, 1);
        return true;
    } else {
        return false;
    }
}

//WHEN DRAGGED AND DROPPED

@Override
public boolean onDrag(View v, DragEvent e) {

    if (e.getAction()==DragEvent.ACTION_DROP) {
        View view = (View) e.getLocalState();

        //IF THEY MATCH

        if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
        {
            ViewGroup from = (ViewGroup) view.getParent();
            ViewGroup to = (ViewGroup) findViewById(R.id.layout2);
            View congrats = findViewById(R.id.top_layout);
            ViewGroup two = (ViewGroup) findViewById(R.id.layout3);

            from.removeView(view);
            v.setBackgroundResource(R.drawable.dragsquare);
            sp.play(dropSound, 1, 1, 0, 0, 1);
            sb.setVisibility(View.VISIBLE);

            imageView = (ImageView)findViewById(R.id.imageView);
            imageView.setVisibility(View.VISIBLE);

            if (to.getChildCount()< 1 && two.getChildCount()< 1)

            {
                congrats.setVisibility(View.VISIBLE);
                imageView.setBackgroundResource(R.drawable.congrats);
                sun=(AnimationDrawable)imageView.getBackground();
                sun.start();

                sp.play(tada, 1, 1, 0, 0, 1);
                congrats.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                      View congrats
(RelativeLayout)findViewById(R.id.top_layout);
                        congrats.setVisibility(View.INVISIBLE);

                    }
                });

            }

//2 square balloons floating


            sb = (ImageView)findViewById(R.id.squareballoon);
            sb.setVisibility(View.VISIBLE);

            sb2 = (ImageView)findViewById(R.id.squareballoon2);
            sb2.setVisibility(View.VISIBLE);

            sp.play(inflate, 1, 1, 0, 0, 1);

            ObjectAnimator sqbalAnim3=
 ObjectAnimator.ofFloat(sb2,"x",-500,500);
            sqbalAnim3.setDuration(700);
            sqbalAnim3.setRepeatCount(20);
            sqbalAnim3.setRepeatMode(ValueAnimator.REVERSE);

            ObjectAnimator sqbalAnim =   
ObjectAnimator.ofFloat(sb2,"y",1800,-1800);
            sqbalAnim.setDuration(3000);
            sqbalAnim.setRepeatMode(ValueAnimator.RESTART);

            AnimatorSet animSetXY = new AnimatorSet();
            animSetXY.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    sb2.setVisibility(View.GONE);
                }
            });
            animSetXY.playTogether(sqbalAnim, sqbalAnim3);
            animSetXY.setStartDelay(20);
            animSetXY.start();


            ObjectAnimator sqbal2Anim =
 ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"y",-450);
            sqbal2Anim.setDuration(3000);
            sqbal2Anim.setRepeatMode(ValueAnimator.RESTART);

            ObjectAnimator sqbalAnim4 =
 ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"x",650,750);
            sqbalAnim4.setStartDelay(20);
            sqbalAnim4.setDuration(300);
            sqbalAnim4.setRepeatCount(6);
            sqbalAnim4.setRepeatMode(ValueAnimator.REVERSE);

            AnimatorSet animSetXY2 = new AnimatorSet();
            animSetXY2.playTogether(sqbal2Anim,sqbalAnim4);
            animSetXY2.start();

            return true;}

        //end of square balloons


        else if(view.getId()==R.id.circleImage &&
 v.getId()==R.id.circleImage1){

            ViewGroup from = (ViewGroup) view.getParent();
            ViewGroup to = (ViewGroup) findViewById(R.id.layout2);
            View congrats = findViewById(R.id.top_layout);

            from.removeView(view);
            v.setBackgroundResource(R.drawable.dragcircle);
            sp.play(dropSound, 1, 1, 0, 0, 1);
            cb = (ImageView)findViewById(R.id.circleballoon);
            cb.setVisibility(View.VISIBLE);

            imageView.setVisibility(View.VISIBLE);
            ViewGroup two = (ViewGroup) findViewById(R.id.layout3);

            if (to.getChildCount()< 1 && two.getChildCount()< 1)

            {
                congrats.setVisibility(View.VISIBLE);
                imageView.setBackgroundResource(R.drawable.congrats);
                sun=(AnimationDrawable)imageView.getBackground();
                sun.start();

                sp.play(tada, 1, 1, 0, 0, 1);
                congrats.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        View congrats
 (RelativeLayout)findViewById(R.id.top_layout);
                        congrats.setVisibility(View.INVISIBLE);

                    }
                });

            }

 //circle balloons floating



            cb = (ImageView)findViewById(R.id.circleballoon);
            cb.setVisibility(View.VISIBLE);
            cb2 = (ImageView)findViewById(R.id.circleballoon2);
            cb2.setVisibility(View.VISIBLE);

            sp.play(inflate, 1, 1, 0, 0, 1);

            ObjectAnimator sqbalAnim3 =
ObjectAnimator.ofFloat(cb,"x",-500,500);
            sqbalAnim3.setDuration(700);
            sqbalAnim3.setRepeatCount(20);
            sqbalAnim3.setRepeatMode(ValueAnimator.REVERSE);

            ObjectAnimator sqbalAnim =
ObjectAnimator.ofFloat(cb,"y",1800,-1800);
            sqbalAnim.setDuration(3000);
            sqbalAnim.setRepeatMode(ValueAnimator.RESTART);

            AnimatorSet animSetXY = new AnimatorSet();
            animSetXY.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    cb.setVisibility(View.GONE);
                }
            });
            animSetXY.playTogether(sqbalAnim, sqbalAnim3);
            animSetXY.setStartDelay(20);
            animSetXY.start();


            ObjectAnimator sqbal2Anim =
ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"y",-450);
            sqbal2Anim.setDuration(3000);
            sqbal2Anim.setRepeatMode(ValueAnimator.RESTART);

            ObjectAnimator sqbalAnim4 =
ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"x",650,750);
            sqbalAnim4.setStartDelay(20);
            sqbalAnim4.setDuration(300);
            sqbalAnim4.setRepeatCount(6);
            sqbalAnim4.setRepeatMode(ValueAnimator.REVERSE);

            AnimatorSet animSetXY2 = new AnimatorSet();
            animSetXY2.playTogether(sqbal2Anim,sqbalAnim4);
            animSetXY2.start();

            return true;}




        } else if(view.getId()==R.id.triangleImage && 
 v.getId()==R.id.triangleImage1){
            ViewGroup from = (ViewGroup) view.getParent();
            ViewGroup to = (ViewGroup) findViewById(R.id.layout2);
            View congrats = findViewById(R.id.top_layout);
            from.removeView(view);
            v.setBackgroundResource(R.drawable.dragtriangle);
            sp.play(dropSound, 1, 1, 0, 0, 1);
            tb = (ImageView)findViewById(R.id.triballoon);
            tb.setVisibility(View.VISIBLE);
            imageView.setVisibility(View.VISIBLE);
            ViewGroup two = (ViewGroup) findViewById(R.id.layout3);

            if (to.getChildCount()< 1 && two.getChildCount()< 1)

            {
                congrats.setVisibility(View.VISIBLE);
                imageView.setBackgroundResource(R.drawable.congrats);
                sun=(AnimationDrawable)imageView.getBackground();
                sun.start();

                sp.play(tada, 1, 1, 0, 0, 1);
                congrats.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        View congrats = findViewById(R.id.top_layout);
                        congrats.setVisibility(View.INVISIBLE);

                    }
                });

            }


            AnimatorSet sunSet = (AnimatorSet)
 AnimatorInflater.loadAnimator(this, R.animator.float1);
            sunSet.setTarget(tb);
            sunSet.start();

            tb = (ImageView)findViewById(R.id.triballoon);

            AnimatorSet sunnySet = (AnimatorSet)
 AnimatorInflater.loadAnimator(this, R.animator.float2);
            sunnySet.setTarget(tb);
            sunnySet.start();

            ImageView tb2 = (ImageView)findViewById(R.id.triballoon2);
            tb2.setVisibility(View.VISIBLE);

            AnimatorSet sunSet1 = (AnimatorSet)
  AnimatorInflater.loadAnimator(this, R.animator.float3);
            sunSet1.setTarget(tb2);
            sunSet1.start();

            tb2 = (ImageView)findViewById(R.id.triballoon2);
            tb2.setVisibility(View.VISIBLE);

            AnimatorSet sunnySet1 = (AnimatorSet)   
    AnimatorInflater.loadAnimator(this, R.animator.float2);
            sunnySet1.setTarget(tb);
            sunnySet1.start();
            sp.play(inflate, 1, 1, 0, 0, 1);

            return true;

这是我想知道的原始内容,有没有一种方法可以使用OR运算符将它们全部放入同一条语句中,而每种形状仍然给出不同的结果,如下所示

@Override
public boolean onDrag(View v, DragEvent e) {

    if (e.getAction() == DragEvent.ACTION_DROP) {
        View view = (View) e.getLocalState();

        //IF THEY MATCH

        if (view.getId() == R.id.squareshape && v.getId() ==
R.id.emptysquare || view.getId() == R.id.circleshape && v.getId() ==
R.id.emptycircle|| view.getId() == R.id.trishape && v.getId() ==
R.id.emptytri ) {

         //view.getId().(v.getId());  view.setBackgroundResource(v)

            mt_sq.setImageResource(R.drawable.dragsquare);
        }else{
            //do nothing
        }

    }
    return true;
}

中间的注释是我想要实现的//view.getId()。(v.getId()); view.setBackgroundResource(v)但很明显,这会给我带来错误,任何人都可以提供解决方案,还是我只需要继续运输原件? 任何和所有建议,欢迎

在另一个SE网站上也提出了类似的问题,其核心与您的问题非常相似-总体上避免在游戏编程/软件开发中出现膨胀。 我的答案可以在这里找到,但是您应该阅读以下经修改的答案,因为它的格式可以普遍解决您的问题。

为什么if语句邪恶

Groo几年前在StackOverflow线程中为这个问题发布了一个绝妙的答案,您可以在这里找到,归结为可读性,将来的证明是if语句的敌人。

但这并不是说您应该始终避免使用if语句! 它们具有简单的一次性检查功能。

编写干净的游戏代码:

假设我们有一条语句,它在我们的render函数上绘制了一个精灵:

if (powerUpActive){

    draw(shieldSprite);

}

经过几周的开发,渲染功能变得becomes肿且难以遍历。 为了解决这个问题,我们应该在PowerUp类中的函数中移动上述逻辑(以及所有类似的逻辑),如下所示:

class PowerUps {
    public void Check() {
        if (powerUpActive){

            draw(shieldSprite);

        }
    }
}

然后打电话

powerUp.Check; 

从渲染。

为了将来证明这一点,我什至还要将powerUpActive更改为更具体的东西,例如在Shield类中的Shield类,然后使用shield.enable()检查它的活动,如下所示:

class PowerUps {
    class Shield {
        private boolean active = false;

        public void enable() {
            active = true;
        }

        public void disable() {
            active = false;
        }
    }
    public void Check() {
        if (shield.active){

            draw(shieldSprite);

        }
    }
}

其目的是双重的-您可以通过调用powerUp.shield.enable() 绘制它来轻松地启用/禁用游戏事件中的加电,而不必太担心回溯。

对于一次屏蔽启动,这似乎是一项艰巨的工作,但这是一个对代码进行过时验证并为自己节省时间的问题,因为后来经过数小时的辛苦工作,最终才需要扩展。

只需考虑一下渲染功能的外观! 扩展您的游戏而不用担心是否坏了就容易了! 我不认识你,但是我已经很兴奋了! :3

注意我提供的示例仅是详细说明。 在现实世界中,您可能希望更积极地使用继承并进一步组织事物,例如在Shield类中保留对shieldSprite的引用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM