我想從我的Player類控件調用我的ProjectileManager類中的'拍攝激光'功能。XNA C#從其他類與調用函數列表
事情是,當我從我的ProjectileManager或Game1(主遊戲)類調用完全相同的功能 - 它的作品。否則我會在精靈上得到一個空值。
我對projectilemanager類的sprite做了一個content.load,當我從projectilemanager本身或主遊戲調用射擊激光功能時工作正常 - 但不是玩家類?
注:香港專業教育學院切出很多的這些類中毫無意義的附加功能對於可讀性
Game1類
public class Game1 : Microsoft.Xna.Framework.Game
{
// CLASSES
Player myPlayer;
ProjectileManager projectileManager = new ProjectileManager();
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
projectileManager.ContentLoad(Content);
}
protected override void Update(GameTime gameTime)
{
if (Keyboard.GetState().IsKeyDown(Keys.V)) //SHOOT LASER TEST - WORKS
{
projectileManager.ShootLaser(new Vector2(5, 5), 5, 1, new Vector2(0, 0), 0, 0);
}
myPlayer.Update();
projectileManager.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.SamplerStates[0] = noFilter;
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null);
myPlayer.Draw(spriteBatch);
projectileManager.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
Player類
class Player
{
// Inherent classes
ProjectileManager projectileManager = new ProjectileManager();
Texture2D sprite;
public Vector2 direction;
float speed;
float health;
int spriteType;
public void Update()
{
UpdateControls();
}
void UpdateControls()
{
if (Keyboard.GetState().IsKeyDown(Keys.Space)) //SHOOT LASER - DOESNT WORK
{
projectileManager.ShootLaser(new Vector2(5, 5), 5, 1, new Vector2(0, 0), 0, 0);
}
}
}
彈Manager類
class ProjectileManager
{
//Lists
List<Projectile> projectiles = new List<Projectile>();
//Sprites
Texture2D sprite;
Texture2D spriteLaser;
//Attributes
Vector2 position;
Vector2 dimensions;
int attackPower;
int moveType; //0 = direction based, 1 = homing on player, 3 = hybrid of both (like a missile that fires straight then turns towards target
int team; //the team that the laser is on for collision
Vector2 direction;
float speed;
public ProjectileManager(){}
public void Update()
{
if (Keyboard.GetState().IsKeyDown(Keys.E))
{
ShootLaser(new Vector2(5,5), 5, 1, new Vector2(0,0), 0, 0);
}
foreach (Projectile each in projectiles)
{
each.Update();
}
}
public void ContentLoad(ContentManager content)
{
spriteLaser = content.Load<Texture2D>("playerLaser");
}
public void Draw(SpriteBatch spritebatch)
{
foreach (Projectile each in projectiles)
{
each.Draw(spritebatch);
}
}
public void ShootLaser (Vector2 a_position, int a_attackPower, float a_speed, Vector2 a_direction, int a_moveType, int a_team)
{
position = a_position;
attackPower = a_attackPower;
speed = a_speed;
direction = a_direction;
moveType = a_moveType;
team = a_team;
projectiles.Add(new Projectile(spriteLaser, position, 5, 2, new Vector2(0,1), 0, 0));
}
}
拋射類
class Projectile
{
Vector2 position;
int attackPower;
int moveType; //0 = direction based, 1 = homing on player, 3 = hybrid of both (like a missile that fires straight then turns towards target
int team; //the team that the laser is on for collision
Texture2D sprite;
Vector2 direction;
float speed;
public Projectile(Texture2D a_sprite, Vector2 a_position, int a_attackPower, float a_speed, Vector2 a_direction, int a_moveType, int a_team) //possibly add another variable for hitting other projetiles/cancelling them out
{
sprite = a_sprite;
position = a_position;
attackPower = a_attackPower;
speed = a_speed;
direction = a_direction;
moveType = a_moveType;
team = a_team;
}
public void Update()
{
//movement
// if movetype = 1
if (moveType == 0) // straight line
{
position.Y++;
//position += direction * speed;
}
}
//public Rectangle GetRectangle()
//{
// return new Rectangle((int)m_position.X, (int)m_position.Y, (int)m_dimensions.X, (int)m_dimensions.Y);
//}
// We pass-in a 'SpriteBatch' object so this function
// can call the 'Draw' function on it.
public void Draw(SpriteBatch spritebatch)
{
spritebatch.Draw(sprite, position, Color.White);
}
}
我剛開始學習C#,我找不到爲什麼在遊戲和彈丸管理類的shootlaser功能工作的任何引用,但是從球員的產量呼叫無結果。
香港專業教育學院試圖調試使用斷點,從我可以收集彈丸的Texture2D精靈返回一個空當功能是通過播放器類叫做..
的思考?
感謝
http://speedy.sh/RedMH/PIXLSPODE.zip 這裏是C#XNA項目,壞的編碼的回合84KB,但如果它有助於 – Swinny 2014-09-25 05:16:28
該網站看起來黑幕。你應該開始一個[github](https://github.com/)帳戶並共享一個鏈接。 – RadioSpace 2014-09-25 05:25:31
https:// github。COM/bswinbanks/PIXLSPLODE_ 做,做了:) – Swinny 2014-09-25 05:49:33