And it makes use of circular reference, so I had a problem with the next part
class HelloWorld …
{
…
CCArray* tileDataArray; //no NSMutableArray on cocos2dx so I made use of CCArray
…
};
>
class Unit …
{
…
HelloWorld* theGame;
…
};
I need to access from the Unit class to the tileDataArray of the HelloWorld class at the moment the ccTouchBegan function is dispatched, and that’s the problem, if I try to read the tileDataArray the application crashes with a *Segmentation fault (core dumped)*
any idea of how to solved it? do I have to change the architecture avoiding this class of circular reference ?
Kinda hard to give a solution with just that amount of code. If I understand correctly, you want to access tileDataArrayfrom the Unit class, is that correct?
What you could do is to:
Declare tileDataArray as public ;
Create a *getTileDataArray()* method on the HelloWorld class ; or
I have attached the involved classes, it’s an ugly code but I made it in 2 nights and I’m facing the language problem, I’m a java developer, but I want to use c/c++ and cocos2dx so it has been hard to deal with it.
The application fails in the function HelloWorld::getTileData(CCPoint) when it is being called by Unit::markPossibleAction(int).
Since you’re adding the Unit objects to the HelloWorldScene object, you can use getParent* to get the parent layer.
For example:
<pre>
bool Unit::ccTouchBegan {
if {
return false;
}
if ) {
return false;
}
state = kStateGrabbed;
HelloWorldScene theGame = ( HelloWorldScene * ) this > getParent;
theGame> unselectUnit();
return true;
}
You can also try having a static **HelloWorldScene** object, but I don’t recommend it.
I think I found the problem. The CCArray::create() method creates an “autorelease” object, due to the circular reference in some moment this object is released, I don’t understand why but that’s the cause of my problem, so I decided to use the alternative form to create those arrays (and in general all the class members that can be set as autorelease)
CCArray * tileDataArray = new CCArray();
tileDataArray~~>init;
…and paying attention to delete/release the object
if
{
tileDataArray~~>release();
tileDataArray = NULL;
}