84 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			84 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C++
		
	
	
	
|  | #include "..\easy2d.h"
 | |||
|  | #include "..\Win\winbase.h"
 | |||
|  | 
 | |||
|  | ActionFrames::ActionFrames() : | |||
|  | 	m_nFrameIndex(0) | |||
|  | { | |||
|  | 	// ֡<><D6A1><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC> .5s ˢ<><CBA2>һ<EFBFBD><D2BB>
 | |||
|  | 	setInterval(500); | |||
|  | } | |||
|  | 
 | |||
|  | ActionFrames::ActionFrames(LONGLONG frameDelay) : | |||
|  | 	m_nFrameIndex(0) | |||
|  | { | |||
|  | 	setInterval(frameDelay); | |||
|  | } | |||
|  | 
 | |||
|  | ActionFrames::~ActionFrames() | |||
|  | { | |||
|  | 	for (auto frame : m_vFrames) | |||
|  | 	{ | |||
|  | 		frame->autoRelease(); | |||
|  | 		frame->release(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void ActionFrames::_init() | |||
|  | { | |||
|  | 	Action::_init(); | |||
|  | 	// <20><>¼<EFBFBD><C2BC>ǰʱ<C7B0><CAB1>
 | |||
|  | 	m_nLast = steady_clock::now(); | |||
|  | } | |||
|  | 
 | |||
|  | void ActionFrames::_exec(steady_clock::time_point nNow) | |||
|  | { | |||
|  | 	// <20>ж<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>㹻
 | |||
|  | 	while (duration_cast<milliseconds>(nNow - m_nLast).count() > m_nAnimationInterval) | |||
|  | 	{ | |||
|  | 		// <20><><EFBFBD>¼<EFBFBD>¼ʱ<C2BC><CAB1>
 | |||
|  | 		m_nLast += milliseconds(m_nAnimationInterval); | |||
|  | 		m_pTargetSprite->setImage(m_vFrames[m_nFrameIndex]); | |||
|  | 		m_nFrameIndex++; | |||
|  | 		// <20>ж϶<D0B6><CFB6><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 		if (m_nFrameIndex == m_vFrames.size()) | |||
|  | 		{ | |||
|  | 			this->stop(); | |||
|  | 			break; | |||
|  | 		} | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | void ActionFrames::_reset() | |||
|  | { | |||
|  | 	Action::_reset(); | |||
|  | 	m_nFrameIndex = 0; | |||
|  | 	// <20><>¼<EFBFBD><C2BC>ǰʱ<C7B0><CAB1>
 | |||
|  | 	m_nLast = steady_clock::now(); | |||
|  | } | |||
|  | 
 | |||
|  | void ActionFrames::addFrame(Image * frame) | |||
|  | { | |||
|  | 	if (frame) | |||
|  | 	{ | |||
|  | 		m_vFrames.push_back(frame); | |||
|  | 		frame->retain(); | |||
|  | 	} | |||
|  | } | |||
|  | 
 | |||
|  | ActionFrames * ActionFrames::copy() const | |||
|  | { | |||
|  | 	auto a = new ActionFrames(this->m_nAnimationInterval); | |||
|  | 	for (auto f : m_vFrames) | |||
|  | 	{ | |||
|  | 		a->addFrame(f); | |||
|  | 	} | |||
|  | 	return a; | |||
|  | } | |||
|  | 
 | |||
|  | ActionFrames * ActionFrames::reverse() const | |||
|  | { | |||
|  | 	auto a = this->copy(); | |||
|  | 	a->m_vFrames.reserve(m_vFrames.size()); | |||
|  | 	return a; | |||
|  | } |