C++ cocos2d-x中的EXC_错误访问

C++ cocos2d-x中的EXC_错误访问,c++,xcode,cocos2d-x,C++,Xcode,Cocos2d X,我只是在训练cocos2d-x。 我试图显示精灵动画,但显示EXC_BAD_访问错误 我编写了如下代码 然而,当我将“animation”函数写到“init”函数的内部时,EXC_BAD_访问错误没有显示出来 怎么了 游戏场景 #include "cocos2d.h" #include "SimpleAudioEngine.h" class GameScene : public cocos2d::CCLayer { public: // Here's a difference

我只是在训练cocos2d-x。 我试图显示精灵动画,但显示EXC_BAD_访问错误

我编写了如下代码

然而,当我将“animation”函数写到“init”函数的内部时,EXC_BAD_访问错误没有显示出来

怎么了

游戏场景

 #include "cocos2d.h"
 #include "SimpleAudioEngine.h"


 class GameScene : public cocos2d::CCLayer
 {
  public:
  // 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 recommend returning the class instance pointer
  static cocos2d::CCScene* scene();


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

  void animation();


  cocos2d::CCSprite* pPlayer;

  //スプライトを格納する領域
  cocos2d::CCSpriteFrame* pSprites[12];

 };
游戏场景

bool GameScene::init()
{
    if(!CCLayer::init())
    {
        return false;
    }



    //端末サイズ取得
CCSize visiblesize = CCDirector::sharedDirector()->getVisibleSize();

//背景画像
CCSprite* bgImg = CCSprite::create("pic1.jpg");

//背景画像のポジション
bgImg->setPosition(ccp(visiblesize.width / 2, visiblesize.height / 2));
//背景画像のポジションを取得
CCPoint pos = bgImg->getPosition();




//===============================================================================
//                              ボタンイベント作成


//ボタンイベント追加
CCMenuItemImage *tapitem = CCMenuItemImage::create("animButton.png", "animButton.png", this, menu_selector(GameScene::animation));

CCMenu* tapMenu = CCMenu::create(tapitem, NULL);

tapMenu->setPosition(ccp(bgImg->convertToNodeSpace(pos).x, bgImg->convertToNodeSpace(pos).y));

//ボタン画像設置
bgImg->addChild(tapMenu);
//背景画像設置
this->addChild(bgImg);


cocos2d::CCSpriteFrame* pSprites[12];

const int WIDTH_SIZE = 96; //1つのスプライトの幅
const int HEIGHT_SIZE = 64; //1つのスプライトの高さ

//●アトラスから矩形切り出し、スプライト領域に格納.アニメーション画像を扱いたい時は、pSpriteから取り出してください。
for(int y = 0; y < 4; y++){
    for(int x = 0; x < 3; x++){
        CCRect rect(x * WIDTH_SIZE, y * HEIGHT_SIZE,WIDTH_SIZE,HEIGHT_SIZE);
        pSprites[y * 3 + x] = CCSpriteFrame::create("texture1.png",rect);
    }
}



// プレイヤースプライトを生成
pPlayer= CCSprite::create("texture1.png", CCRectMake(0, 64, 96, 64));

// プレイヤーのポジション
pPlayer->setPosition(ccp(100,100));

//プレイヤーをシーンに登録して
bgImg->addChild(pPlayer);



return true;

}





void GameScene::animation()
{

//移動先の指定
CCMoveTo* move = CCMoveTo::create(1.8, ccp(200, 100));

//===============================================
//               アニメーションの作成
CCAnimation* animation = CCAnimation::create();


for(int i=3;i<6;i++){
    animation->addSpriteFrame(pSprites[i]);
}

//アニメーションの設定:1コマ0.1秒で切り替える
animation->setDelayPerUnit(0.1);

//アニメーションの設定:6回ループさせる
animation->setLoops(6);


CCRepeatForever *pAction = CCRepeatForever::create( CCAnimate::create(animation) );

// 作成したアニメーションを実行
pPlayer->runAction(pAction);
pPlayer->runAction(move);

}
bool游戏场景::init()
{
如果(!CCLayer::init())
{
返回false;
}
//端末サイズ取得
CCSize visiblesize=CCDirector::sharedDirector()->getVisibleSize();
//背景画像
CCSprite*bgImg=CCSprite::create(“pic1.jpg”);
//背景画像のポジション
bgImg->设置位置(ccp(visiblesize.width/2,visiblesize.height/2));
//背景画像のポジションを取得
CCPoint pos=bgImg->getPosition();
//===============================================================================
//                              ボタンイベント作成
//ボタンイベント追加
CCMenuItemImage*tapitem=CCMenuItemImage::create(“animButton.png”、“animButton.png”,这个菜单_选择器(游戏场景::动画));
CCMenu*tapMenu=CCMenu::create(tapitem,NULL);
点击菜单->设置位置(ccp(bgImg->转换节点空间(pos.x,bgImg->转换节点空间(pos.y));
//ボタン画像設置
bgImg->addChild(点击菜单);
//背景画像設置
此->添加子对象(bgImg);
cocos2d::CCSpriteFrame*pSprites[12];
const int WIDTH_SIZE=96//1つのスプライトの幅
const int HEIGHT_SIZE=64//1つのスプライトの高さ
//●アトラスから矩形切り出し、スプライト領域に格納.アニメーション画像を扱いたい時は、pSpriteから取り出してください。
对于(int y=0;y<4;y++){
对于(int x=0;x<3;x++){
CCRect rect(x*宽度大小,y*高度大小,宽度大小,高度大小);
pSprites[y*3+x]=CCSpriteFrame::create(“texture1.png”,rect);
}
}
// プレイヤースプライトを生成
pPlayer=CCSprite::create(“texture1.png”,CCRectMake(0,64,96,64));
// プレイヤーのポジション
pPlayer->设置位置(ccp(100100));
//プレイヤーをシーンに登録して
bgImg->addChild(pPlayer);
返回true;
}
void GameScene::animation()
{
//移動先の指定
CCMoveTo*move=CCMoveTo::create(1.8,ccp(200100));
//===============================================
//               アニメーションの作成
CCAnimation*animation=CCAnimation::create();
对于(inti=3;iaddSpriteFrame(pSprites[i]);
}
//アニメーションの設定:1.コマ0.1秒で切り替える
动画->setDelayPerUnit(0.1);
//アニメーションの設定:6回ループさせる
动画->设置循环(6);
CCRepeatForever*pAction=CCRepeatForever::create(CCAnimate::create(动画));
// 作成したアニメーションを実行
pPlayer->runAction(打包);
pPlayer->runAction(移动);
}

好吧,它不允许我留下评论,因为我没有50个代表…这个错误到底是在哪里出现的?我是说那行

是这个密码吗

for(int i=3;i<6;i++){
    animation->addSpriteFrame(pSprites[i]);
}
for(inti=3;iaddSpriteFrame(pSprites[i]);
}
如果是,那么请确保“pSprites[i]”尚未发布……如果是这种情况,请使用“new”创建它,然后自己进行清理

之所以会发生这种情况,是因为在“init”函数中没有人引用这些精灵……当流从“init”函数中出来时,这些值会被释放(因为它们是使用CREATE创建的,默认情况下设置了autorelease)

同样,这是我的假设,看看代码。。。
一定要给出你的反馈!

这里有一个可以帮助你在EXC_BAD_ACCESS中进行调试的程序。希望这对你有用,如果还有其他事情请告诉我。谢谢你的回答。是的!它的代码段。我已经修复了如下。for(int y=0;y<4;y++){for(int x=0;x<3;x++){pSprites[y*3+x]>release()}然后错误停止显示。但是,我修复的方法是长描述。例如,是否有如下形式?pSprites=CCSpriteFrame::release();但是,当它是这种形式时,我不断得到这样的错误:“在没有对象参数的情况下调用非静态成员函数。这是因为您可以对CCObject和since调用此方法”pSprites“是一个数组,您必须对每个值调用Release。此外,Release是一个成员函数而不是静态函数。它应该这样调用,>>>>>pSprites[i]->Release();//在循环中执行它>>>>>>>>>>>另外,请注意,在“autorelease”对象(使用“create”创建的对象)上调用“Release”时要小心“默认情况下,函数是自动释放的,因此您不必担心它,除非您想显式删除它。因为一旦删除所有引用,此对象将被删除)请标记此答案,如果它对您有帮助!干杯!非常感谢您的回答!