#include "HelloWorldScene.h"
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::node();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::node();
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
{
//////////////////////////////////////////////////////////////////////////
// super init first
//////////////////////////////////////////////////////////////////////////
CC_BREAK_IF(! CCLayer::init());
//////////////////////////////////////////////////////////////////////////
// add your codes below...
//////////////////////////////////////////////////////////////////////////
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
//生成关闭按钮,并注册回调函数
CCMenuItemImage *pCloseItem = CCMenuItemImage::itemFromNormalImage(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
// 2. Add a label shows "Hello World".
// Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::labelWithString("New World", "Thonburi", 64);
CC_BREAK_IF(! pLabel);
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 20));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, 1);
// 3. Add add a splash screen, show the cocos2d splash image.
//CCSprite* pSprite = CCSprite::spriteWithFile("HelloWorld.png");
//CC_BREAK_IF(! pSprite);
// Place the sprite on the center of the screen
//pSprite->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
//this->addChild(pSprite, 0);
//加载游戏地图
map = CCTMXTiledMap::tiledMapWithTMXFile("0.tmx");
CC_BREAK_IF(! map);
this->addChild(map, 0);
//创建4个方向的行动动画模板
walkAnimations = new CCAnimation*[4];
walkAnimations[kDown] = createAnimationByDirection(kDown);
walkAnimations[kLeft] = createAnimationByDirection(kLeft);
walkAnimations[kRight] = createAnimationByDirection(kRight);
walkAnimations[kUp] = createAnimationByDirection(kUp);
//创建一个精灵
heroSprite = CCSprite::spriteWithSpriteFrame(walkAnimations[kDown]->getFrames()->getObjectAtIndex(0));
//设置人物的锚点为(0,0), 超始位置为(1,11)
heroSprite->setAnchorPoint(CCPointZero);
heroSprite->setPosition(positionForTileCoord(ccp(1, 11)));
this->addChild(heroSprite);
//让精灵执行动作————播放动画
//heroSprite->runAction(CCRepeatForever::actionWithAction(animate));
//创建上下左右按钮
CCMenuItem *down = CCMenuItemFont::itemFromString("down", this, menu_selector(HelloWorld::menuCallBackMove));
CCMenuItem *left = CCMenuItemFont::itemFromString("left", this, menu_selector(HelloWorld::menuCallBackMove));
CCMenuItem *right = CCMenuItemFont::itemFromString("right", this, menu_selector(HelloWorld::menuCallBackMove));
CCMenuItem *up = CCMenuItemFont::itemFromString("up", this, menu_selector(HelloWorld::menuCallBackMove));
CCMenu *menu = CCMenu::menuWithItems(down, left, right, up, NULL);
//为了方便查找,给每个menuitem 设置一个tag
down->setTag(kDown);
left->setTag(kLeft);
right->setTag(kRight);
up->setTag(kUp);
//菜单项按间距水平排列
menu->alignItemsVerticallyWithPadding(50);
this->addChild(menu);
isHeroWalking = false;
//设置定时器为每帧调用
schedule(schedule_selector(HelloWorld::update));
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
CCAnimation * HelloWorld::createAnimationByDirection(int direction)
{
//创造人物的纹理
CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("hero.png");
//用纹理创建4帧动画
CCSpriteFrame *frame0, *frame1, *frame2, *frame3;
frame0 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 32*direction, 32, 32));
frame1 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*1, 32*direction, 32, 32));
frame2 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*2, 32*direction, 32, 32));
frame3 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*3, 32*direction, 32, 32));
CCMutableArray<CCSpriteFrame *> *animFrames = new CCMutableArray<CCSpriteFrame*>(4);
animFrames->addObject(frame0);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);
//创建一个动画对象
CCAnimation *animation = new CCAnimation();
animation->initWithFrames(animFrames, 0.2f); //0.2表示每帧动画的间隔
animFrames->release();
return animation;
}
void HelloWorld::menuCallBackMove(CCObject *pSender)
{
if(isHeroWalking)
return;
isHeroWalking = true;
CCNode *node = (CCNode *)pSender;
//按钮的tag就是要行走的方向
int targetDirection = node->getTag();
//移动的距离
CCPoint moveByDirection;
switch(targetDirection)
{
case kDown:
moveByDirection = ccp(0, -32);
break;
case kLeft:
moveByDirection = ccp(-32, 0);
break;
case kRight:
moveByDirection = ccp(32, -0);
break;
case kUp:
moveByDirection = ccp(0, 32);
break;
}
CCPoint targetPosition = ccpAdd(heroSprite->getPosition(), moveByDirection);
//如果为边界或墙,则只需设置人物的朝向
if(checkCollision(targetPosition) == kWall)
{
setFaceDirection((HeroDirection)targetDirection);
isHeroWalking = false;
return ;
}
CCAction *action = CCSequence::actions(
CCSpawn::actions(
CCAnimate::actionWithAnimation(walkAnimations[targetDirection], false),
CCMoveBy::actionWithDuration(0.28f, moveByDirection), NULL
),
CCCallFuncND::actionWithTarget(this, callfuncND_selector(HelloWorld::onWalkDone),
(void*)targetDirection), NULL);
heroSprite->runAction(action);
}
void HelloWorld::setFaceDirection(HeroDirection direction)
{
heroSprite->setTextureRect(CCRectMake(0, 32*direction, 32, 32));
}
void HelloWorld::onWalkDone(CCNode *pTarget, void *data)
{
int indirection = (int)data;
setFaceDirection((HeroDirection)indirection);
isHeroWalking = false;
setSceneScrollPosition(heroSprite->getPosition());
}
CCPoint HelloWorld::positionForTileCoord(CCPoint tileCoord)
{
CCPoint pos = ccp((tileCoord.x * map->getTileSize().width),
((map->getMapSize().height - tileCoord.y -1) * map->getTileSize().height));
return pos;
}
CCPoint HelloWorld::tileCoordForPosition(CCPoint position)
{
int x = position.x / map->getTileSize().width;
int y = (( map->getMapSize().height - 1) *map->getTileSize().height - position.y)
- 1
- 2
- 3
- 4
- 5
前往页