各位程序員好!我很抱歉打擾你,但我有一個學校項目是創建一個遊戲。我差不多完成了,我所要做的就是讓我的角色(玩家)從敵方子彈中受到傷害。如何從敵人身上取得傷害(子彈)
這是我的敵人級別,我有我的子彈名單,這使得敵人射擊子彈。你可以看到我試圖跟蹤子彈的位置(我跟着這些教程關於使敵人https://www.youtube.com/watch?v=_TlnUM-uhSI和https://www.youtube.com/watch?v=tfiKwOo_4xo)
在你跳上我之前我只是想說我已經在各地搜索了一個答案,但因爲XNA對我想創建的內容非常有限,所以對我來說非常困難。
class Enemies
{
public Texture2D texture;
public Vector2 position;
public Vector2 velocity;
public bool isVisible = true;
Random random = new Random();
int randX, randY;
float temp_bulletenemyX;
float temp_bulletenemyY;
// bullets
private List<Bullets> bullets = new List<Bullets>();
Texture2D bulletTexture;
public Enemies(Texture2D NewTexture, Vector2 NewPosition, Texture2D newBulletTexture)
{
texture = NewTexture;
position = NewPosition;
randY = random.Next(-4, 4);
randX = random.Next(-4, -1);
velocity = new Vector2(randX, randY);
bulletTexture = newBulletTexture;
}
float shoot = 0;
public void update(GraphicsDevice graphics, GameTime gameTime)
{
KeyboardState keyboardState = Keyboard.GetState();
position += velocity;
if (position.Y <= 0 || position.Y >= graphics.Viewport.Height - texture.Height)
velocity.Y = -velocity.Y;
if (position.X < 0 - texture.Width)
isVisible = false;
shoot += (float)gameTime.ElapsedGameTime.TotalSeconds;
if (shoot > 1)
{
shoot = 0;
Shootbullet();
}
updateBullets();
}
public void updateBullets()
{
foreach (Bullets bullet in bullets)
{
bullet.position += bullet.velocity;
temp_bulletenemyX = bullet.position.X;
temp_bulletenemyY = bullet.position.Y;
if (bullet.position.X < 0)
bullet.isVisible = false;
}
for (int i = 0; i < bullets.Count; i++)
if(!bullets[i].isVisible)
{
bullets.RemoveAt(i);
i--;
}
}
public void Shootbullet()
{
Bullets newBullet = new Bullets(bulletTexture);
newBullet.velocity.X = velocity.X - 3f;
newBullet.position = new Vector2(position.X + newBullet.velocity.X, position.Y + (texture.Height/2) - (bulletTexture.Height/2));
newBullet.isVisible = true;
if (bullets.Count() < 3) // hur många skott den skall skjuta
bullets.Add(newBullet);
}
public void draw(SpriteBatch spriteBatch)
{
foreach (Bullets bullet in bullets)
bullet.Draw(spriteBatch);
spriteBatch.Draw(texture, position, Color.White);
}
public float PosX
{
get
{
return position.X;
}
}
public Vector2 Pos
{
get
{
return position;
}
}
public float PosY
{
get
{
return position.Y;
}
}
public List<Bullets> GetbulletList
{
get{
return bullets;
}
}
public Texture2D text
{
get
{
return texture;
}
}
public Texture2D BulletText
{
get
{
return bulletTexture;
}
繼承人,我已經試過了根本的Game1代碼,以使我的性格可能受到傷害。
player.rectangle = new Rectangle(Convert.ToInt32(player.PosX), Convert.ToInt32(player.PosY), player.text.Width, player.text.Height);
foreach (Enemies bullet in enemies.ToList())
{
rec_bullet = new Rectangle(Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.X), Convert.ToInt32(bullet.GetbulletList.ElementAt(i).position.Y), nyenemy.BulletText.Width, nyenemy.BulletText.Height);
hit = CheckCollision(rec_bullet, player.rectangle);
if (hit == true)
{
player.health -= 10;
hit = false;
}
i++;
如果一切都很混亂,我非常抱歉,我已經通過遵循許多教程和一些我自己的代碼將所有內容放在一起。如果我違反論壇規則,我也很抱歉,我在這裏是新的。
最好的問候基亞。
您必須爲我們提供CheckCollision的代碼,並告訴我們您的代碼如何工作?子彈是否會通過你的球員或出現錯誤,或其他事情發生?你的子彈是飛行還是站立?你的球員可以移動嗎?或者是如何使它們一起工作的想法中的問題?請更準確地說明問題。 – Monset
另外,您應該嘗試通過調試器運行源代碼。使用Visual Studio進行調試非常簡單,並且可以幫助您瞭解值的含義以及發生的情況。如果在windows和visual studio下調試打印將有所幫助 - Console.WriteLine()將允許您打印出變量:當一個項目符號處於活動狀態時,它就是X/Y,例如當它死亡時。 – Neil