"飞机大战"开发笔记

 

此处是简化版,仅供参考。


 1. 直接拖背景,把两个背景放到一个空物体内便于管理。两个背景都需要挂上一下移动脚本。

 2. 背景移动: void Update () {

this.transform.Translate (Vector3.down * movespeed *Time.deltaTime);  //做一个平移操作
Vector3 position = this.transform.position;


if (position.y <= -8.52f) {             //如果全部移出则从新回到原来的上方去,即两个长度的距离
this.transform.position = new Vector3 (position.x,position.y + 8.52f*2,position.z); 
}
   }

  3. 常用的层: Background、Character、UI、Foreground。
 
  4. 设置飞机的动画:  public class hero : MonoBehaviour {


public bool animation = true;


public float time = 0;
public int frameNumber = 10;
public Sprite[] sprites;


void Start () {
                                                                            
}
void Update () {

if (animation) {
time += Time.deltaTime;
int frameIndex = time / (1f / frameNumber); //frameIndex表示当前是第几帧
int frame = frameIndex % 2;   
this.GetComponent<SpriteRenderer> ().sprite = sprites [frame];


}

  5. 在发射处创建3个空物体,作为子弹发射,需要实例化每个物体。

            public class Gun : MonoBehaviour {

public GameObject bullet;  //大写的是类,小写的gameobject是对象
public float rate = 0.2f;


   void Start () {
openFire ();
  }

 
    public void fire(){
GameObject.Instantiate (bullet, transform.position, Quaternion.identity);//克隆原始物体并返回克隆物体

    }
 public void openFire(){
InvokeRepeating ("fire", 1, rate); //1s后开始调用方法,以0.2f的速率生成克隆物体
}
             }

       此时不能发射出去,发射的代码:public float speed = 6;   //直接在预制体上挂载脚本也行
 
void Update () {

transform.Translate (Vector3.up * speed * Time.deltaTime);  //发射是一个平移操作
if (transform.position.y > 4.3f) {
Destroy (this.gameObject);
}
}

  6.  自动生成敌人或者奖品(在此以一个奖励品为例)
       public class spawn : MonoBehaviour {

public GameObject enemy0Prefab;

public float enemy0rate = 1f;
 
void Start () {

InvokeRepeating ("createEnemy0", 1, enemy0rate);

}

void Update () {

}
public void createEnemy0(){
   
GameObject.Instantiate (enemy0Prefab, transform.position, Quaternion.identity);

}

  7. 移动飞机:  public bool isMouseDown = false;

        private Vector3 lastMousePosition = Vector3.zero;

                 if (Input.GetMouseButtonDown (0)) {
isMouseDown = true;
      }
if (Input.GetMouseButtonUp (0)) {
isMouseDown = false;
    }

if (isMouseDown) {    //移动主角,这是最重要的一步

if(lastMousePosition != Vector3.zero){       //将屏幕坐标转换为世界坐标
                                    
Vector3 offset = Camera.main.ScreenToWorldPoint( Input.mousePosition)-lastMousePosition; //这一帧的位置-上一帧的位置

                        transform.position = transform.position + offset;

}
lastMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

}


   8. 飞机的边界检查:
                private void CheckPosition(){    //边界检查,移动飞机
Vector3 pos = transform.position;
float x = pos.x;
float y = pos.y;
if (x < -1.98f) {
x = -1.98f;
}
if (x > 2.2f) {
x = 2.2f;
}
if (y < -3.8f) {
y = -3.8f;
}
if (y > 3.8f) {
y = 3.8f;
}
transform.position = new Vector3 (x, y, 0);  //注意:不能是transform.position.y,错误形式
   }


   9. 把子弹添加“Box Collider2D”“Rigidbody 2D”,其中为触发器。
      给敌机添加“Box Collider2D”就可以了。注意两者不能同时为触发器。


  10. 播放动画或者击中的动画:


             if (isDeath) {                         //死亡动画的播放,和之前区别是只播放一次
time += Time.deltaTime;
int frameIndex =(int)( time / (1f / frameNumber));
if (frameIndex >= explosionSprites.Length ) {          //如果帧数大于总的动画帧数,就销毁
Destroy (this.gameObject);
} else {                                   //否则播放动画
this.GetComponent<SpriteRenderer> ().sprite = explosionSprites [frameIndex];
}//因为出现了越界的情况,上面销毁了下面还在用,所以用else语句就可以解决
}

        else{
if (type == EnemyType.middleEnemy || type == EnemyType.bigEnemy) {     //此二者有击中的动画
if (HitTime >= 0) {
HitTime -= Time.deltaTime;
int frameIndex = (int)((resetHitTime - HitTime) / (1f / frameNumber));
frameIndex %= 2;
this.GetComponent<SpriteRenderer> ().sprite = hitSprites [frameIndex];
}
}

   11. 把奖励物品都添加“Box Collider2D”,每次吃完奖励之后的武器切换,都设置为触发器,如果不设置也行,只是效果不好
       把飞机添加“Box Collider2D”并且是触发器,“RigidBody2D”去除重力。

         public float superguntime = 10f;
private float resetsupertime;   //每次吃了之后需要重置

         void Start () {
resetsupertime = superguntime;   //第一次赋值,重置时间为10s
superguntime = 0;  //默认是没有的
}

      问题:得到超级武器之后,时间不停止,一直是负增长。


  12. 显示得分,创建gamemanager来管理,用的是单例模式: public static gamemanager _instance;

 void Awake () {
_instance = this;
 }
                                                       public void toDie(){
 isDeath = true; 
gamemanager._instance.score += score;
}
      让分数显示在屏幕上(使用GUI Text):public Text txt_score;


void Start () {

instance = this;
txt_score = this.transform.FindChild ("Canvas").FindChild ("txt_score").GetComponent<Text> ();


}
void Update () {
txt_score.text = "Score:" + score;
}
       
   13. 敌机,背景音乐都是play on wake+loop,子弹只是play on wake                        


       炸弹的收集与显示:public Text txt_bomb;
                  
                         txt_bomb = this.transform.FindChild ("Canvas").FindChild ("txt_bomb").GetComponent<Text> ();

                          public void AddBomb(){
 
                         count++;
                        txt_bomb.text = "X" + count;
                               }
        炸弹的使用与减少显示:  public void UseABomb(){   //只要按下空格,就调用toDie方法
if (count > 0) {
count--;
txt_bomb.text = "X" + count;
} else {
txt_bomb.gameObject.SetActive (false);
}
}
void Update () {
if (Input.GetKeyDown (KeyCode.Space) && Bombmanager._instance.count > 0) {
    this.UseABomb ();
}
             enemy.cs:       if (Input.GetKeyDown (KeyCode.Space) && Bombmanager._instance.count > 0) {
                toDie ();
              }


*********************最重要的: 此时选中enemy脚本,检视面板中上方选择执行顺序,先添加enemy,再添加BombManager,应用一下即可。













































































































































































































































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屠变恶龙之人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值