cocos2dx运行时的错误怎么解决

qq_35064827 2016-06-25 06:36:34
//helloworld.cpp
#include "HelloWorldScene.h"
#include"player.h"
#define PI 3.1415926
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorld::create();
scene->addChild(layer);
return scene;
}

bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}

Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("player.plist", "player.png");

auto sprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("0_run_0.png"));
sprite->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
sprite->setTag(123);
this->addChild(sprite);
moveBy = nullptr;
auto listener = EventListenerTouchOneByOne::create();//创建一个触摸监听(单点触摸)
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);//指定触摸的回调函数
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
m_bIsRunning = false;
return true;
}
bool HelloWorld::onTouchBegan(Touch *pTouch, Event *pEvent)
{
m_pTouchPoint = pTouch->getLocation();
return true;//返回true表示接收触摸事件
}
void HelloWorld::onTouchMoved(Touch* pTouch, Event *event)
{

if (m_bIsRunning == false && sqrt((pTouch->getDelta().x*pTouch->getDelta().x) + (pTouch->getDelta().y*pTouch->getDelta().y)) > 18)
{
auto sp = (Sprite*)this->getChildByTag(123);
m_bIsRunning = true;
//一象限
if (pTouch->getLocation().x > m_pTouchPoint.x&&pTouch->getLocation().y > m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(PI / 8))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleft);
}
else if ((pTouch->getDelta().y) / (pTouch->getDelta().x) > tan(PI / 8) && ((pTouch->getDelta().y) / (pTouch->getDelta().x)< tan(0.75*PI)))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleftup);

}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runup);
}
}
////二像限
if (pTouch->getLocation().x < m_pTouchPoint.x&&pTouch->getLocation().y > m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x)< tan(PI / 8))

{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleft);
}
else if (((pTouch->getDelta().y) / (pTouch->getDelta().x) > tan(PI / 8)) && (abs((pTouch->getDelta().y) / (pTouch->getDelta().x)) < tan(0.75*PI)))

{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleftup);
}
else

{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runup);
}

}
////三像限
if (pTouch->getLocation().x < m_pTouchPoint.x&&pTouch->getLocation().y < m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(PI / 8))
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleft);
}
else if (((pTouch->getDelta().y) / (pTouch->getDelta().x)> tan(PI / 8)) && ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(0.75*PI)))
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->runleftdown);
}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}
sp->runAction(Player::getInstance()->rundown);
} //m_pSp->runAction(g_AnimateM[0]->rundown);
}
////四像限
if (pTouch->getLocation().x > m_pTouchPoint.x&&pTouch->getLocation().y < m_pTouchPoint.y)
{
if ((pTouch->getDelta().y) / (pTouch->getDelta().x) < tan(PI / 8))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}
sp->runAction(Player::getInstance()->runleft);
}
else if (((pTouch->getDelta().y) / (pTouch->getDelta().x) > tan(PI / 8)) && (abs((pTouch->getDelta().y) / (pTouch->getDelta().x)) < tan(0.75*PI)))
{
if (sp->isFlippedX() == false)
{
sp->setFlippedX(true);
}

sp->runAction(Player::getInstance()->runleftdown);
}
else
{
if (sp->isFlippedX() == true)
{
sp->setFlippedX(false);
}

sp->runAction(Player::getInstance()->rundown);

}
}
}
}

void HelloWorld::onTouchEnded(Touch* pTouch, Event *event)
{
m_bIsRunning = false;
auto sp= (Sprite*)this->getChildByTag(123);
sp->stopAllActions();

}
...全文
542 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
暗光之痕 2016-06-26
  • 打赏
  • 举报
回复

是这个错误吗?
重复运行相同动作了,使用 sprite->stopAllActions() 移除原本存在的动作,然后在运行就没有问题了。
qq_35064827 2016-06-25
  • 打赏
  • 举报
回复
程序的功能就是通过touchmove得到行走方向,touchend停止动作。不知道怎么回事,运行时触摸点多移动几次就会报错。 跟宗错误显示断在runaction()里面的addaction里面调用的ccArrayAppendObject(element->actions, action),arr->arr 就是参数element->actions 引发了异常: 写入访问权限冲突。 arr->arr 是 0x1110112。
qq_35064827 2016-06-25
  • 打赏
  • 举报
回复
bool Player::initStand() { char name[256] = { 0 }; //standdown animationstanddown = Animation::create(); for (int i = 0; i < 2; i++) { sprintf(name, "0_stand_%d.png", i); animationstanddown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationstanddown->setDelayPerUnit(0.15); animationstanddown->setRestoreOriginalFrame(true); animationstanddown->setLoops(-1); standdown = Animate::create(animationstanddown); //standleftdown animationstandleftdown = Animation::create(); for (int i = 0; i < 2; i++) { sprintf(name, "1_stand_%d.png", i); animationstandleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationstandleftdown->setDelayPerUnit(0.15); animationstandleftdown->setRestoreOriginalFrame(true); animationstandleftdown->setLoops(-1); standleftdown = Animate::create(animationstandleftdown); //standleft animationstandleft = Animation::create(); for (int i = 0; i < 2; i++) { sprintf(name, "2_stand_%d.png", i); animationstandleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationstandleft->setDelayPerUnit(0.15); animationstandleft->setRestoreOriginalFrame(true); animationstandleft->setLoops(-1); standleft = Animate::create(animationstandleft); //standleftup animationstandleftup = Animation::create(); for (int i = 0; i < 2; i++) { sprintf(name, "3_stand_%d.png", i); animationstandleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationstandleftup->setDelayPerUnit(0.15); animationstandleftup->setRestoreOriginalFrame(true); animationstandleftup->setLoops(-1); standleftup = Animate::create(animationstandleftup); //standup animationstandup = Animation::create(); for (int i = 0; i < 2; i++) { sprintf(name, "4_stand_%d.png", i); animationstandup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationstandup->setDelayPerUnit(0.15); animationstandup->setRestoreOriginalFrame(true); animationstandup->setLoops(-1); standup->create(animationstandup); return true; } bool Player::initAttack() { char name[256] = { 0 }; //attackdown animationattackdown = Animation::create(); animationattackdown->retain(); for (int i = 0; i < 5; i++) { sprintf(name, "0_attack_%d.png", i); animationattackdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationattackdown->setDelayPerUnit(0.15); animationattackdown->setRestoreOriginalFrame(true); animationattackdown->setLoops(-1); attackdown = Animate::create(animationattackdown); //attackleftdown animationattackleftdown = Animation::create(); for (int i = 0; i < 5; i++) { sprintf(name, "1_attack_%d.png", i); animationattackleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationattackleftdown->setDelayPerUnit(0.15); animationattackleftdown->setRestoreOriginalFrame(true); animationattackleftdown->setLoops(-1); attackleftdown = Animate::create(animationattackleftdown); //attackleft animationattackleft = Animation::create(); for (int i = 0; i < 5; i++) { sprintf(name, "2_attack_%d.png", i); animationattackleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationattackleft->setDelayPerUnit(0.15); animationattackleft->setRestoreOriginalFrame(true); animationattackleft->setLoops(-1); attackleft = Animate::create(animationattackleft); //attackleftup animationattackleftup = Animation::create(); for (int i = 0; i < 5; i++) { sprintf(name, "3_attack_%d.png", i); animationattackleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationattackleftup->setDelayPerUnit(0.15); animationattackleftup->setRestoreOriginalFrame(true); animationattackleftup->setLoops(-1); attackleftup = Animate::create(animationattackleftup); //attackup animationattackup = Animation::create(); for (int i = 0; i < 5; i++) { sprintf(name, "4_attack_%d.png", i); animationattackup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationattackup->setDelayPerUnit(0.15); animationattackup->setRestoreOriginalFrame(true); animationattackup->setLoops(-1); attackup = Animate::create(animationattackup); return true; } bool Player::initBeattack() { char name[256] = { 0 }; //beattackdown animationbeattackdown = Animation::create(); sprintf(name, "0_beattack_0.png"); animationbeattackdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationbeattackdown->setDelayPerUnit(0.15); animationbeattackdown->setRestoreOriginalFrame(true); animationbeattackdown->setLoops(-1); beattackdown = Animate::create(animationbeattackdown); //beattackleftdown animationbeattackleftdown = Animation::create(); sprintf(name, "1_beattack_0.png"); animationbeattackleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationbeattackleftdown->setDelayPerUnit(0.15); animationbeattackleftdown->setRestoreOriginalFrame(true); animationbeattackleftdown->setLoops(-1); beattackleftdown = Animate::create(animationbeattackleftdown); //beattackleft animationbeattackleft = Animation::create(); sprintf(name, "2_beattack_0.png"); animationbeattackleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationbeattackleft->setDelayPerUnit(0.15); animationbeattackleft->setRestoreOriginalFrame(true); animationbeattackleft->setLoops(-1); beattackleft = Animate::create(animationbeattackleft); //beattackleftup animationbeattackleftup = Animation::create(); sprintf(name, "3_beattack_0.png"); animationbeattackleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationbeattackleftup->setDelayPerUnit(0.15); animationbeattackleftup->setRestoreOriginalFrame(true); animationbeattackleftup->setLoops(-1); beattackleftup = Animate::create(animationbeattackleftup); //beattackup animationbeattackup = Animation::create(); sprintf(name, "4_beattack_0.png"); animationbeattackup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationbeattackup->setDelayPerUnit(0.15); animationbeattackup->setRestoreOriginalFrame(true); animationbeattackup->setLoops(-1); beattackup = Animate::create(animationbeattackup); return true; } bool Player::initDeath() { char name[256] = { 0 }; //deathdown animationdeathdown = Animation::create(); sprintf(name, "0_death_0.png"); animationdeathdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationdeathdown->setDelayPerUnit(0.15); animationdeathdown->setRestoreOriginalFrame(true); animationdeathdown->setLoops(-1); deathdown = Animate::create(animationdeathdown); //deathleftdown animationdeathleftdown = Animation::create(); sprintf(name, "1_death_0.png"); animationdeathleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationdeathleftdown->setDelayPerUnit(0.15); animationdeathleftdown->setRestoreOriginalFrame(true); animationdeathleftdown->setLoops(-1); deathleftdown = Animate::create(animationdeathleftdown); //deathleft animationdeathleft = Animation::create(); sprintf(name, "2_death_0.png"); animationdeathleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationdeathleft->setDelayPerUnit(0.15); animationdeathleft->setRestoreOriginalFrame(true); animationdeathleft->setLoops(-1); deathleft = Animate::create(animationdeathleft); //deathleftup animationdeathleftup = Animation::create(); sprintf(name, "3_death_0.png"); animationdeathleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationdeathleftup->setDelayPerUnit(0.15); animationdeathleftup->setRestoreOriginalFrame(true); animationdeathleftup->setLoops(-1); deathleftup = Animate::create(animationdeathleftup); //deathup animationdeathup = Animation::create(); sprintf(name, "4_death_0.png"); animationdeathup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); animationdeathup->setDelayPerUnit(0.15); animationdeathup->setRestoreOriginalFrame(true); animationdeathup->setLoops(-1); deathup = Animate::create(animationdeathup); return true; }
qq_35064827 2016-06-25
  • 打赏
  • 举报
回复
//player.cpp #include "player.h" static Player *player = nullptr; Player * Player::getInstance() { if (player ==nullptr) { player = new Player(); player->init(); } return player; } Player::Player() { standdown = nullptr; standleftdown = nullptr; standleft=nullptr; standleftup = nullptr; standup = nullptr; //run rundown = nullptr; runleftdown = nullptr; runleft = nullptr; runleftup = nullptr; runup = nullptr; //attack attackdown = nullptr; attackleftdown = nullptr; attackleft = nullptr; attackleftup = nullptr; attackup = nullptr; //beattack beattackdown = nullptr; beattackleftdown = nullptr; beattackleft = nullptr; beattackleftup = nullptr; beattackup = nullptr; //death deathdown = nullptr; deathleftdown = nullptr; deathleft = nullptr; deathleftup = nullptr; deathup = nullptr; //animation //stand animationstanddown = nullptr; animationstandleftdown = nullptr; animationstandleft = nullptr; animationstandleftup = nullptr; animationstandup = nullptr; //run animationrundown = nullptr; animationrunleftdown = nullptr; animationrunleft = nullptr; animationrunleftup = nullptr; animationrunup = nullptr; //attack animationattackdown = nullptr; animationattackleftdown = nullptr; animationattackleft = nullptr; animationattackleftup = nullptr; animationattackup = nullptr; //beattack animationbeattackdown = nullptr; animationbeattackleftdown = nullptr; animationbeattackleft = nullptr; animationbeattackleftup = nullptr; animationbeattackup = nullptr; //death animationdeathdown = nullptr; animationdeathleftdown = nullptr; animationdeathleft = nullptr; animationdeathleftup = nullptr; animationdeathup = nullptr; } Player::~Player() { rundown ->release(); runleftdown->release(); runleft->release(); runleftup->release(); runup->release(); animationrundown->release(); animationrunleftdown->release(); animationrunleft->release(); animationrunleftup->release(); animationrunup->release(); player->release(); } bool Player::init() { initRun(); initStand(); initAttack(); initBeattack(); initDeath(); return true; } bool Player::initRun() { char name[256] = { 0 }; //rundown animationrundown = Animation::create(); animationrundown->retain(); for (int i = 0; i < 6; i++) { sprintf(name, "0_run_%d.png", i); animationrundown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationrundown->setDelayPerUnit(0.15); animationrundown->setRestoreOriginalFrame(true); animationrundown->setLoops(-1); rundown=Animate::create(animationrundown); //runleftdown animationrunleftdown=Animation::create(); animationrunleftdown ->retain(); for (int i = 0; i < 6; i++) { sprintf(name, "1_run_%d.png", i); animationrunleftdown->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationrunleftdown->setDelayPerUnit(0.15); animationrunleftdown->setRestoreOriginalFrame(true); animationrunleftdown->setLoops(-1); runleftdown = Animate::create(animationrunleftdown); //runleft animationrunleft = Animation::create(); animationrunleft->retain(); for (int i = 0; i < 6; i++) { sprintf(name, "2_run_%d.png", i); animationrunleft->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationrunleft->setDelayPerUnit(0.15); animationrunleft->setRestoreOriginalFrame(true); animationrunleft->setLoops(-1); runleft = Animate::create(animationrunleft); runleft->retain(); //runleftup animationrunleftup = Animation::create(); animationrunleftup->retain(); for (int i = 0; i < 6; i++) { sprintf(name, "3_run_%d.png", i); animationrunleftup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationrunleftup->setDelayPerUnit(0.15); animationrunleftup->setRestoreOriginalFrame(true); animationrunleftup->setLoops(-1); runleftup = Animate::create(animationrunleftup); runleftup->retain(); //runup animationrunup = Animation::create(); animationrunup->retain(); for (int i = 0; i < 6; i++) { sprintf(name, "4_run_%d.png", i); animationrunup->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(name)); } animationrunup->setDelayPerUnit(0.15); animationrunup->setRestoreOriginalFrame(true); animationrunup->setLoops(-1); runup = Animate::create(animationrunup); runup->retain(); return true; }
3D ToolKit for cocos2dx 是一个简单的cocos2d-x 3D扩展,可以方便地在cocos2dx环境中创建3D图形。 基于cocos2d-x 2.2开发。 已实现功能: 1,向量和矩阵运算。 2,扩展的3D相机。(支持 透视投影 和 正交投影) 3,3D Sprite。(支持空间变换,更换mesh,更换shader) 4,扩展的shader program和uniform回调。 实现原理:http://www.cocoachina.com/bbs/read.php?tid=181942 目前iOS和Win32工程可用。 注意:如果你按下面方法成功编译了Demo工程,但是运行的时候崩溃,可能是由于cocos2d-x 2.2的一个bug造成的,解决办法看这里:https://github.com/wantnon2/3DToolKit-for-cocos2dx/issues/1 编译运行Demo工程: 一,iOS (IDE: Xcode): 1,下载一个cocos2d-x 2.2(如果没有的话)。 2,在cocos2d-x-2.2目录下新建一个文件夹myProject,再在myProject下建一个文件夹3DToolKitDemo。 3,git下来的HelloCpp文件夹放到3DToolKitDemo文件夹中。 4,编译运行HelloCpp中的ios工程。 二,Win32 (IDE: vs2010): 1,下载一个cocos2d-x 2.2(如果没有的话)。 2,在cocos2d-x-2.2目录下新建一个文件夹myProject,再在myProject下建一个文件夹3DToolKitDemo。 3,git下来的HelloCpp文件夹放到3DToolKitDemo文件夹中。 4,打开cocos2d-x-2.2下的cocos2d-win32.vc2010.sln。 5,将demo的win32工程添加到解决方案中,并设为启动项。 6,编译运行。 注意:如果遇到"无法打开xxx.lib"错误,可以在解决方案管理器中选择TestCpp工程,将其设为启动项并编译。编译成功后重新将demo工程设为启动项再运行。 在自己的cocos2dx项目中使用ToolKit: 一,iOS (IDE: Xcode): 1,将c3dToolKit文件夹添加到项目中。 2,将c3dDefaultResource文件夹拷贝到Resources目录下,并将c3dDefaultResource以“蓝色文件夹”的形式添加到项目中。 3,在需要使用的地方包含头文件"c3dToolKit.h"。 二,win32 (IDE: vs2010): 1,在工程的 属性->C ->附加包含目录中 中配置c3dToolKit文件夹路径。 2,将c3dDefaultResource文件夹拷贝到Resources目录下。 3,在需要使用的地方包含头文件"c3dToolKit.h"。 代码:https://github.com/wantnon2/3DToolKit-for-cocos2dx

721

社区成员

发帖
与我相关
我的任务
社区描述
Cocos2d-x相关内容讨论专区
社区管理员
  • Cocos2d-x
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧