質問編集履歴

1

コードの掲載

2018/10/11 09:24

投稿

yaoriku_ava
yaoriku_ava

スコア25

test CHANGED
File without changes
test CHANGED
@@ -9,3 +9,153 @@
9
9
  派生元のcovos2d::Nodeやcocos2d::Spriteのメンバ変数・関数を参照しようとすると、「Cannot initialize object parameter of type 'cocos2d::Node'('cocos2d::Sprite') with an expression of type 'CharacterChip'」というエラーが出てしまい、開発が進みません。
10
10
 
11
11
  原因としてはどんなものが考えられるのでしょうか…
12
+
13
+ ヘッダーと実装はこちらになります。
14
+
15
+
16
+
17
+ ```c++
18
+
19
+ class CharacterChip : public cocos2d::Sprite{
20
+
21
+ private:
22
+
23
+ //現在向いている方向
24
+
25
+ Direction _currentDir = (Direction)-1;
26
+
27
+ //各方向のアニメーションを記憶しておく入れ物
28
+
29
+ cocos2d::Map<Direction, cocos2d::Action*> animationTable;
30
+
31
+
32
+
33
+ public:
34
+
35
+ //リソースを解放してオブジェクトを破棄
36
+
37
+ virtual ~CharacterChip();
38
+
39
+ virtual bool init(const std::string& name);
40
+
41
+ static CharacterChip* create(const std::string& name);
42
+
43
+ void changeDirection(Direction dir);
44
+
45
+ };
46
+
47
+ ```
48
+
49
+ ```c++
50
+
51
+ CharacterChip::~CharacterChip(){
52
+
53
+ this->removeFromParentAndCleanup(true); //ここにCannot initialize object parameter of type 'cocos2d::Node' with an expression of type 'CharacterChip'というエラー
54
+
55
+ this->animationTable.clear();
56
+
57
+ }
58
+
59
+
60
+
61
+ bool CharacterChip::init(const std::string& name){
62
+
63
+ if(!Sprite::init()) //ここにCannot initialize object parameter of type 'cocos2d::Sprite' with an expression of type 'CharacterChip'というエラー
64
+
65
+ return false;
66
+
67
+
68
+
69
+ auto baseSprite = Sprite::create(name);
70
+
71
+ auto textureSource = baseSprite->getTexture();
72
+
73
+ textureSource->setAliasTexParameters();
74
+
75
+
76
+
77
+ int frameWidth = baseSprite->getContentSize().width/4;
78
+
79
+ int frameHeight = baseSprite->getContentSize().height;
80
+
81
+ this->setContentSize(Size(frameWidth, frameHeight));//ここにCannot initialize object parameter of type 'cocos2d::Sprite' with an expression of type 'CharacterChip'というエラー
82
+
83
+ int harfWidth = baseSprite->getContentSize().width/2;
84
+
85
+
86
+
87
+ for(int y = 0; y < 2; y++){
88
+
89
+ auto animation = Animation::create();
90
+
91
+ animation->setDelayPerUnit(0.3f);
92
+
93
+ animation->setRestoreOriginalFrame(true);
94
+
95
+ for(int x = 0; x < 2; x++){
96
+
97
+ int _x = frameWidth * x + (y >= 1 ? harfWidth : 0);
98
+
99
+ int _y = 0;
100
+
101
+ animation->addSpriteFrameWithTexture(textureSource, Rect(_x, _y, frameWidth, frameHeight));
102
+
103
+ }
104
+
105
+ this->animationTable.insert((Direction)y, RepeatForever::create(Animate::create(animation)));
106
+
107
+ }
108
+
109
+ return true;
110
+
111
+ }
112
+
113
+
114
+
115
+ CharacterChip* CharacterChip::create(const std::string& name){
116
+
117
+ CharacterChip* pRet = new (std::nothrow) CharacterChip();
118
+
119
+ if(pRet && pRet->init(name)){
120
+
121
+ pRet->autorelease(); //ここにCannot initialize object parameter of type 'cocos2d::Ref' with an expression of type 'CharacterChip'というエラー
122
+
123
+ return pRet;
124
+
125
+ }else{
126
+
127
+ delete pRet;
128
+
129
+ pRet = nullptr;
130
+
131
+ return nullptr;
132
+
133
+ }
134
+
135
+ }
136
+
137
+
138
+
139
+ void CharacterChip::changeDirection(Direction dir){
140
+
141
+ if(this->_currentDir == dir)
142
+
143
+ return;
144
+
145
+ if(this->animationTable.find(dir) == this->animationTable.end())
146
+
147
+ return;
148
+
149
+ this->runAction(this->animationTable.at(dir));
150
+
151
+ }
152
+
153
+ ```
154
+
155
+
156
+
157
+ エラーが全て似たような感じなので、原因がわかれば一気に解決できそうなのですが...
158
+
159
+ 実体がないというのも、create関数中にエラーが出ていることが関係しているのではと思います。
160
+
161
+ CharacterChipクラスは、Spriteクラス、Nodeクラス、Refクラスを全て継承しているはずなのに、なぜその派生元のクラスのメンバ変数やメンバ関数をいじれないんでしょう泣