魔法技能 和人物动画原理一样,下面是我的一个简单实现的源代码
/////////
//Magic.h
//
#pragma once
#include "Texture.h"
class CMagic
{
public:
LONG m_ClientX;
LONG m_ClientY;
CTexture* m_Textures;
int m_NumTextures;
int m_CurrentTexIndex;
virtual void Draw();
virtual void Load();
virtual void Update();
};
class CMoveMagic : public CMagic
{
public:
LONG m_OrigiClientX;
LONG m_OrigiClientY;
LONG m_EndClientX;
LONG m_EndClientY;
LONG m_CurrentClientX;
LONG m_CurrentClientY;
int m_Speend;
virtual void Draw();
virtual void Load();
virtual void Update();
};
////
//Magic.cpp
/////
#include "stdafx.h"
#include "Magic.h"
void CMagic::Draw()
{
CRect rect(0,0,m_Textures[m_CurrentTexIndex].m_Width,m_Textures[m_CurrentTexIndex].m_Height);
m_Textures[m_CurrentTexIndex].Draw(m_ClientX,m_ClientY,rect);
}
void CMagic::Load()
{
m_ClientX = 90;
m_ClientY = 0;
m_NumTextures = 10;
m_Textures = new CTexture[10];
m_CurrentTexIndex = 0;
m_Textures[0].LoadEx("D:\\2DGameEngine\\Magic\\000280.bmp",72,54);
m_Textures[1].LoadEx("D:\\2DGameEngine\\Magic\\000281.bmp",160,117);
m_Textures[2].LoadEx("D:\\2DGameEngine\\Magic\\000282.bmp",164,131);
m_Textures[3].LoadEx("D:\\2DGameEngine\\Magic\\000283.bmp",168,148);
m_Textures[4].LoadEx("D:\\2DGameEngine\\Magic\\000284.bmp",148,153);
m_Textures[5].LoadEx("D:\\2DGameEngine\\Magic\\000285.bmp",100,98);
m_Textures[6].LoadEx("D:\\2DGameEngine\\Magic\\000286.bmp",120,220);
m_Textures[7].LoadEx("D:\\2DGameEngine\\Magic\\000287.bmp",160,213);
m_Textures[8].LoadEx("D:\\2DGameEngine\\Magic\\000288.bmp",140,157);
m_Textures[9].LoadEx("D:\\2DGameEngine\\Magic\\000289.bmp",88,90);
}
void CMagic::Update()
{
m_CurrentTexIndex++;
if(m_CurrentTexIndex>9)
m_CurrentTexIndex = 0;
}
//////////////////////////////////////////////////////////////////////////
////////
void CMoveMagic::Draw()
{
CRect rect(0,0,m_Textures[m_CurrentTexIndex].m_Width,m_Textures[m_CurrentTexIndex].m_Height);
m_Textures[m_CurrentTexIndex].Draw(m_CurrentClientX,m_CurrentClientY,rect);
}
void CMoveMagic::Load()
{
m_NumTextures = 8;
m_Textures = new CTexture[8];
m_CurrentTexIndex = 0;
m_Textures[0].LoadEx("D:\\2DGameEngine\\Magic\\000180.bmp",40,36);
m_Textures[1].LoadEx("D:\\2DGameEngine\\Magic\\000181.bmp",40,22);
m_Textures[2].LoadEx("D:\\2DGameEngine\\Magic\\000182.bmp",40,23);
m_Textures[3].LoadEx("D:\\2DGameEngine\\Magic\\000183.bmp",40,36);
m_Textures[4].LoadEx("D:\\2DGameEngine\\Magic\\000184.bmp",36,50);
m_Textures[5].LoadEx("D:\\2DGameEngine\\Magic\\000185.bmp",40,37);
m_Textures[6].LoadEx("D:\\2DGameEngine\\Magic\\000186.bmp",40,23);
m_Textures[7].LoadEx("D:\\2DGameEngine\\Magic\\000187.bmp",40,21);
m_OrigiClientX = 0;
m_OrigiClientY = 80;
m_EndClientX = 280;
m_EndClientY = 80;
m_CurrentClientX = m_OrigiClientX;
m_CurrentClientY = m_OrigiClientY;
m_Speend = 30;
}
void CMoveMagic::Update()
{
m_CurrentClientX += m_Speend;
if(m_CurrentClientX>m_EndClientX)
m_CurrentClientX = 0;
m_CurrentTexIndex = (m_CurrentClientX-m_OrigiClientX)*7/(m_EndClientX-m_OrigiClientX);
}