2014-09-25 91 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