📄 sprite.cpp
字号:
/*
* Sprite.cpp
*
* Created on: 2009-3-3
* Author: pengxingfu
*/
#include "Sprite.h"
CSprite * CSprite::NewL(TInt aXVelocity,TInt aYVelocity, const TPoint& aInitialPosition)
{
CSprite* self = NewLC(aXVelocity,aYVelocity,aInitialPosition);
CleanupStack::Pop(self);
return self;
}
CSprite * CSprite::NewLC(TInt aXVelocity,TInt aYVelocity, const TPoint& aInitialPosition)
{
CSprite* self = new (ELeave) CSprite(aXVelocity,aYVelocity,aInitialPosition);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
CSprite::CSprite(TInt aXVelocity,TInt aYVelocity, const TPoint& aInitialPosition)
: iPosition(aInitialPosition),
iXVelocity(aXVelocity),
iYVelocity(aYVelocity)
{
// No implementation required
}
CSprite::~CSprite()
{
// No implementation required
}
void CSprite::ConstructL()
{
// No implementation required
}
TPoint CSprite::Position() const
{
return iPosition;
}
void CSprite::Move(const TSize& aSize, const TRect& aBounds)
{
//okToMove 判断是否将当前的iPosition置为移动一个速度单位(x,y轴均改变)后的座标
TBool okToMove = ETrue;
// Calculate the new sprite position
//算出新的左上角的位置
TPoint newPostion = iPosition + TPoint(iXVelocity,iYVelocity);
// Check that the new sprite X position is not out of bounds
TInt width = aSize.iWidth;
TInt leftX = newPostion.iX;
TInt rightX = newPostion.iX + width;
//如果图形的最左边X座标小于 Rect()的最左端,跑出手机屏幕的左边去了
//或跑出手机屏幕的右边去了,则将x轴速度反向
if ((rightX > aBounds.iBr.iX) || (leftX < aBounds.iTl.iX))
{
// If it is, "bounce" the sprite off the edge of the boundary
iXVelocity = -iXVelocity;
// and don't let it move this tick
okToMove = EFalse;
}
// Check that the new sprite Y position is not out of bounds
TInt height = aSize.iHeight;
TInt topY = newPostion.iY;
TInt bottomY = newPostion.iY + height;
if ((bottomY > aBounds.iBr.iY) || (topY < aBounds.iTl.iY))
{
// If it is, "bounce" the sprite off the edge of the boundary
iYVelocity = -iYVelocity;
// and don't let it move this tick
okToMove = EFalse;
}
// If the sprite can move this tick
//如果经过比较后没有跑到屏幕外面去,预先的移动就是有效的,只需将球的位置改变成移动后的值即可
if (okToMove)
{
// Move it
iPosition = newPostion;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -