我想从我的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