0
点赞
收藏
分享

微信扫一扫

飞机大战笔记


一、建立滚动场景和主战机动画;

飞机大战笔记_ide

两个脚本;

场景动画:

using UnityEngine;
using System.Collections;

public class BackgroundTransform : MonoBehaviour {

public float moveSpeed = 2f;

// Update is called once per frame
void Update () {
this.transform.Translate( Vector3.down * moveSpeed * Time.deltaTime );
Vector3 postion = this.transform.position;
if(postion.y<=-8.52f){
this.transform.position = new Vector3(postion.x,postion.y+8.52f*2,postion.z );
}
}
}


住战机的帧动画:

using UnityEngine;
using System.Collections;

public class Hero : MonoBehaviour {

public bool animation = true;
//bool变量标示是否在动画

public int frameCountPersconds = 10;//每秒播放10帧,一个动画两帧,相当于5遍动画。

public float timer = 0;
//计时器

public Sprite[] sprites;//通过数组设置动画

private SpriteRenderer spriteRender;

void Start(){
spriteRender = this.GetComponent<SpriteRenderer> ();
//这样初始化避免每次调用费时
}
// Update is called once per frame
void Update () {
if (animation) {
timer += Time.deltaTime;//计算出一帧需要时时间1/10 1f/frameCountPersconds
int frameIndex = (int) ( timer / (1f / frameCountPersconds) );
int frame = frameIndex % 2;//计算出当前帧

/***/
spriteRender.sprite = sprites [frame];
//得到SpriteRenderer组件,并把当前帧赋给sprite属性
}
}
}


二、主战机发射子弹,敌机以及奖励的随机生成

hero上空子类,也就是3个炮口Gun类:

发射的时间间隔,发射的对象的控制:

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour {

public float rate = 0.2f;//每隔0.2秒发一颗子弹

public GameObject bullet;//表示子弹的预制对象(frifab)

void Start(){
openFire ();
}

//新建一个方法
public void fire(){
GameObject.Instantiate (bullet, transform.position, Quaternion.identity);
//GameObject里面的实例化方法(变量,位置,旋转角度)
//Quaternion.identity代表没有旋转的一个静态变量
}

public void openFire(){
InvokeRepeating("fire",1,rate);
//InvokeRepeating方法(调用的方法的名字,过多长开始时间调用,每间隔多少秒调用一次 )
//InvokeRepeating(string methodName,float time, float repeatRate )
}
}




子弹类:移动加消失

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {

public float speed = 2;

void Update(){
transform.Translate ( Vector3.up * speed * Time.deltaTime );
if (transform.position.y > 4.3f) {
Destroy(this.gameObject);
}
}
}

敌机类:

敌机的移动血量,速度,得分,以及出镜头后消失方法:

using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

public int hp=1;
public float speed = 2;
public float score = 100;

// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
this.transform.Translate ( Vector3.down*speed*Time.deltaTime );
if (this.transform.position.y <= -5.6) {
Destroy(this.gameObject);
}
}
}


奖励类:

奖励的移动速度,奖励的类型,以及奖励出境消失方法:

using UnityEngine;
using System.Collections;

public class Award : MonoBehaviour {

public int type = 0;// 0 gun 1 explode

public float speed = 1.5f;


void Update(){

this.transform.Translate (Vector3.down * Time.deltaTime * speed);
if (this.transform.position.y <= -4.5)
Destroy (this.gameObject);
}
}


产卵类:

也就是随机生成3种敌机和2种奖励。x轴用到随机数:

<pre name="code" class="csharp">using UnityEngine;
using System.Collections;

public class Spawn : MonoBehaviour {

public GameObject enemy0Prefab;
public GameObject enemy1Prefab;
public GameObject enemy2Prefab;

public GameObject awardType0Prefab;
public GameObject awardType1Prefab;

public float enemy0Rate = 0.5f;//敌机生成速率
public float enemy1Rate = 5f;//敌机生成速率
public float enemy2Rate = 8f;//敌机生成速率

public float awardType0Rate = 7;//奖励生成速率
public float awardType1Rate = 10;//奖励生成速率

// Use this for initialization
void Start () {
InvokeRepeating ("cteareEnemy0",1,enemy0Rate);
InvokeRepeating ("cteareEnemy1",3,enemy1Rate);
InvokeRepeating ("cteareEnemy2",6,enemy2Rate);
InvokeRepeating ("cteareAwardType0",10,awardType0Rate);
InvokeRepeating ("cteareAwardType1",10,awardType1Rates);
}

// Update is called once per frame
void Update () {

}

public void cteareEnemy0(){
float x = Random.Range (-2.15f, 2.15f);
GameObject.Instantiate (enemy0Prefab,new Vector3(x,transform.position.y,0), Quaternion.identity);
}
public void cteareEnemy1(){
float x = Random.Range (-2.04f, 2.04f);
GameObject.Instantiate (enemy1Prefab,new Vector3(x,transform.position.y,0), Quaternion.identity);
}
public void cteareEnemy2(){
float x = Random.Range (-1.57f, 1.57f);
GameObject.Instantiate (enemy2Prefab,new Vector3(x,transform.position.y,0), Quaternion.identity);
}
public void cteareAwardType0(){
float x = Random.Range (-2.1f, 2.1f);
GameObject.Instantiate (awardType0Prefab,new Vector3(x,transform.position.y,0), Quaternion.identity);
}
public void cteareAwardType1(){
float x = Random.Range (-2.1f, 2.1f);
GameObject.Instantiate (awardType1Prefab,new Vector3(x,transform.position.y,0), Quaternion.identity);
}
}


三、设置主角的控制和敌机的动画

显示设置子弹和敌机的碰撞检测

然后给敌机设置好两种动画的播放。

子弹:

using UnityEngine;
using System.Collections;

public class Bullet : MonoBehaviour {




public float speed = 2;

void Update(){
transform.Translate ( Vector3.up * speed * Time.deltaTime );
if (transform.position.y > 4.3f) {
Destroy(this.gameObject);
}

}
void OnTriggerEnter2D(Collider2D other) {
if(other.tag == "Enemy"){

if(!other.GetComponent<Enemy>().isDeath){

//如果飞机死了,播放爆炸效果,子弹就不能碰撞它了

other.gameObject.SendMessage("BeHit");
//SendMessage("BeHit")作用是调用所有类中叫BeHit的方法
GameObject.Destroy(this.gameObject);


}
}





}//检测碰撞,查mono

}

敌机:

using UnityEngine;
using System.Collections;

//定义枚举类型
public enum EnemyType{

smallEnemy,
middleEnemy,
bigEnemy
}

public class Enemy : MonoBehaviour {


//定义枚举类型,设置飞机爆炸动画


public int hp=1;
public float speed = 2;
public float score = 100;

public EnemyType type= EnemyType.smallEnemy;


public bool isDeath = false;//死亡标志

public Sprite [] explosionSprites;


private float timer = 0f;
//播放动画需要的计时器,一般都使用私有

public int explosionAnimationFrame = 10;
//动画播放帧率设置,每秒播放帧数,基本都是10

private SpriteRenderer render;
//使用这个组件播放

public float hitTimer = 0.2f ;
//碰撞播放时间
private float resetHitTime ;
//充值碰撞时间的
public Sprite [] hitSprites;

// Use this for initialization
void Start () {
render = this.GetComponent<SpriteRenderer> ();

resetHitTime = hitTimer;
hitTimer = 0;
}

// Update is called once per frame
void Update () {

this.transform.Translate ( Vector3.down*speed*Time.deltaTime );
if (this.transform.position.y <= -5.6) {
Destroy(this.gameObject);
}

if (isDeath) {
//如果死亡,就播放死亡爆炸动画
//if(typeof == EnemyType.smallEnemt){

timer += Time.deltaTime;
int frameIndex = (int)(timer / (1f / explosionAnimationFrame));
if (frameIndex >= explosionSprites.Length) {
//destroy
Destroy (this.gameObject);
} else
render.sprite = explosionSprites [frameIndex];
//}

} else {

if(type == EnemyType.middleEnemy || type == EnemyType.bigEnemy){

if(hitTimer >= 0){
hitTimer -= Time.deltaTime;

int frameIndex = (int) ((resetHitTime - hitTimer)/(1f / explosionAnimationFrame));
frameIndex%=2;
render.sprite = hitSprites [frameIndex];
}
}
}
}

public void BeHit(){
hp-=1;
//if( type == EnemyType.smallEnemy){
//hp-=1;
if (hp <= 0) {
isDeath = true;
}
//}
else {
hitTimer = resetHitTime;
}


}

}








举报

相关推荐

飞机大战游戏

【JAVA】飞机大战

javascript飞机大战

飞机大战简单版

飞机大战(easyx版)

飞机大战C++

0 条评论