弹丸旋转和射击时出错 [英] Error in the projectile rotation and shoot
本文介绍了弹丸旋转和射击时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
代码在C#中,IDE是Visual Studio2017。我的代码和资产在 diliupg / Space_Battles - Bitbucket [ ^ ]。
当我从宇宙飞船中间发射射弹射击时,我的太空船旋转代码出了问题。没有其他错误。我怎样才能让宇宙飞船从前方开火?
我尝试过的事情:
当我按这样的空格时,我创建了激光镜头:
Code is in C# and the IDE is Visual Studio2017.my code and assets are at diliupg / Space_Battles — Bitbucket[^].
My Space ship rotation code has something wrong cause when I fire the projectile shoots from the middle of the spaceship. There are no other errors. How can I get the spaceship to fire from the front?
What I have tried:
I create the lazer shot when I press the space by like this:
<pre>
if (keyboard.IsKeyDown(Keys.Space) && shoot)
{
shoot = false;
//is.fireInst.Play();
this.fire.Play(.2f, .9f, 0);
lazerX = (float)(spritePosX - (0) * Math.Cos(rotationAngle));
lazerY = (float)(spritePosY - (0) * Math.Sin(rotationAngle));
Projectile projectile = new Projectile(heroLazer, "heroLazer",
lazerX, lazerY, rotationAngle);
Game1.AddProjectile(projectile);
}
// then in the projectile class:
public Projectile(Texture2D lazerSprite, string spriteName,
float posx, float posy, float heading)
{
projectileSprite = lazerSprite;
type = spriteName;
spriteRotOrigin = new Vector2(projectileSprite.Width / 2, projectileSprite.Height / 2);
spriteHeading = heading;
spritePosX = posx; // the x position of the lazer
spritePosY = posy; // the y position of the lazer
spritePos = new Vector2(spritePosX, spritePosY);
drawRectangle = new Rectangle((int)spritePosX, (int)spritePosY,
projectileSprite.Width / 2, projectileSprite.Height / 2);
}
// then in the update method
<pre>public void update(GameTime gameTime, KeyboardState keyboard)
{
//projectile active or not?
if (active == true)
{
projectileAge += gameTime.ElapsedGameTime.Milliseconds;
spritePosX += (float)(Math.Sin(spriteHeading) *
gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
spritePosY -= (float)(Math.Cos(spriteHeading) *
gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
spritePos = new Vector2(spritePosX, spritePosY);
}
if (projectileAge > projectileLife)
{
active = false;
}
}
// then in the draw method like this
<pre lang="c#">public void Draw(SpriteBatch spriteBatch)
{
//spriteBatch.Draw(projectileSprite, drawRectangle, Color.White);
spriteBatch.Draw(projectileSprite, spritePos, null,
Color.FromNonPremultiplied(255, 255, 255, 255), spriteHeading,
spriteRotOrigin, 1.0f, SpriteEffects.None, 0f);
}
推荐答案
我用以下代码解决了问题
I solved the problem with the following code
if (keyboard.IsKeyDown(Keys.Space) && shoot)
{
shoot = false;
this.fire.Play(.2f, .9f, 0);
// the code below will position a projectile at the top
// of any rotating sprite facing the sprite direction
lazerX = spritePosX + HeroSprite.Width / 2 * (float)Math.Sin(rotationAngle);
lazerY = spritePosY - HeroSprite.Height / 2 * (float)Math.Cos(rotationAngle);
Projectile projectile = new Projectile(heroLazer, "heroLazer",
lazerX, lazerY, rotationAngle);
Game1.AddProjectile(projectile);
}
然后使用此代码
and then with this code
public void update(GameTime gameTime, KeyboardState keyboard)
{
//projectile active or not?
if (active == true)
{
projectileAge += gameTime.ElapsedGameTime.Milliseconds;
ProjectilePosX += (float)(Math.Sin(spriteHeading) *
gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
ProjectilePosY -= (float)(Math.Cos(spriteHeading) *
gameTime.ElapsedGameTime.TotalMilliseconds) * ProjectileMoveAmount;
spritePos = new Vector2(ProjectilePosX, ProjectilePosY);
}
if (projectileAge > projectileLife)
{
active = false;
}
}
这篇关于弹丸旋转和射击时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文