0xC0000005:访问冲突写位置0x00000034
0xC0000005: Access violation writing location 0x00000034
当我的角色(飞扬的小鸟)碰到障碍物时,它会变成游戏结束的场景。问题是它显示了游戏结束的场景,但同时出现了
0x0FBE9B95 (libcocos2d_2015.dll) 中 (於 FlappyBird.exe) 擲回例外狀況: 0xC0000005: 讀取位置 0x00000034 時發生存取違規。
翻译:
0x0FBE9B95 in (libcocos2d_2015.dll) (under FlappyBird.exe) throws an exception 0xC0000005: Access violation writing location 0x00000034.
但是,当我隐藏所有背景内容以及 _scheduleUpdate()_
时,异常消失了! (现场也什么都没有出现......但知道物理仍然存在,我飞扬的小鸟可以掉到地上并转向游戏结束场景)
这是我的程序:
(GameScene.cpp)
#include "GameScene.h"
#include "GameOverScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "Defination.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* GameScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
// 'layer' is an autorelease object
auto layer = GameScene::create();
layer->setPhysicsWorld(scene->getPhysicsWorld());
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool GameScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto origin = Director::getInstance()->getVisibleOrigin();
/*Set screen boundary so that characters will not move outside screen*/
auto edgeBody = PhysicsBody::createEdgeBox(visibleSize,
PHYSICSBODY_MATERIAL_DEFAULT, 3);
edgeBody->setCollisionBitmask(OBSTACLE_COLLISION_BITMASK);
edgeBody->setContactTestBitmask(true);
auto edgeNode = Node::create();
edgeNode->setPhysicsBody(edgeBody);
edgeNode->setPosition(Point(visibleSize.width / 2 + origin.x,
visibleSize.height / 2 + origin.y));
this->addChild(edgeNode);
/*BG*/
bgLayer = Layer::create();
bgTotalWidth = 0;
auto sprite = Sprite::create("bg1.png");
float bgScale = visibleSize.height / sprite->getContentSize().height;
float bgWidth = sprite->getContentSize().width * bgScale;
sprite->release();
allSpriteScale = bgScale;
while (bgTotalWidth < visibleSize.width + bgWidth) {
auto bgSprite = Sprite::create("bg1.png");
bgSprite->setAnchorPoint(Vec2(0, 0));
bgSprite->setPosition(Point(origin.x + bgTotalWidth, origin.y));
bgSprite->setScale(bgScale);
auto scrollAction = RepeatForever::create(MoveBy::create(1, Vec2(-
BACKGROUND_MOVE_SPEED, 0)));
bgSprite->runAction(scrollAction);
bgLayer->addChild(bgSprite);
bgTotalWidth += bgWidth;
}
this->addChild(bgLayer, 0);
/*Pipe*/
pipe = Pipe::Pipe(allSpriteScale);
pipeLayer = Layer::create();
this->addChild(pipeLayer, 1);
/*Flappy Bird*/
playerLayer = Layer::create();
this->addChild(playerLayer, 2);
bird = &FlappyBird::FlappyBird(allSpriteScale, playerLayer);
/*Scheduler*/
scheduleUpdate(); //Enable update
schedule(schedule_selector(GameScene::spawnPipe), PIPE_SPAWN_INTERVAL);
auto collisionListener = EventListenerPhysicsContact::create();
collisionListener->onContactBegin =
CC_CALLBACK_1(GameScene::onContactBegin, this);
Director::getInstance()->getEventDispatcher()-
>addEventListenerWithSceneGraphPriority(collisionListener, this);
return true;
}
void GameScene::update(float delta) {
/*BG Update*/
for each (Sprite* sp in bgLayer->getChildren())
{
if (sp->getPositionX() <= -(sp->getContentSize().width * sp-
>getScaleX())) {
sp->setPosition(Point(bgTotalWidth + sp->getPositionX(), 0));
break;
}
}
}
void GameScene::spawnPipe(float delta) {
pipe.Spawn(pipeLayer);
}
bool GameScene::onContactBegin(PhysicsContact &contact)
{
PhysicsBody* a = contact.getShapeA()->getBody();
PhysicsBody* b = contact.getShapeB()->getBody();
if ( (a->getCollisionBitmask() == FLAPPYBIRD_COLLISION_BITMASK && b-
>getCollisionBitmask() == OBSTACLE_COLLISION_BITMASK) ||
(b->getCollisionBitmask() == FLAPPYBIRD_COLLISION_BITMASK && a-
>getCollisionBitmask() == OBSTACLE_COLLISION_BITMASK)
)
{
auto gameoverScene = GameOverScene::createScene();
Director::getInstance()-
>replaceScene(TransitionFade::create(SCENE_TRANSITION_DURATION,
gameoverScene));
}
return true;
}
Visual Studio表示的暗恋线在main.cpp:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
return Application::getInstance()->run(); // Crush here
}
还有一件有趣的事,我几乎无法更改我的代码!在 _init()_
中,如果我只是隐藏定义我的背景精灵动作的 2 行(逻辑上不会影响其他人),我的 GameScene 中就会出现异常!
非常非常奇怪......希望有人能帮助我。 :(
删除sprite->release()
,这一行导致异常。 Sprite class 已经是一个自动释放对象并在生成后注册在自动释放池中。
所以,当调用replaceScene()
时,所有不应该被调用的对象都会被释放。但是,它不能引用自动释放池中的精灵,因为它已经被sprite->release()
行释放并导致错误。
当我的角色(飞扬的小鸟)碰到障碍物时,它会变成游戏结束的场景。问题是它显示了游戏结束的场景,但同时出现了
0x0FBE9B95 (libcocos2d_2015.dll) 中 (於 FlappyBird.exe) 擲回例外狀況: 0xC0000005: 讀取位置 0x00000034 時發生存取違規。
翻译:
0x0FBE9B95 in (libcocos2d_2015.dll) (under FlappyBird.exe) throws an exception 0xC0000005: Access violation writing location 0x00000034.
但是,当我隐藏所有背景内容以及 _scheduleUpdate()_
时,异常消失了! (现场也什么都没有出现......但知道物理仍然存在,我飞扬的小鸟可以掉到地上并转向游戏结束场景)
这是我的程序:
(GameScene.cpp)
#include "GameScene.h"
#include "GameOverScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "Defination.h"
USING_NS_CC;
using namespace cocostudio::timeline;
Scene* GameScene::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::createWithPhysics();
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);
// 'layer' is an autorelease object
auto layer = GameScene::create();
layer->setPhysicsWorld(scene->getPhysicsWorld());
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool GameScene::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto visibleSize = Director::getInstance()->getVisibleSize();
auto origin = Director::getInstance()->getVisibleOrigin();
/*Set screen boundary so that characters will not move outside screen*/
auto edgeBody = PhysicsBody::createEdgeBox(visibleSize,
PHYSICSBODY_MATERIAL_DEFAULT, 3);
edgeBody->setCollisionBitmask(OBSTACLE_COLLISION_BITMASK);
edgeBody->setContactTestBitmask(true);
auto edgeNode = Node::create();
edgeNode->setPhysicsBody(edgeBody);
edgeNode->setPosition(Point(visibleSize.width / 2 + origin.x,
visibleSize.height / 2 + origin.y));
this->addChild(edgeNode);
/*BG*/
bgLayer = Layer::create();
bgTotalWidth = 0;
auto sprite = Sprite::create("bg1.png");
float bgScale = visibleSize.height / sprite->getContentSize().height;
float bgWidth = sprite->getContentSize().width * bgScale;
sprite->release();
allSpriteScale = bgScale;
while (bgTotalWidth < visibleSize.width + bgWidth) {
auto bgSprite = Sprite::create("bg1.png");
bgSprite->setAnchorPoint(Vec2(0, 0));
bgSprite->setPosition(Point(origin.x + bgTotalWidth, origin.y));
bgSprite->setScale(bgScale);
auto scrollAction = RepeatForever::create(MoveBy::create(1, Vec2(-
BACKGROUND_MOVE_SPEED, 0)));
bgSprite->runAction(scrollAction);
bgLayer->addChild(bgSprite);
bgTotalWidth += bgWidth;
}
this->addChild(bgLayer, 0);
/*Pipe*/
pipe = Pipe::Pipe(allSpriteScale);
pipeLayer = Layer::create();
this->addChild(pipeLayer, 1);
/*Flappy Bird*/
playerLayer = Layer::create();
this->addChild(playerLayer, 2);
bird = &FlappyBird::FlappyBird(allSpriteScale, playerLayer);
/*Scheduler*/
scheduleUpdate(); //Enable update
schedule(schedule_selector(GameScene::spawnPipe), PIPE_SPAWN_INTERVAL);
auto collisionListener = EventListenerPhysicsContact::create();
collisionListener->onContactBegin =
CC_CALLBACK_1(GameScene::onContactBegin, this);
Director::getInstance()->getEventDispatcher()-
>addEventListenerWithSceneGraphPriority(collisionListener, this);
return true;
}
void GameScene::update(float delta) {
/*BG Update*/
for each (Sprite* sp in bgLayer->getChildren())
{
if (sp->getPositionX() <= -(sp->getContentSize().width * sp-
>getScaleX())) {
sp->setPosition(Point(bgTotalWidth + sp->getPositionX(), 0));
break;
}
}
}
void GameScene::spawnPipe(float delta) {
pipe.Spawn(pipeLayer);
}
bool GameScene::onContactBegin(PhysicsContact &contact)
{
PhysicsBody* a = contact.getShapeA()->getBody();
PhysicsBody* b = contact.getShapeB()->getBody();
if ( (a->getCollisionBitmask() == FLAPPYBIRD_COLLISION_BITMASK && b-
>getCollisionBitmask() == OBSTACLE_COLLISION_BITMASK) ||
(b->getCollisionBitmask() == FLAPPYBIRD_COLLISION_BITMASK && a-
>getCollisionBitmask() == OBSTACLE_COLLISION_BITMASK)
)
{
auto gameoverScene = GameOverScene::createScene();
Director::getInstance()-
>replaceScene(TransitionFade::create(SCENE_TRANSITION_DURATION,
gameoverScene));
}
return true;
}
Visual Studio表示的暗恋线在main.cpp:
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
return Application::getInstance()->run(); // Crush here
}
还有一件有趣的事,我几乎无法更改我的代码!在 _init()_
中,如果我只是隐藏定义我的背景精灵动作的 2 行(逻辑上不会影响其他人),我的 GameScene 中就会出现异常!
非常非常奇怪......希望有人能帮助我。 :(
删除sprite->release()
,这一行导致异常。 Sprite class 已经是一个自动释放对象并在生成后注册在自动释放池中。
所以,当调用replaceScene()
时,所有不应该被调用的对象都会被释放。但是,它不能引用自动释放池中的精灵,因为它已经被sprite->release()
行释放并导致错误。