自定义View6 -塔防小游戏:第三篇防御塔随意放置+多组野怪( 四 )


//炮弹动画private void shotMove(float x, float y, float x2, float y2, int blamePosition, int towerPosition) {if (!towerList.get(towerPosition).isAttacking()) {towerList.get(towerPosition).setAttacking(true);shotView = new ImageView(this.getContext());shotView.setImageDrawable(getContext().getDrawable(R.drawable.shot));shotView.layout(0, 0, 20, 20);addView(shotView);//开炮音效回调iShotService.shot();translateAnimation = new TranslateAnimation(x, x2, y, y2);translateAnimation.setDuration(towerList.get(0).getAttacksSpeed());translateAnimation.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {blameList.get(blamePosition).setHP(blameList.get(blamePosition).getHP() - towerList.get(towerPosition).getHarm());towerList.get(towerPosition).setAttacking(false);int childCount = getChildCount();if (childCount > 1) {removeView(getChildAt(childCount - 1));}}@Overridepublic void onAnimationRepeat(Animation animation) {}});shotView.startAnimation(translateAnimation);}}我们需要控制开始与暂停和onResume下动画的释放等
public void start() {if (valueAnimator != null) {//开启动画addBlame();//addTower(450,450);valueAnimator.start();countDownTimer.start();}}public void pause() {if (valueAnimator != null) {//开启动画valueAnimator.pause();countDownTimer.cancel();}}/*** hasWindowFocus:true 获得焦点 , 开启动画;false 失去焦点 , 停止动画*/@Overridepublic void onWindowFocusChanged(boolean hasWindowFocus) {super.onWindowFocusChanged(hasWindowFocus);if (!hasWindowFocus) {if (valueAnimator != null) {//开启动画valueAnimator.pause();countDownTimer.cancel();countDownTimer = null;}if (countDownTimer != null) {countDownTimer.cancel();countDownTimer = null;}}}总结:这里加入了新的背景图、多个防御塔随意摆放、一旦摆放就无法移动(后续加入拆除、升级)等功能 。难点还是在野怪移动上 , 还有多个防御塔攻击互相不影响 。
问题:现在的思路是刷新一下 , 野怪走一步 , 后续如果加入减速防御塔的话 , 应该怎么走呢 , 多个野怪如何做到行走速度互不影响呢 。
个人思路:可以正常野怪一次走5步 , 如果被减速防御塔打中后 , 就把5步见为2步 , position+5调整为position+2 。这里只是记录学习View的过程 , 不要较真哦 。

经验总结扩展阅读