Android游戲開發(fā):如何實(shí)現(xiàn)爆炸效果
在做Android游戲MagicBubble開發(fā)的時(shí)候,在連通兩個(gè)Bubbles的時(shí)候,Bubble會(huì)以水泡爆破的情形消失。筆者的思路是這樣的:在FrameLayout里面加入一ImageView,再定義一個(gè)爆炸的Animation,不需要的時(shí)候,ImageView就隱藏起來,需要的時(shí)候,就把ImageView移動(dòng)到需要的地方,再StartAnimation,這樣,就可以實(shí)現(xiàn)爆炸效果。
下面是簡(jiǎn)化后的程序的代碼,程序的效果如下:點(diǎn)中屏幕中任意地方,就在點(diǎn)擊地方顯示爆炸效果。
首先是Animation的定義,定義一個(gè)Frame Animation,依次播放5幀動(dòng)畫,每幀動(dòng)畫持續(xù)時(shí)間為50毫秒:
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true">
- <item android:drawable="@drawable/explode1" android:duration="50" />
- <item android:drawable="@drawable/explode2" android:duration="50" />
- <item android:drawable="@drawable/explode3" android:duration="50" />
- <item android:drawable="@drawable/explode4" android:duration="50" />
- <item android:drawable="@drawable/explode5" android:duration="50" />
- </animation-list>
接著是主程序代碼:
- package com.ray.bubble;
- import android.app.Activity;
- import android.content.Context;
- import android.graphics.drawable.AnimationDrawable;
- import android.os.Bundle;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.view.View.OnTouchListener;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- public class BubbleExplosion extends Activity {
- private FrameLayout fl;
- private ExplosionView exv1;
- private AnimationDrawable exa1;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- //set full screen
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
- WindowManager.LayoutParams. FLAG_FULLSCREEN);
- fl = new FrameLayout(this);
- fl.setBackgroundResource(R.drawable.bg);
- exv1 = new ExplosionView(this);
- exv1.setVisibility(View.INVISIBLE);
- exv1.setBackgroundResource(R.anim.explosion);
- exa1 = (AnimationDrawable)exv1.getBackground();
- fl.addView(exv1);
- fl.setOnTouchListener(new LayoutListener());
- setContentView(fl);
- }
- class ExplosionView extends ImageView{
- public ExplosionView(Context context) {
- super(context);
- }
- // 處理爆炸的位置
- public void setLocation(int top,int left){
- this.setFrame(left, top, left+40, top+40);
- }
- }
- class LayoutListener implements OnTouchListener{
- public boolean onTouch(View v, MotionEvent event) {
- //首先,你必須停止播放動(dòng)畫,如果動(dòng)畫開始,你不能重復(fù)一遍!
- exv1.setVisibility(View.INVISIBLE);
- exa1.stop();
- float x = event.getX();
- float y = event.getY();
- exv1.setLocation((int)y-20, (int)x-20);
- exv1.setVisibility(View.VISIBLE);
- exa1.start();
- return false;
- }
- }
- }
配合Android的SurfaceView,Animation可以實(shí)現(xiàn)很好的過渡效果,SurfaceView的用法很簡(jiǎn)單。
【編輯推薦】
- GameSalad:讓每個(gè)人都變成游戲開發(fā)者
- Android平臺(tái)將迎來虛幻引擎游戲開發(fā)時(shí)代
- 國(guó)外知名游戲開發(fā)商稱Android平臺(tái)比iOS更賺錢
- 獨(dú)立手機(jī)游戲開發(fā)者的未來走向
- 高效地進(jìn)行Android 游戲開發(fā)