所以,我创建了一个State
类。在State
类的create函数中,它通过调用StateVisual
类的create函数创建一个StateVisual
类,并将自身作为参数传递。 (参数是参考,因此没有复制)。 StateVisual
然后设置参数(State
实例),因为它是父变量,它是状态类型的指针。
在StateVisual
的创建功能中,一切正常。但是,当您使用它的更新方法并尝试打印它的父级大小时,它会打印出一些奇怪的值。
#ifndef STATE_H
#define STATE_H
#include "cocos2d.h"
class StateVisual;
class State {
public:
State();
~State();
static State create();
StateVisual *visual;
float size;
void setSize(float);
void update(float);
private:
cocos2d::Scheduler* _scheduler;
};
#endif
StateVisual.cpp
#ifndef STATE_VIS_H
#define STATE_VIS_H
#include "cocos2d.h"
#include "State.h"
class StateVisual : public cocos2d::Sprite {
public:
StateVisual();
~StateVisual();
// create a visual
State* parent;
static StateVisual* create(State& parent);
cocos2d::Label* label;
void setSize(float);
void update(float);
private:
bool _activated;
float _size;
};
#endif
State.cpp
#include "State.h"
#include "GameScene.h"
State::State() : size(0) {
CCLOG("Created");
}
State::~State() {
}
void State::setSize(float newSize) {
size = newSize;
CCLOG("%f, %f", newSize, size);
}
void State::update(float dt) {
}
State State::create() {
State state;
state.visual = StateVisual::create(state);
cocos2d::SEL_SCHEDULE ss;
return state;
}
StateVisual.cpp
#include "StateVisual.h"
#include "GameScene.h"
using namespace cocos2d;
StateVisual::StateVisual() : parent(nullptr) {
CCLOG("New STATE VISUAL!");
}
StateVisual::~StateVisual() {
}
void StateVisual::setSize(float size) {
setContentSize(Size(size, size));
if (size > 30) {
label->setSystemFontSize(size*.1);
label->setOpacity(255);
}
else {
label->setOpacity(0);
}
}
void StateVisual::update(float dt) {
cocos2d::MathUtil::smooth(&_size, parent->size, dt, .2);
setSize(_size);
CCLOG("%f, %f", _size, (*this->parent).size);
}
StateVisual* StateVisual::create(State &parent) {
StateVisual* visual(new StateVisual());
if (visual->initWithFile("Circle.png"))
{
visual->setSize(200);
visual->_size = 200;
visual->parent = &parent;
visual->parent->setSize(20);
CCLOG("PARENT SIZE: %f", visual->parent->size);
visual->autorelease();
visual->scheduleUpdate();
return visual;
}
CC_SAFE_DELETE(visual);
return NULL;
}
输出:
Created
New STATE VISUAL!
cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible
missing file.
20.000000, 20.000000
PARENT SIZE: 20.000000
500.000000, 500.000000
cocos2d: QuadCommand: resizing index size from [-1] to [2560]
168.058044, -107374176.000000 <-- Those are the weird values it prints
155.130508, -107374176.000000
The program '[1464] State.exe' has exited with code 0 (0x0).
答案 0 :(得分:5)
0xCCCCCCCC
,用于填充Debug版本中未初始化内存的典型值,解释为32位浮点数,等于-107374176.000000
。您正在打印未初始化的float
值。
答案 1 :(得分:0)
State State::create() {
State state;
state.visual = StateVisual::create(state);
cocos2d::SEL_SCHEDULE ss;
return state;
}
在此函数结束时,本地对象状态将被销毁(或移动到新对象)。这意味着在State::create
visual->parent = &parent; // parent = state object on stack
因为这条线visual->parent
现在悬空了。
那些奇怪的值是你线程堆栈的随机内容...
你应该做的是使用state \ parent的共享指针。
答案 2 :(得分:0)
void StateVisual::update(float dt) {
cocos2d::MathUtil::smooth(&_size, parent->size, dt, .2);
setSize(_size);
CCLOG("%f, %f", _size, (*this->parent).size);
parent->size
而在第三行使用(*this->parent).size
? StateVisual* visual(new StateVisual());
如果visual
是自动变量,我认为它看起来应该是StateVisual visual(new StateVisual());
,否则不应该是StateVisual* visual = new StateVisual(new StateVisual());
您还确定要将新的StateVisual对象传递给StateVisual的构造函数吗?