⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sprite.cpp

📁 S60培训教材代码(连载)
💻 CPP
字号:
/* Copyright (c) 2001, Nokia Mobile Phones. All rights reserved */

#include "Sprite.h"


// Standard Epoc construction sequence
CSprite* CSprite::NewL(TInt aXVelocity,TInt aYVelocity, const TPoint& aInitialPosition)
    {
    CSprite* self = CSprite::NewLC(aXVelocity,aYVelocity,aInitialPosition);
    CleanupStack::Pop();
    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)
    {
    }

// destructor
CSprite::~CSprite()
    {
    }

void CSprite::ConstructL()
    {

    }

TPoint CSprite::Position() const
    {
    // access sprite position
    return iPosition;
    }

void CSprite::SetPosition(const TPoint& aNewPosition)
    {
    // set sprite position
    iPosition = aNewPosition;
    }
void CSprite::SetXVelocity(TInt aNewXVel)
    {
    // set sprite object's speed on X axis
    iXVelocity = aNewXVel;
    }

void CSprite::SetYVelocity(TInt aNewYVel)
    {
    // set sprite object's speed on Y axis
    iYVelocity = aNewYVel;
    }

TInt CSprite::GetXVelocity() const
    {
    // access sprite object's speed on X axis
    return iXVelocity;
    }

TInt CSprite::GetYVelocity() const
    {
    // access sprite object's speed on Y axis
    return iYVelocity;
    }

void CSprite::SetBounceCount(TInt aNewCount)
    {
    // reset bounce counter
    iBounceCount = aNewCount;
    }

TInt CSprite::GetBounceCount() const
    {
    // access sprite object's bounce count
    return iBounceCount;
    }

void CSprite::Move(const TSize& aSize, const TRect& aBounds)
    {
    TBool okToMove = ETrue;
    
    // Calculate the new sprite position we are trying to move to
    TPoint newPosition = iPosition + TPoint(iXVelocity,iYVelocity);
    
    // Check that the new sprite X position is not out of bounds
    TInt width = aSize.iWidth;
    TInt leftX = newPosition.iX;
    TInt rightX = newPosition.iX + width;
    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 
        okToMove = EFalse;
        // increment the bounce counter
        iBounceCount++;
        }

    // Check that the new sprite Y position is not out of bounds
    TInt height = aSize.iHeight;
    TInt topY = newPosition.iY;
    TInt bottomY = newPosition.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 
        okToMove = EFalse;
        // increment the bounce counter
        iBounceCount++;
        }

    // If the sprite can move 
    if (okToMove)
        {
        // Move it
        iPosition = newPosition;
        }
    }



    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -