1
問題:在空格鍵按下時,我需要我的射彈發射並繼續其生命,無論另一次按下或放開空格鍵。XNA/C#射彈發射問題 - 需要一個射彈一次在空格鍵上發射新聞
下面是代碼添加,更新和繪製射彈。我已經嘗試過許多對此代碼的修改,但是我還沒有足夠的經驗來找到合適的解決方案。下面
輸入代碼在這裏下面
private void UpdateProjectiles()
{
//update projectiles
for (int i = projectiles.Count - 1; i >= 0; i--)
{
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
//adds particle to first projectile but not again until the next fire butotn press
//particleEngine.EmitterLocation = new Vector2(projectiles[i].Position.X, projectiles[i].Position.Y);
projectiles[i].Update();
projectileOn = true;
}
//if projectiles not being fired remove them from the game screen
else
{
projectiles.RemoveAt(i);
}
}
}
,吸引Draw方法
//add projectile if spacebar is pressed
private void AddProjectile(Vector2 position)
{
//i need somthing like this to make bullets autofire on phone
//while (TouchPanel.IsGestureAvailable)
//{
// Projectile projectile = new Projectile();
// projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
// projectiles.Add(projectile);
//}
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
Projectile projectile = new Projectile();
projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
projectiles.Add(projectile);
}
}
更新彈丸代碼
AddProjectile碼彈到屏幕
//draw the projectiles
//***********************************************************
//using the if here allows control of projectiles to pass...
//...to the "currentstate of the spacebar (is pressed = fire)
/***/
if (currentKeyboardState.IsKeyDown(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Pressed)
{
for (int i = 0; i < projectiles.Count; i++)
{
projectiles[i].Draw(spriteBatch);
}
}
//remove projectiles and reset
if (currentKeyboardState.IsKeyUp(Keys.Space) ||
currentGamePadState.Buttons.A == ButtonState.Released)
{
UpdateProjectiles();
}
所以,這是我到目前爲止所說的,並且如前所述可以使彈丸良好地工作,我不能讓他們繼續他們的生活(直到碰撞或他們到達屏幕的末端)一旦我放開鍵盤空格鍵。
嗨Jodacola,是的,謝謝,使我現在完全有意義,我會盡快回復你的建議的實施儘快現在我知道這種方法更好一點我想重寫我的代碼是abit scribbly和即時思考這些都可以重新分類成像精靈管理器和邏輯,輸入,級別經理等更好的父類,我可以問你,你認爲這種類型的模塊化是一個好辦法,因爲從長遠來看,它可能證明要更好地劃分?建議永遠受歡迎 – gruffy321 2012-02-27 14:00:12
@ gruffy321在大多數情況下,分區是最好的。理想情況下,兩個對象不需要知道對方的存在。當然,這有時是不可能的,但在這種情況下,您似乎可以通過重新設計類結構來改進代碼。 – annonymously 2012-02-28 11:04:46
感謝兄弟,你的意見顯然意味着很多現在已經重新編寫了所有東西,所以我可以在基礎類中處理Weponary,我的派生玩家精靈可以訪問,這個我雖然會創建一個場景,任何精靈都可以擁有子彈的能力,但是沒必要必須使用它等 – gruffy321 2012-02-28 18:06:45