Problems with my 2D animation

Started by
7 comments, last by JoshBaker 13 years, 1 month ago
Hi ,
Ive recently done a class which i can recreate to animate however and howmany sprites as i like.
Problem is , the width and height seem to be getting horrible wrong and are never aligned properly!

Animated class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace FirstGame
{
public class AnimatedSprite
{
public Texture2D Texture { get; set; }
public int Rows { get; set; }
public int Columns { get; set; }
private int currentFrame;
private int totalFrames;


public AnimatedSprite(Texture2D texture, int rows, int columns)
{
Texture = texture;
Rows = rows;
Columns = columns;
currentFrame = 0;
totalFrames = Rows * Columns;
}

public void Update()
{

currentFrame++;
if (currentFrame == totalFrames)
currentFrame = 0;
}

public void Draw(SpriteBatch spriteBatch, Vector2 location)
{
int width = Texture.Width / Rows;
int height = Texture.Height / Columns;
int row = (int)((float)currentFrame / (float)Columns);

int column = currentFrame % Columns;




Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);





spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);



}
}}



animatedBullet = new AnimatedSprite(Bullet, 1, 10);
animatedShield = new AnimatedSprite(Shield, 2, 8);


animatedBullet.Update();
animatedShield.Update();



animatedBullet.Draw(spriteBatch, FixOrigin(SpritePosition));
animatedShield.Draw(spriteBatch, shieldOffset);



This is used in my main Game.cs



As you can see the shield is cut off at the top and no matter what kind of numbers i put in , it never comes out right! :@


helpof.png

Uploaded with ImageShack.us
Advertisement
Bump? Still no help?....
If someone doesn't answer this before I get home, ill check it out and try and find a solution for you.
The only thing that catches my eye is:

int width = Texture.Width / Rows;
int height = Texture.Height / Columns;


The 'Rows' and 'Columns' need to be swapped.

The only thing that catches my eye is:

int width = Texture.Width / Rows;
int height = Texture.Height / Columns;


The 'Rows' and 'Columns' need to be swapped.


These seems to have fixed it!
Thank you man...

Just one more problem , the animations are playing way to fast... Any easy way around this without completely slowing down the entire game?
Okay i added a timeinterval and made the class add a frame every time the counter reached 500 milliseconds.
This is making the Animation working good although now ive hit another problem...

As soon as i have more than 1 thing animating (deriving from the same class) the game decides to stop animating the previous sprite sheet...


talk about problem after problem :|

ill post code as soon as im back from Uni :)...
Sorry to bring this back up.

But i never fixed my problem...

when one of my animating.update is occuring , the other doesnt work...


helpb.png

Uploaded with ImageShack.us

Here the shield is animating , but when i fire , my laser doesnt animate?

if (powerCollision == true && powerUp1Collected == true)
{


animatedShield.Update();


}



shield animating...

if (FireRate < 100 && RedFireMode == true)
{
animatedBullet.Update();

}


Firing animating...

These do not work together?...

for (i = 0; i < 20; i++)
{
if (aEnemy.x > BulletPosition.X && aEnemy.y > BulletPosition.Y && aEnemy.y < BulletPosition.Y && RedFireMode == true && aEnemy.enemyAlive == true)
{

aEnemy.enemyAlive = false;
BulletPosition.X = -5000;
BulletPosition.Y = -5000;



}



This code if for my enemy collision...
My bullet only fires on the X axis ( its a laser...)
yet the collision is awful and don't know why?

Please help!
Well, for one...

aEnemy.y > BulletPosition.Y && aEnemy.y < BulletPosition.Y

This here will always fail as the enemy's y position cannot be larger AND smaller than the bullet's y position at the same time. Check to see if the enemy's position falls within a range instead. For example...

aEnemy.y > (BulletPosition.Y + 8) && aEnemy.y < (BulletPosition.Y - 8)

Or simpler still, just test to see if the difference between the bullet and the enemy's positions is less than a certain size.

abs(aEnemy.y - BulletPosition.Y) < 8

Well, for one...

aEnemy.y > BulletPosition.Y && aEnemy.y < BulletPosition.Y

This here will always fail as the enemy's y position cannot be larger AND smaller than the bullet's y position at the same time. Check to see if the enemy's position falls within a range instead. For example...

aEnemy.y > (BulletPosition.Y + 8) && aEnemy.y < (BulletPosition.Y - 8)

Or simpler still, just test to see if the difference between the bullet and the enemy's positions is less than a certain size.

abs(aEnemy.y - BulletPosition.Y) < 8

That worked thanks!
But im still having problems with having two things animate at the same time? :| Anyone had this problem before...

This topic is closed to new replies.

Advertisement