首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >二维平台的联合拼贴

二维平台的联合拼贴
EN

Stack Overflow用户
提问于 2014-06-01 21:02:12
回答 1查看 732关注 0票数 0

我在网上学习一个教程,脚本基本上是告诉gameObject,如果摄像机到达某个位置,就实例化它自己的一个克隆。最后一句让我困惑的是

代码语言:javascript
复制
  if (rightOrLeft > 0) 
    {
    newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
    }
else 
    {
    newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
    }
}

简而言之,我和这里的语法和数学不太一样。有谁能帮我清理一下吗?

代码语言:javascript
复制
using UnityEngine;
using System.Collections;

[RequireComponent (typeof(SpriteRenderer))]

public class Tiling : MonoBehaviour {

public int offsetX = 2;         

public bool hasARightBuddy = false;
public bool hasALeftBuddy = false;

public bool reverseScale = false;   


private float spriteWidth = 0f;     

private Camera cam;
private Transform myTransform;

void Awake () {
    cam = Camera.main;
    myTransform = transform;
}

// Use this for initialization
void Start () {
    SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
    spriteWidth = sRenderer.sprite.bounds.size.x;
}

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


    if (hasALeftBuddy == false || hasARightBuddy == false) 
    {

        float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;


        float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend; //sprite width/2..51.2  
                                                                                                         //(0 + 51.2 - 26.67315 = 24.52685)
                                                                                                         //(102.4 + 51.2 - 26.67315 = 126.92685) etc.. //clone of the myTransform
        float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend; // (0 - 51.2 + 26.67315 = -24.52685)
                                                                                                        // (-102.4 - 51.2 + 26.67315 = -126.92685) etc..//clone of the myTransform

        if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
        {
            MakeNewBuddy (1);
            hasARightBuddy = true;
        }
        else if (cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
        {
            MakeNewBuddy (-1);
            hasALeftBuddy = true;
        }
    }
}


void MakeNewBuddy (int rightOrLeft)
{

    Vector3 newPosition = new Vector3 (myTransform.position.x + spriteWidth * rightOrLeft, myTransform.position.y, myTransform.position.z);

    Transform newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;


    if (reverseScale == true) 
    {
        newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
    }


    if (rightOrLeft > 0) 
        {
        newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
        }
    else 
        {
        newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
        }
    }
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-01 21:06:47

在我看来,代码告诉当前的游戏对象,在创建新邻居时,它有一个邻居。

布尔值有一个邻接变量,可以很方便地计算出这个创建过程不需要再次发生:)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23984437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档