A计划学习--贪吃蛇笔记

1.用image添加button组件代替button
2.控制物体移动不一定要写在方法里,可以用invokerepeating
方法,想要修改变量时可以cancelinvoke()先暂停再修改
3.食物的随机生成可以让每次生成的食物的图片用数组存储
然后获取图片组件替换。
4.单例模式,用于吃完食物后生成新食物,现在准备调用的
食物生成脚本里创建 public static 类名 变量名 。然后
通过变量名访问。(要理解会用)
5.判断奇偶可以用总长度除2(body.count%2==)?0:1;
6.设置图片getcomponent<image>().sprite
7.设置位置索引然后把位置加入索引list.add();
8.colorutility.tryparsehtmlstring("",out tempcolor)
尝试解析颜色
9.活用switch() case 
10.timescale是指游戏的缩放比例等于0时即可让游戏暂停
11.用cancelnvoke()可以暂停调用方法。
12.练习携程Ienumerator  yield return new waitforsecond
等待多长时间,startcoroutine(方法(时间))。
13.playerprefs.setint 保存数据。
14.resources 加载资源用的文件夹resource.load(string path)
方法加载资源,path的书写不需要加resources/以及文件扩展名
resource.load<类型>();
15.foreach在unity中的应用foreach(transform t in gameobject.transform)

16.四元数控制旋转Quaternion.euler(0.0.0);

核心算法:

  void Grow()          //实例化蛇身
    {
        AudioSource.PlayClipAtPoint(Eatclip, Vector3.zero);
       int index = (bodyList.Count % 2 == 0 )? 0 : 1;
      // Debug.Log(index);
        GameObject body = Instantiate(bodyPrefabs,new Vector3 (2000,2000,0),Quaternion .identity );
        body.GetComponent<Image>().sprite = bodySprite[index];
        body.transform.SetParent(Cancas, false);
        bodyList.Add(body.transform);
    }

public void Move()
    {
        HeadPos = transform.localPosition;                                              //记录头部位置
        transform.localPosition = new Vector3(HeadPos.x + x, HeadPos.y + y, HeadPos .z);//控制移动
        if(bodyList .Count > 0)
        {
            for (int i=bodyList .Count - 2; i >=0; i--)
            {
               bodyList[i + 1].localPosition = bodyList[i].localPosition;
           }
            bodyList[0].localPosition = HeadPos;
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_35711014/article/details/79434025