Android游戏开发:如何实现爆炸效果

移动开发 Android 游戏开发
本文介绍了在Android游戏开发中,如何实现爆炸效果的代码演示。解决了在做Android游戏MagicBubble开发的时候,连通两个Bubbles,Bubble会以水泡爆破的问题。

在做Android游戏MagicBubble开发的时候,在连通两个Bubbles的时候,Bubble会以水泡爆破的情形消失。笔者的思路是这样的:在FrameLayout里面加入一ImageView,再定义一个爆炸的Animation,不需要的时候,ImageView就隐藏起来,需要的时候,就把ImageView移动到需要的地方,再StartAnimation,这样,就可以实现爆炸效果。

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

MagicBubble1 

MagicBubble2

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

  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>  

接着是主程序代码:

  1. package com.ray.bubble;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.graphics.drawable.AnimationDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8. import android.view.Window;  
  9. import android.view.WindowManager;  
  10. import android.view.View.OnTouchListener;  
  11. import android.widget.FrameLayout;  
  12. import android.widget.ImageView;  
  13. public class BubbleExplosion extends Activity {  
  14. private FrameLayout fl;  
  15. private ExplosionView exv1;  
  16. private AnimationDrawable exa1;  
  17. public void onCreate(Bundle savedInstanceState) {  
  18. super.onCreate(savedInstanceState);  
  19. //set full screen  
  20. requestWindowFeature(Window.FEATURE_NO_TITLE);  
  21. getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,  
  22. WindowManager.LayoutParams. FLAG_FULLSCREEN);  
  23. fl = new FrameLayout(this);  
  24. fl.setBackgroundResource(R.drawable.bg);  
  25. exv1 = new ExplosionView(this);  
  26. exv1.setVisibility(View.INVISIBLE);  
  27. exv1.setBackgroundResource(R.anim.explosion);  
  28. exa1 = (AnimationDrawable)exv1.getBackground();  
  29. fl.addView(exv1);  
  30. fl.setOnTouchListener(new LayoutListener());  
  31. setContentView(fl);  
  32. }  
  33. class ExplosionView extends ImageView{  
  34. public ExplosionView(Context context) {  
  35. super(context);  
  36. }  
  37. // 处理爆炸的位置  
  38. public void setLocation(int top,int left){  
  39. this.setFrame(left, top, left+40, top+40);  
  40. }  
  41. }  
  42. class LayoutListener implements OnTouchListener{  
  43. public boolean onTouch(View v, MotionEvent event) {  
  44. //首先,你必须停止播放动画,如果动画开始,你不能重复一遍!  
  45. exv1.setVisibility(View.INVISIBLE);  
  46. exa1.stop();  
  47. float x = event.getX();  
  48. float y = event.getY();  
  49. exv1.setLocation((int)y-20, (int)x-20);  
  50. exv1.setVisibility(View.VISIBLE);  
  51. exa1.start();  
  52. return false;  
  53. }  
  54. }  
  55. }  

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

【编辑推荐】

  1. GameSalad:让每个人都变成游戏开发者
  2. Android平台将迎来虚幻引擎游戏开发时代
  3. 国外知名游戏开发商称Android平台比iOS更赚钱
  4. 独立手机游戏开发者的未来走向
  5. 高效地进行Android 游戏开发

 

责任编辑:佚名 来源: 开发者社区
相关推荐

2013-05-21 13:33:02

Android游戏开发异步音乐播放

2011-08-29 09:49:35

LuaAndroid游戏

2013-05-21 13:55:51

Android游戏开发图像渐变特效

2022-12-12 11:11:05

2013-05-21 15:28:31

2013-05-21 16:17:13

2013-07-05 10:26:40

Android

2012-05-09 14:49:23

HTML5

2023-03-23 09:09:32

前端词云效果

2011-07-08 15:08:16

iPhone 图片

2011-07-22 18:20:04

IOS View 动画

2014-07-15 10:34:14

Android游戏引擎

2010-03-03 15:06:52

Android 游戏开

2013-11-06 11:31:28

Android游戏

2013-05-20 15:42:22

2011-06-27 10:45:06

BattleheartAndroid游戏开发

2017-01-22 17:25:55

Android放大镜效果源码分析

2010-09-06 09:43:46

TCPUDPAndroid

2012-08-10 09:22:38

CoronaCorona SDKCorona SDK游

2011-08-22 14:21:24

iPhone开发UIView Anim
点赞
收藏

51CTO技术栈公众号