#include "HelloWorldScene.h"
using namespace cocos2d;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
//1) Cache the sprite frames and texture
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("AnimBear.plist");
//2) Create a sprite batch node
CCSpriteBatchNode *spriteSheet = CCSpriteBatchNode::create("AnimBear.png");
this->addChild(spriteSheet);
//3) Gather the list of frames
CCArray *walkAnimFrames = CCArray::create();
for (int i = 1; i <= 8; ++i)
{
walkAnimFrames->addObject(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
CCString::createWithFormat("bear%d.png", i)->getCString()));
}
//4) Create the animation object
CCAnimation *walkAnim = CCAnimation::createWithSpriteFrames(walkAnimFrames, 0.1f);
//5) Create the sprite and run the animation action
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
this->setBear(CCSprite::createWithSpriteFrameName("bear1.png"));
this->getBear()->setPosition(ccp(winSize.width / 2, winSize.height / 2));
this->setWalkAction(CCRepeatForever::create(CCAnimate::create(walkAnim)));
//this->getBear()->runAction(this->getWalkAction());
spriteSheet->addChild(this->getBear());
this->setTouchEnabled(true);
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
HelloWorld::HelloWorld()
{
_bear = NULL;
_walkAction = NULL;
_moveAction = NULL;
_bearMoving = false;
}
HelloWorld::~HelloWorld()
{
}
void HelloWorld::registerWithTouchDispatcher()
{
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
return true;
}
void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
//1) Determine the touch location
CCPoint touchLocation = this->convertTouchToNodeSpace(pTouch);
//2) Set the desired velocity
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
float bearVelocity = screenSize.width / 3.0f;
//3) Figure out the amount moved in X and Y
CCPoint moveDifference = ccpSub(touchLocation, this->getBear()->getPosition());
//4) Figure out the actual length moved
float distanceToMove = ccpLength(moveDifference);
//5) Figure out how long it will take to move
float moveDuration = distanceToMove / bearVelocity;
//6) Flip the animation if necessary
if (moveDifference.x < 0)
{
this->getBear()->setFlipX(false);
}
else
{
this->getBear()->setFlipX(true);
}
//7) Run the appropriate actions
this->getBear()->stopAction(this->getMoveAction());
if (!_bearMoving)
{
this->getBear()->runAction(this->getWalkAction());
}
this->setMoveAction(CCSequence::create(
CCMoveTo::create(moveDuration, touchLocation),
CCCallFunc::create(this, callfunc_selector(HelloWorld::bearMoveEnded)),
NULL));
this->getBear()->runAction(this->getMoveAction());
_bearMoving = true;
}
void HelloWorld::cleanup()
{
CC_SAFE_RELEASE_NULL(_moveAction);
CCLayer::cleanup();
}
void HelloWorld::bearMoveEnded()
{
this->getBear()->stopAction(this->getWalkAction());
_bearMoving = false;
}
没有合适的资源?快使用搜索试试~ 我知道了~
AnimBear cocos2d-x
共40个文件
png:14个
dll:8个
plist:6个
5星 · 超过95%的资源 需积分: 9 160 下载量 17 浏览量
2013-08-12
15:28:48
上传
评论 6
收藏 2.43MB 7Z 举报
温馨提示
本文实践自 Ray Wenderlich、Tony Dahbura 的文章《How to Use Animations and Sprite Sheets in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建一个简单的熊行走动画,如何使用精灵表单,如何改变熊行走的方向等等。有关源码、资源等在文章下面给出了地址。http://blog.csdn.net/akof1314/article/details/9922581
资源推荐
资源详情
资源评论
收起资源包目录
AnimBearcocos2d-x-2.1.4.7z (40个子文件)
BearImagesHD
bear6.png 44KB
bear2.png 43KB
bear5.png 45KB
bear4.png 45KB
bear8.png 43KB
bear3.png 45KB
Thumbs.db 30KB
bear1.png 46KB
bear7.png 41KB
AnimBear
proj.win32
resource.h 496B
AnimBear.rc 2KB
AnimBear.win32.vcproj 6KB
res
AnimBear.ico 47KB
main.cpp 953B
Release.win32
Debug.win32
main.h 270B
Classes
AppDelegate.cpp 2KB
HelloWorldScene.h 1KB
HelloWorldScene.cpp 4KB
AppDelegate.h 960B
Resources
SD
AnimBear.plist 4KB
AnimBear.png 81KB
HD
AnimBear.plist 4KB
AnimBear.png 175KB
HDR
AnimBear.plist 4KB
AnimBear.png 337KB
bin
SD
AnimBear.plist 4KB
AnimBear.png 81KB
libtiff.dll 513KB
glew32.dll 324KB
HD
AnimBear.plist 4KB
AnimBear.png 175KB
zlib1.dll 76KB
iconv.dll 868KB
AnimBear.win32.exe 79KB
pthreadVCE2.dll 76KB
libcurl.dll 1.13MB
HDR
AnimBear.plist 4KB
AnimBear.png 337KB
libCocosDenshion.dll 424KB
libcocos2d.dll 1.56MB
共 40 条
- 1
无幻
- 粉丝: 3538
- 资源: 150
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页