Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

Spaceship fires lazer from center of sprite and NOT from the front

$
0
0

@diliupg wrote:

I have a strnge problem. When I fire the spaceship lazer it ALWAYS fire from the center of the spaceship. The sprite rotation and directions are all ok. Please help to get the ship to fire from its front.
I create a lazer shot like this:
`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 the projectile is created like this.
`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);

}
`
and updated like this

`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;
 }`

can anyone help? Do you need images and all the code?

Posts: 12

Participants: 4

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles