나는 게임을 만들고 있습니다. 나는 "Player"라는 객체를 만들었습니다. 플레이어 클래스는 다음과 같습니다사용자 지정 개체에 메서드를 추가하는 방법
게임에서public class Player
{
public Vector2 pos;
public Rectangle hitbox;
public Rectangle leftHitbox;
public Rectangle topHitbox;
public Rectangle bottomHitbox;
public Rectangle rightHitbox;
public Texture2D texture;
public Vector2 speed;
public bool canMoveLeft;
public bool canMoveRight;
public int vertSpeed;
public Player(Vector2 position, Texture2D tex)
{
pos = position;
texture = tex;
speed = new Vector2(1, 1);
vertSpeed = 0;
hitbox = new Rectangle((int) position.X, (int) position.Y, tex.Width, tex.Height);
leftHitbox = new Rectangle((int) pos.X, (int) pos.Y, 1, tex.Height);
topHitbox = new Rectangle((int) pos.X, (int) pos.Y, tex.Width, 1);
bottomHitbox = new Rectangle((int) pos.X, (int) (pos.Y + tex.Height), tex.Width, 1);
rightHitbox = new Rectangle();
canMoveLeft = true;
canMoveRight = true;
Debug.WriteLine("The texture height is {0} and the bottomHitbox Y is {1}", tex.Height, bottomHitbox.Y);
}
, 내가 같은 클래스에 넣어 이러한 방법을 사용하여 플레이어를 이동 : 당신이 볼 수 있듯이,
public static void MovePlayerToVector(Player player, Vector2 newPos)
{
player.pos = newPos;
UpdateHitboxes(player);
}
그러나,이 방법은 소요 플레이어 개체 및 pos
변수를 변경합니다. 이것을 객체를 확장하는 메소드로 만들 수있는 방법이 있습니까? 예를 들어, 플레이어를 이동하는 것은 같을 것이다 :이 대신
Player player = new Player(bla, bla);
player.MovePlayerToVector(new Vector2(1,1));
을 .. :
Player player = new Player(bla, bla);
Player.MovePlayerToVector(player, new Vector2(1,1));
을 .. 매우 비효율적된다.
나는 이것이 무엇인지 불리우며 Google이 그것을 할 수 없다. 도와주세요. 감사.
내가 10 분에 답변으로 표시 할 수 있습니다, 사이트의 나에게 빠른 응답에 대한 문제 :( 그리고 감사를 제공! – Ilan321