1.Android MotionEvent
public boolean onTouchEvent (MotionEvent event){
menu.onTouch(event);
celect.onTouch(event);
myPlane.onTouch(event);
return true;
}
public void onTouch(MotionEvent event){
if (event.getAction()==MotionEvent.ACTION_DOWN){
float ex = event.getX();
float ey = event.getY();
if (ex>x&&ex<x+bitmap3.getWidth()&&ey>y&&ey<y+bitmap3.getHeight()){
MySurfaceView.Game_STATE = 0;
}
}
}
关于MotionEvent的一个重要概念,事件类型。事件类型就是指MotionEvent对象所代表的动作。有的事件代表你手指按下这个动作,有的事件代表你手指在屏幕上滑动,还有的事件代表你手指离开屏幕。这些事件的事件类型就分别为ACTION_DOWN,ACTION_MOVE,和ACTION_UP。上述这个动作所产生的一系列事件,被称为一个事件流,它包括一个ACTION_DOWN事件,很多个ACTION_MOVE事件,和一个ACTION_UP事件。
2.Vector
Vector mybulletVector = new Vector<>();
这个创建一个动态数组的普通对象,是线性安全,而Arraylist是非线性安全
if (count % 10 == 0) {
gameSoundPool.playSound(1);
Mybullet mybullet = new Mybullet(BitmapFactory.decodeResource(this.getResources(), R.mipmap.mybullet),myPlane.getX()+(myPlane.getWidth()/2)-15,myPlane.getY(),0);
mybulletVector.add(mybullet);} //添加数据
for (int i=0;i<mybulletVector.size();i++) {
if (mybulletVector.elementAt(i).isDead()) { //判断
mybulletVector.remove(i); //删除数据
}
}
for (int i=0;i<mybulletVector.size();i++){
mybulletVector.elementAt(i).Appear(canvas,paint); //for循环打印
if (bossPlane.isCollision(mybulletVector.elementAt(i))){
gameSoundPool.playSound(2);
mybulletVector.remove(i);
}
}
3.soundpool
SoundPool主要用于播放一些较短的声音片段,与MediaPlayer相比,SoundPool的优势在于CPU资源占用量低和反应延迟小。另外,SoundPool还支持自行设置声音的品质、音量、 播放比率等参数
SoundPool(int maxStreams, int streamType, int srcQuality):第一个参数指定支持多少个声音;第二个参数指定声音类型:第三个参数指定声音品质。
Soundpool的方法其一:
int load(Context context, int resld, int priority):从 resld 所对应的资源加载声音。
int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate):该方法的第一个参数指定播放哪个声音;leftVolume、rightVolume指定左、右的音量:priority指定播放声音的优先级,数值越大,优先级越高;loop指定是否循环,0为不循环,-1为循环;rate指定播放的比率,数值可从0.5到2, 1为正常比率。
使用SoundPool播放声音的步骤如下:
1) 调用SoundPool的构造器创建SoundPool的对象。
2) 调用SoundPool对象的load()方法从指定资源、文件中加载声音。最好使用HashMap< Integer, Integer>来管理所加载的声音。
3) 调用SoundPool的play方法播放声音。