现在的位置: 首页 > 综合 > 正文

Android游戏开发之爆炸效果

2013年10月17日 ⁄ 综合 ⁄ 共 5299字 ⁄ 字号 评论关闭

在做Android游戏MagicBubble开发的时候,在连通两个Bubbles的时候,Bubble会以水泡爆破的情形消失。为了实现这一效果,我查找了不少资料,希望能找到一些标准的实现方面,花了不少时间,发觉Android关于游戏开发的资料实在太少了,更不用说标准做法了,没办法,只能按照自己的思路来实现这一效果。

    我的思路是这样的(仅供参考,希望有更好做法的朋友跟我们共享一下):在FrameLayout里面加入一ImageView,再定义一个爆炸的Animation,不需要的时候,ImageView就隐藏起来,需要的时候,就把ImageView移动到需要的地方,再StartAnimation,这样,就可以实现爆炸的效果。

   下面是简化后的程序的代码,程序的效果如下:点中屏幕中任意地方,就在点击地方显示爆炸效果。

 

首先是Animation的定义,定义一个Frame Animation,依次播放5帧动画,每帧动画持续时间为50毫秒:

Xml代码 复制代码
  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:oneshot="true">  
  3.     <item android:drawable="@drawable/explode1" android:duration="50" />  
  4.     <item android:drawable="@drawable/explode2" android:duration="50" />  
  5.     <item android:drawable="@drawable/explode3" android:duration="50" />  
  6.     <item android:drawable="@drawable/explode4" android:duration="50" />  
  7.     <item android:drawable="@drawable/explode5" android:duration="50" />     
  8. </animation-list>  

 接着是主程序代码:

Java代码 复制代码
  1. package com.ray.bubble;   
  2.   
  3. import android.app.Activity;   
  4. import android.content.Context;   
  5. import android.graphics.drawable.AnimationDrawable;   
  6. import android.os.Bundle;   
  7. import android.view.MotionEvent;   
  8. import android.view.View;   
  9. import android.view.Window;   
  10. import android.view.WindowManager;   
  11. import android.view.View.OnTouchListener;   
  12. import android.widget.FrameLayout;   
  13. import android.widget.ImageView;   
  14.   
  15. public class BubbleExplosion extends Activity {   
  16.     private FrameLayout fl;   
  17.     private ExplosionView exv1;   
  18.     private AnimationDrawable exa1;   
  19.     public void onCreate(Bundle savedInstanceState) {   
  20.         super.onCreate(savedInstanceState);   
  21.         //set full screen   
  22.         requestWindowFeature(Window.FEATURE_NO_TITLE);   
  23.         getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,   
  24.                       WindowManager.LayoutParams. FLAG_FULLSCREEN);   
  25.         fl = new FrameLayout(this);   
  26.         fl.setBackgroundResource(R.drawable.bg);   
  27.            
  28.         exv1 = new ExplosionView(this);   
  29.         exv1.setVisibility(View.INVISIBLE);   
  30.         exv1.setBackgroundResource(R.anim.explosion);   
  31.         exa1 = (AnimationDrawable)exv1.getBackground();   
  32.         fl.addView(exv1);   
  33.         fl.setOnTouchListener(new LayoutListener());   
  34.         setContentView(fl);   
  35.     }   
  36.        
  37.     class ExplosionView extends ImageView{   
  38.   
  39.         public ExplosionView(Context context) {   
  40.             super(context);   
  41.         }   
  42.         //handle the location of the explosion   
  43.         public void setLocation(int top,int left){   
  44.             this.setFrame(left, top, left+40, top+40);   
  45.         }      
  46.     }   
  47.        
  48.     class LayoutListener implements OnTouchListener{   
  49.   
  50.         public boolean onTouch(View v, MotionEvent event) {   
  51.             //firstly, u have to stop the animation,if the animation   
  52.             //is starting ,u can not start it again!   
  53.             exv1.setVisibility(View.INVISIBLE);   
  54.             exa1.stop();   
  55.             float x = event.getX();   
  56.             float y = event.getY();   
  57.             exv1.setLocation((int)y-20, (int)x-20);   
  58.             exv1.setVisibility(View.VISIBLE);   
  59.             exa1.start();   
  60.             return false;   
  61.         }   
  62.            
  63.     }   
  64. }  

 配合Android的SurfaceView,Animation可以实现很好的过渡效果,SurfaceView的用法很简单,可参考:

http://rayleung.javaeye.com/blog/420410

 

抱歉!评论已关闭.