2015-02-10 76 views
0

嗨,我做了一个小型游戏,我的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; 

这是我原来是我想知道的是,是否有使用或运算把他们都在同一个语句,但仍给出不同的结果的方式每个形状像这样

@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)但显而易见,这给了我错误任何人都可以提供一个解决方案,或者我只是需要保持卡车在我的原始?任何和所有的建议欢迎

+1

使用杰克沃顿的ButterKnife库,所以你不要经常使用findViewById。 – 2015-02-10 10:37:10

+0

黄油刀看起来很棒,我通过网站阅读,但我不知道如何实现这一点,但对于更有经验的编码人员来说,这将是答案,所以我会很快将此标记为答案,除非有人用更简单的解决方案感谢你@Kevin Crain – martinseal1987 2015-02-10 10:56:18

+0

它不适合有经验的程序员,它易于实现和使用。 :) – 2015-02-10 11:09:13

回答

0

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

为什么if声明邪

Groo发布一个梦幻般的答案,几年前在StackOverflow的线程这个问题,你可以找到here,这归结为可读性和适应未来发展是if语句的敌人。

这并不是说你应该总是避免if语句!他们有简单的用途,一次性支票。

编写干净的游戏代码:

假设我们有一个说法,绘制一个精灵我们的渲染功能:

if (powerUpActive){ 

    draw(shieldSprite); 

} 

几个发展星期后,渲染功能变得臃肿和难以穿越。为了解决这个问题,我们应该将上述逻辑(以及所有类似的逻辑)函数内部,在类的上电,像这样:

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

      draw(shieldSprite); 

     } 
    } 
} 

并调用

powerUp.Check; 

从渲染。

为了未来的证明,我甚至会将powerUpActive更改为更类似于类Shield的东西,在PowerUp类中,然后使用盾检查它的活动。使能(),像这样:

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

     } 
    } 
} 

这样做的目的是双重的 - 你可以实现从一个游戏事件/关闭电源UPS容易,通过调用powerUp.shield.enable()平局它不必太担心回溯。

对于一个屏蔽电启动来说,这可能看起来像很多工作,但这是一个未来证明你的代码的问题,并且可以节省数小时,几个小时的努力工作,当你最终想要扩展时。

想想想想你的渲染功能将会有多漂亮!如果你打破了一些东西,不用担心扩展你的游戏将会多么容易!我不了解你,但我很兴奋! :3

NB我提供的例子只是阐述一个点。在现实世界中,您可能希望更积极地使用继承,并组织更多的东西,例如保留对Shield类中的shieldSprite的引用。

+1

我认为我想要的比想象的要简单得多(也许不可能),例如,这个游戏永远不会在更大的应用程序中扩展其分散注意力,因此我的愿望g为更简单的代码量,但你的答案说很多,你所说的很多东西都会帮助我,所以我会将你的标记标记为非常感谢你 – martinseal1987 2015-02-10 11:21:14

+0

如果你不打算将应用程序扩展得更远,浪费时间来优化它 - 你已经从中学到了什么,并且应该继续前进。特别是如果你只是为了减少代码而缩小它的大小。未来好运! – Sipty 2015-02-10 11:43:03

+1

谢谢Sipty你的权利,这就是我已经从这个,但它现在完成并达到其目的,谢谢所有答复 – martinseal1987 2015-02-11 00:49:34