부울 값을 가진 Tile
이라는 이름의 C# 클래스를 만들었습니다. getIsWall()
을 통해 액세스하려고하면 항상 True
이됩니다. 코드 첨부. GameObject가 올바른 부울 값을 반환하지 않습니다.
내가 true
에 isWall
을 설정하고 "무료"타일 내가 false
에 isWall
설정 "벽"에 대한 방법 createTile()
를 사용하여 게임 오브젝트의 100 × 100 격자를 생성 시작하기 전에
그런 다음 generateAisle
메서드에서 북쪽 (강제 방향 = 디버그의 경우 1) 통로를 만들고 싶습니다. 그리고 타일 3 개가 방법 checkIfWall()
을 사용하여 벽인지 확인합니다. 내가 다시 할 때 나는 항상 TRUE
이겠지만 때로는 타일이 FALSE
(게임이 실행되는 동안 확인)이라는 것을 안다.
누군가 나를 도울 수 있습니까? 감사! @ LearnCocos2D I ")는 GameObject.Find ("모두를 제거하고, 단순히 createTile에서 만들어진 타일() 및 저장 배열의 참조를 리턴의 제안 후
public void generateAisle(int x, int z, int lengthAisle){
bool free = false;
bool free2 = false;
bool free3 = false;
int direction = 1;//Random.Range (1, 4);
if (direction == 1) {//north
int tempZ = z+1;
for (int i=0; i<lengthAisle; i++) {
free = checkIfWall (x, tempZ+i);
free2 = checkIfWall ((x-1), tempZ+i);
free3 = checkIfWall ((x+1), tempZ+i);
if(!free || !free2 || !free3){
free = false;
break;
}
}
if(free){
for (int i=0; i<lengthAisle; i++) {
/*Destroy(GameObject.Find("Tile"+x+"."+(tempZ+i)));
createTile(x,z+i,"Free");*/
}
}
} else if (direction == 2) {//south
int tempZ = z-1;
for (int i=0; i<lengthAisle; i++) {
free = checkIfWall (x+i, tempZ);
free2 = checkIfWall (x-1+i, tempZ);
free3 = checkIfWall (x+1+i, tempZ);
if(!free || !free2 || !free3){
free = false;
break;
}
}
if(free){
for (int i=0; i<lengthAisle; i++) {
Destroy(GameObject.Find("Tile"+x+"."+(tempZ+i)));
createTile(x,z+i,"Free");
}
}
} else if (direction == 3) {//east
for (int i=0; i<lengthAisle; i++) {
free = checkIfWall (x, z + i);
}
} else if (direction == 4) {//west
for (int i=0; i<lengthAisle; i++) {
free = checkIfWall (x, (z - i));
}
}
}
private bool checkIfWall(int x, int z){
Debug.Log (GameObject.Find ("Tile" + x + "." + z).GetComponent<Tile>().getIsWall());
if (GameObject.Find ("Tile" + x + "." + z) != null) {
Debug.Log ("Tile" + x + "." + z + " = " + GameObject.Find ("Tile" + x + "." + z).GetComponent<Tile>().getIsWall());
if (GameObject.Find ("Tile" + x + "." + z).GetComponent<Tile>().getIsWall()) {
return true;
} else {
return false;
}
}else {
return false;
}
}
public void createTile(int x, int z, string kind="Wall"){
GameObject newTile = (GameObject)Instantiate(Resources.Load(kind, typeof(GameObject)));
newTile.name = "Tile"+x+"."+z;
newTile.gameObject.GetComponent<Tile>().setPosition(x,z);
if (kind == "Wall") {
newTile.transform.position = new Vector3 (x, 1, z);
newTile.GetComponent<Tile>().setWall(true);
}else{
newTile.transform.position = new Vector3 (x, 0, z);
newTile.GetComponent<Tile>().setWall(false);
}
}
getIsWall 기능을 게시하십시오. Find를 자주 수행하고 +로 문자열을 자주 추가하는 것은 매우 비효율적입니다. 객체를 한 번 "찾아야"하고 문자열을 구성하고 반복해서 찾지 않고 로컬 변수에 저장해야합니다. 또한 getIsWall이 true/false를 반환하는지 여부에 따라 true/false를 반환하는/else가 아닌 "return ... getIsWall()"을 수행 할 수도 있습니다. – LearnCocos2D
당신은 hte method checkIfWall에 대해 옳았습니다. 좋은 해결책은 아닙니다. 간단히하려고 노력할 것입니다. 그럼 당신의 제안을 따르 겠어, 내가 생성되면 배열에 gameobject에 대한 참조를 저장합니다! – Cappec