飞机大战-GameObject精灵父类的实现
2015-05-30 22:08
489 查看
所有的精灵都继承这个父类,精灵共有的属性或者是使用次数多的变量以及一些公共的方法,都在此类声明,提高代码的利用率。
有一个碰撞检测方法,用的是矩形碰撞检测,原理是:一个精灵矩形的中点横坐标减去另一个精灵矩形中点的横坐标他们的绝对值会小于他们矩形长度的一半的和,同理纵
坐标也一样。当然方法也有很多种,不过这样会带来误差,想象一下可以知道,精灵不一定是一个矩形,而我们把它定义成一个矩形,这样会带来误差碰撞,我们可以在方法中
处理一下,(楼主偷懒了)因为宁愿少检测也不带来误差检测。
有一个碰撞检测方法,用的是矩形碰撞检测,原理是:一个精灵矩形的中点横坐标减去另一个精灵矩形中点的横坐标他们的绝对值会小于他们矩形长度的一半的和,同理纵
坐标也一样。当然方法也有很多种,不过这样会带来误差,想象一下可以知道,精灵不一定是一个矩形,而我们把它定义成一个矩形,这样会带来误差碰撞,我们可以在方法中
处理一下,(楼主偷懒了)因为宁愿少检测也不带来误差检测。
package com.example.qgns; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Paint; public class GameObject { protected int currentFrome;//当前帧 protected int score;//分数 protected int speed;//速度 protected int harm;//伤害 protected int bloodVloume//血量; protected int currentBlood;//当前血量 protected float object_x;//精灵x坐标 protected float object_y;//精灵y坐标 protected float object_width;//精灵宽 protected float object_height;//精灵高 protected float screen_width;//屏宽 protected float screen_height;//屏高 protected boolean isAlive;//是否存活 protected boolean isExplosion;//是否爆炸 protected Paint paint;//画笔 protected Resources res;//资源 public GameObject(Resources res) { this.res = res; paint = new Paint();//实例化画笔 } public void initScreen(float screen_width, float screen_height) {//得到屏幕的宽和高 this.screen_width = screen_width; this.screen_height = screen_height; } public void initial(int i, float m, float n, int j) {//精灵的初始化操作 isAlive = true; object_y = -object_height * (i * 2 + 1);//y坐标随传进来的i而确定 } public void initBitmap() { } public void myDraw(Canvas canvas) { } public void move() { } public void release() { } public void attacked(int harm) { } public boolean isCollide(GameObject obj) {//碰撞检测方法 if (Math.abs((object_x + object_width / 2) - (obj.object_x + obj.object_width / 2)) < (object_width + obj .object_width) / 2 && Math.abs((object_y + object_height / 2) - (obj.object_y + obj.object_height / 2)) < (object_height + obj .object_height) / 2) { isExplosion = true;//碰撞了将爆炸的标志位改为true return true; } return false; } }
相关文章推荐
- Object-c 控制语句
- object-c 基本数据类型
- python遇到AttributeError: 'str' object has no attribute '_getAssertEqualityFunc'问题解决过程
- OC视频笔记3.1(Foundation框架,NSObject里面的方法)3.2(isKindOfClass和isMemberOfClass)
- object references an unsaved transient instance - save the transient instance before flushing
- error LNK2019: 无法解析的外部符号 __imp_GetUserObjectInformationW,该符号在函数 OPENSSL_isservice 中被引用
- Android开发: JSONObject的数据解析
- [Objective-C] 类、对象和方法
- Flex中的XMLListCollection,ArrayCollection和ObjectProxy
- Objective- c浅谈对象的初始化
- WaitForSingleObject和CEvent用法
- Object-C非正式协议与正式协议的区别
- 关于IE和火狐,谷歌,Safari对Html标签Object和Embed的支持问题
- source insight支持object-c显示的设置
- NSString,NSMutableString
- Objective-C 和 Core Foundation 对象相互转换的内存管理总结
- cannot open shared object file: No such file or directory解决
- C#如何把List of Object转换成List of T具体类型
- Ljava.lang.Object; cannot be cast to com.bdqn.hibernate.entity.*
- 使用Objective-C的文档生成工具:appledoc