hi
when you add a sprite to an array, what info is recorded? i know you can get its position but what else is in their? also how can i add my own custom things to be recorded like say the sprite state?
thanks
Custom array?
(16 posts) (5 voices)-
Posted 2 years ago #
-
hi, everything property that the sprite has is stored.
If you want to add your own properties you have to extend the CCSprite class.for example:
interface MySprite : CCSprite { int state; }When you instantiate a MySprite object you will be able to access the CCSprite properties plus you own.
Posted 2 years ago # -
hi, thanks for your reply, when i add a sprite then do i have to do anything differently to make it use MySprite insted of CCSprite?
thanksPosted 2 years ago # -
To create a custom sprite class with additional methods, instances variables, ect... You need to subclass CCSprite. When subclassing your new class will support all the features of a CCSprite plus any additional ones you care to add.
@interface MySprite : CCSprite //Subclass of CCSprite { int MyNewInstanceVariable; } -(void)newMethod; @end @implementation MySprite ....Code... @endThen to create a custom sprite...
MySprite *sprite = [MySprite spriteWithFile:@"image.png"]; //Or any other methods used to create a spriteI believe there is documentation on the website in regards to subclassing and I also believe there may be examples included in the download.
Posted 2 years ago # -
thanks so much for your reply, will give it a try in the morning
Posted 2 years ago # -
alright, i tried to subclass ccsprite but as a first attempt at subclassing it didn't go so well.i did it all in a seperate file to my main game, imported cocos2d, imported my new sprite class into the main game, that all builds fine but when i try and add my state variable this happens:
//Sprite.h <p>@interface Sprite : CCSprite { int state; } @end //Sprite.m #import "Sprite.h" @implementation Sprite -(id) init if( (self=[super init]) ) return self; @end //Helloworld.m //then when i call it i do this Sprite *gem1 = [Sprite spriteWithFile:@"GEM.png"]; //set the position gem1.position = ccp(100,100); //set the state gem1.state = 1; error object cannot be set, either read only or no setter found.what am i doing wrong? i looked at the documentation and found no help their, does anyone know where i can read up on how to subclass cocos2d classes?
thanksPosted 2 years ago # -
If you want to be able to access the state instance variable with the dot notation like your using you need to make it a property else you would need to make custom getter and setter methods.
//This would go in your interface file before the @end. @property(readwrite) int state;//This would go in the implementation file. @synthesize state;The way shown above will automatically create the getter and setter methods for you. If you want to or need to write custom methods to access a instance variable then you would do something like so...
-(void)setState:(int)theState;//Sets the value of state to theState -(int)getState;//Returns the value of stateTo best understand Objective-C you should look into online tutorials or purchasing a book.
More information on properties http://developer.apple.com/mac/library/documentation/cocoa/conceptual/ObjectiveC/Articles/ocProperties.html.
Posted 2 years ago # -
alright, one last question and i will leave you all alone, how do i actually use the i-var, i have tried gems->state = 1 which returns an error "instance variable "state" is declated protected". but it is declared as readwrite. whats going on?
Posted 2 years ago # -
try gems.state = 1;
Posted 2 years ago # -
just fyi,
gems->state=1;tries to access the ivar directly whilegems.state=1;is shorthand for[gems setState:1];also, while this:
-(int)getState;//Returns the value of statewill work fine, the ObjC naming standard for getters is just:
-(int)state;//Returns the value of statePosted 2 years ago # -
i originally had it as gems.state=1 and was getting and error:
if([test count] >= maxTokens){ for(int i=0;i<[test count];i++){ CCNode *gems = (Sprite *)[test objectAtIndex:i]; if (gems->testState == 1) { //error: Request for member "testState" is something not a structure or union NSLog(@"hello"); } } }i assume the error is somthing to do with my array but i have no idea how to fix this, is their a good book or sit someone can suggest that covers subclassing in depth? thanks
Posted 2 years ago # -
You are trying to access 'testState', it should just simply be 'state'
if(gems.state == 1)... //or if([gems getState] == 1)...That is if the getter and setter methods have been made manually or you made 'state' a property.
Posted 2 years ago # -
As was mentioned above, if your variable is called 'state', then 1) make a property for it. 2) synthesize it, 3) call is with gems.state.
In your code above you're trying to access a variable 'testState' in the wrong manner. It should be gems.testState (if your variable is testState). You can also access it by [gems testState] as well if you followed points #1 and #2.
You should Google some Objective-C tutorials to understand classes, methods, subclassing, general syntax and other fun stuff.
Posted 2 years ago # -
alright, it all works fine when i call it from within my touch class but when i try and see the state of one of my gems from my array (which is why i started this whole thing) i get an error.
i am adding my gems to the array with:
MySprite *gem = [MySprite spriteWithFile:@"GEM.png"];//add sprite [test addObject:gem];//add sprite to array where test is the array ( i know my naming sucks but im working on it)then when i retrieve the info from my array i can get all the usual stuff like position but if i try and read the state of the object by trying to make a variable = the value i read out of the array i get this error:
testVar=gems.spriteState;error:Request for member "spriteState" is something not a structure or unionif i place that same line in the ccTouchesBegan area it works fine and tells me what the state of the gem is. Why does it work in one place and not the other?
Posted 2 years ago # -
instead of:
CCNode *gems = (Sprite *)[test objectAtIndex:i];
try:
MySprite *gems = (MySprite *)[test objectAtIndex:i];
then:
gems.somePropertyshould work.make damn sure test only has MySprite objects in it ;)
Posted 2 years ago # -
Thank you so much Codemattic, that fixed my problem
Posted 2 years ago #
Reply
You must log in to post.