【问题标题】:how do i make my code shorter如何使我的代码更短
【发布时间】:2015-04-10 07:59:42
【问题描述】:

你好,我做了一个小形状游戏,我的 if 语句开始失控,所以我做了一个更简单的测试场景游戏来尝试简化代码,但我不像 id 那样熟悉 java 和 android ,所以在尝试了一些事情之后,我想我会问这个问题,我如何让这段代码更小? 目前,我正在将 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) 但很明显这给了我错误任何人都可以提供解决方案还是我只需要继续运输我的原件?欢迎提出任何建议

【问题讨论】:

  • 使用 Jake Wharton 的 ButterKnife 库,因此您不会经常使用 findViewById。
  • 黄油刀看起来很棒,我浏览了网站,但我不知道如何实现这一点,但对于更有经验的编码人员来说,这将是答案,所以我会尽快将其标记为答案,除非有人有一个更简单的解决方案,谢谢@Kevin Crain
  • 它不适合有经验的程序员,它易于实现和使用。 :)
  • 我知道,但看起来我需要在 android studio 中编辑 gradle 文件才能使用它,这对我来说从来都不是很好,lol 仍然没有设法让 Facebook sdk 与 studio 一起工作
  • gradleplease.appspot.com,您可以使用此链接查找库,第一步使用 gradle 编译将库添加到项目中,第二步如果您使用活动或片段会有所不同,对于活动在onCreate 使用 ButterKnife.inject(this),对于 onCreateView 中的片段使用 ButterKnife.inject(this, rootView),第 3 步将为您要注入的所有视图使用 InjectView 注释,所有查找注入发明以便您可以标记具有类似 OnClick 注释的方法,而不是使用 setOnClickListener

标签: java android if-statement operators


【解决方案1】:

在另一个 SE 网站上提出了类似的问题,其核心与您的问题非常相似——避免游戏编程/软件开发中的膨胀。我的答案可以在 here 找到,但您应该阅读下面修改后的答案,因为它的格式可以以一般方式解决您的问题。

为什么if 声明是邪恶的

Groo 几年前在 StackOverflow 线程中发布了对这个问题的精彩回答,您可以找到 here,这归结为可读性和未来证明是 if 的敌人陈述。

这并不是说您应该始终避免使用 if 语句!它们有它们的用途,只需简单的一次性检查。

编写干净的游戏代码:

假设我们有一个语句,它在我们的渲染函数中绘制了一个精灵:

if (powerUpActive){

    draw(shieldSprite);

}

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

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

            draw(shieldSprite);

        }
    }
}

然后打电话

powerUp.Check; 

来自渲染。

为了将来证明这一点,我什至会将 powerUpActive 更改为更具体的东西,例如在 PowerUp 类中的 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 的引用。

【讨论】:

  • 我认为我想要的比你建议的要简单得多(也许不可能),例如,这个游戏永远不会在更大的应用程序中扩展它的分心,因此我的想要更简单的代码,但你的回答说了很多,而且你所说的大部分内容都会对我有所帮助,所以我会将你的代码标记为正确,非常感谢
  • 如果您不打算进一步扩展应用程序,那么就没有必要浪费时间优化它——您已经从中了解了您需要什么,应该继续前进。特别是如果您只是为了减少代码而想缩小它的大小。祝你未来好运!
  • 谢谢你的权利,这就是我从这里拿走的东西,但它现在已经完成并达到了它的目的,感谢所有回复
猜你喜欢
  • 1970-01-01
  • 2018-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-28
  • 2017-08-04
  • 2016-02-18
  • 2023-01-02
相关资源
最近更新 更多