cocos2dx的问题, 改编helloworld例子时候发生的, 困扰一天了.....

__veritas__ 2014-01-08 10:51:28
直接上代码吧.

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

#include "SimpleAudioEngine.h"

USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer
{

public:
//CCAnimation** walkAnimations; //只要加上这个声明就崩溃啊, 我也崩溃了

CCAnimation* createAnimationByDirection(int dir);

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};

#endif //__HELLOWORLD_SCENE_H__




#include "HelloWorldScene.h"

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
{
//////////////////////////////////////////////////////////////////////////
// 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::create(
"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::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);

// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);

// map
CCTMXTiledMap* map = CCTMXTiledMap::create("0.tmx");
addChild(map);

// hero texture
CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("hero.png");
heroTexture->retain();
CCSpriteFrame *frame0, *frame1, *frame2, *frame3;
frame0 = CCSpriteFrame::createWithTexture(heroTexture, CCRectMake(32*0, 32*0, 32, 32));
frame1 = CCSpriteFrame::createWithTexture(heroTexture, CCRectMake(32*1, 32*0, 32, 32));
frame2 = CCSpriteFrame::createWithTexture(heroTexture, CCRectMake(32*2, 32*0, 32, 32));
frame3 = CCSpriteFrame::createWithTexture(heroTexture, CCRectMake(32*3, 32*0, 32, 32));

CCArray *animFrames = CCArray::createWithCapacity(4);
animFrames->addObject(frame0);
animFrames->addObject(frame1);
animFrames->addObject(frame2);
animFrames->addObject(frame3);

CCAnimation *animation = new CCAnimation();
animation->initWithSpriteFrames(animFrames, 0.2f);
animation->retain();
animFrames->release();

CCSprite *heroSprite = CCSprite::createWithSpriteFrame(
(CCSpriteFrame*)animation->getFrames()->objectAtIndex(0));

heroSprite->setPosition(ccp(100, 100));
addChild(heroSprite);

CCAnimate *animate = CCAnimate::create(animation);

heroSprite->runAction(CCRepeatForever::create(animate));



bRet = true;
} while (0);

return bRet;
}

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}


CCAnimation* HelloWorld:: createAnimationByDirection(int dir) {
CCAnimation *pRet;

return pRet;
}


...全文
252 点赞 收藏 4
写回复
4 条回复
wang1990yujin 2014年01月15日
楼主弄好了?
回复 点赞
无名剑 2014年01月09日
我这边 加不加都是崩溃 bool HelloWorld::init()中的 CCSprite *heroSprite = CCSprite::createWithSpriteFrame( (CCSpriteFrame*)animation->getFrames()->objectAtIndex(0)); 存在内存越界 我把它 改成 CCSprite *heroSprite = CCSprite::createWithSpriteFrame(frame0); 然后就顺利看到你那DEMO的界面了
回复 点赞
__veritas__ 2014年01月08日
http://pan.baidu.com/s/1kTMAgPh 工程放这了, 谁帮看看吧, 给我讲讲怎么回事, pm一个地址, 我给你U点我们山里的土特产 跪谢
回复 点赞
发动态
发帖子
游戏开发
创建于2007-08-27

7288

社区成员

2.3w+

社区内容

游戏开发相关内容讨论专区
社区公告
暂无公告