2014-09-25 61 views
1

我想從我的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精靈返回一個空當功能是通過播放器類叫做..

的思考?

感謝

+0

http://speedy.sh/RedMH/PIXLSPODE.zip 這裏是C#XNA項目,壞的編碼的回合84KB,但如果它有助於 – Swinny 2014-09-25 05:16:28

+0

該網站看起來黑幕。你應該開始一個[github](https://github.com/)帳戶並共享一個鏈接。 – RadioSpace 2014-09-25 05:25:31

+0

https:// github。COM/bswinbanks/PIXLSPLODE_ 做,做了:) – Swinny 2014-09-25 05:49:33

回答

0

添加ContentLoad方法您Player類並調用projectileManager.ContentLoad有則調用myPlayer.ContentLoadGame1.LoadContent方法

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
// CLASSES 
Player myPlayer; 



protected override void LoadContent() 
{ 
    spriteBatch = new SpriteBatch(GraphicsDevice); 
    myPlayer.ContentLoad(CoolContent); 
} 

播放器類:

class Player 
{ 

// Inherent classes 
ProjectileManager projectileManager = new ProjectileManager(); 

Texture2D sprite; 
public Vector2 direction; 
float speed; 
float health; 
int spriteType; 

public void LoadContent(CoolContentType coolContent) 
{ 
    projectileManager.LoadContent(coolContent); 
} 

public void Update() 
{ 

    UpdateControls(); 

} 

除了連接您的ContentLoad方法調用。你應該擺脫Projectile類中的Texture2D,因爲它是多餘的。

所有你需要的是來自Draw調用中的投射物的值,而ProjectileManager存儲紋理。

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) 
{ 
    spritebatch.Begin(); 

    foreach (Projectile p in projectiles) 
    { 
     spritebatch.draw(spriteLaser, p.position,Color.CoolColor);//see how I use the projectile info (p.position). it doesn't need it's own texture :) 
    } 

    spritebatch.End(); 
} 
+0

剛剛嘗試了這一點,通過添加斷點我可以看到,當我使用Player類控件時,拋射類的更新和繪製方法甚至不會運行。 – Swinny 2014-09-25 05:04:23

+0

http://speedy.sh/RedMH/PIXLSPODE.zip 如果有人想有一個快速看問題 – Swinny 2014-09-25 05:15:33

+0

三江源, 我之所以彈丸本身內部的Texture2D是因爲後來在軌道下我將有多種類型的子彈/激光,並希望在列表中定義它們,這樣我的所有子彈都在一個列表中處理。 添加ContentLoad的球員並沒有解決這個問題,似乎更新和繪製方法的arent甚至初始化時,我使用播放器類「攝影」功能。 並進一步明確,其排序很重要,我得到這個工作,我將使用相同的修復讓我的敵人的AI拍攝獨立 – Swinny 2014-09-25 05:32:11