Cocos2d-x中的错误访问异常

时间:2013-09-02 00:09:23

标签: c++ pointers exc-bad-access cocos2d-x

我对C ++编程和Cocos2d-x有点新,我在第一次使用Cocos2d-x游戏时遇到了一些麻烦。我实际上只是试图模拟引力并根据它更新玩家的位置。我正在运行Xcode 4.6.1并且由于某种原因我在一次运行后不断获得BAD_ACCESS异常(如果我重新启动它似乎工作)。它发生在libobjc.A.dylib`objc_release。我尝试过做一个allexceptions断点,它在dylib的内存地址处断开。这只发生在我将一个速度变量添加到我的Player类之后,所以也许我没有正确分配指针等等?以下是我相信的相关课程。

#include "cocos2d.h"

using namespace cocos2d;
class Player : public Sprite{
private:
    Point velocity;

public:
    static Player* create(const char *filename);
    void update(float delta);
    void setVelocity(const Point &v);
    const Point& getVelocity() const;
    ~Player();


};


#include "Player.h"
USING_NS_CC;
using namespace cocos2d;
Player::~Player(){

}

Player* Player::create(const char *filename){
    Player* self = (Player *) Sprite::create(filename);
    self->setVelocity(Point::ZERO);
    return self;
}

void Player::update(float delta){
    log("In player update");
    Point gravity = Point(0.0, -450.0);
    Point gravityStep = gravity * delta;
    this->setVelocity(this->getVelocity() + gravityStep);
    log("Velocity after setting: %f", getVelocity().y);
    Point stepVelocity = this->getVelocity() * delta;
    this->setPosition(this->getPosition() + stepVelocity);
    log("Position after setting: %f, %f", getPosition().x, getPosition().y);
}

void Player::setVelocity(const Point &v){
    this->velocity = v;
}

const Point& Player::getVelocity() const
{
    return this->velocity;
}

#include "GameLayer.h"

USING_NS_CC;

Scene* GameLayer::scene()
{
    // 'scene' is an autorelease object
    Scene *scene = Scene::create();

    // 'layer' is an autorelease object
    GameLayer *layer = GameLayer::create();

    // add layer as a child to scene
    scene->addChild(layer);

    // return the scene
    return scene;
}

GameLayer::~GameLayer(){
    CC_SAFE_RELEASE(player);
    CC_SAFE_RELEASE(map);
}

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

    LayerColor *blueSky = LayerColor::create(*new Color4B(100, 100, 250, 255));
    this->addChild(blueSky);

    map = TMXTiledMap::create("level1.tmx");
    this->addChild(map);

    player = Player::create("koalio_stand.png");
    player->setPosition(Point(100,50));
    map->addChild(player, 15);

    setKeyboardEnabled(true);
    this->scheduleUpdate();

    return true;
}

void GameLayer::update(float dt){
    player->update(dt);
}
编辑:所以我发现播放器的构造函数是问题所在。但是,我想知道执行以下操作是否犹豫不决(因为Player继承自Sprite)来复制Sprite函数:

Sprite* Sprite::create(const char *filename)
{
    Sprite *sprite = new Sprite();
    if (sprite && sprite->initWithFile(filename))
    {
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return NULL;
}

并且播放器功能为:

Player* Player::create(const char *filename){
    Player *sprite = new Player();
    if (sprite && sprite->initWithFile(filename))
    {
        sprite->setVelocity(Point::ZERO);
        sprite->autorelease();
        return sprite;
    }
    CC_SAFE_DELETE(sprite);
    return NULL;
}

或者是否有更简洁的方法来调用Sprite的创建,但是要创建一个继承并设置速度的Player对象?

0 个答案:

没有答案