ios - Display a CCSprite seven times -
i beginner in cocos2d , wanted display 7 coins in horizontal pattern. wrote in main gameplay layer:
in init, have this
coins = [ccsprite spritewithfile:@"coins.png"]; i made method coin patterns want in (display 7 times across)
- (void)coinpatterns { coins.position = ccp(150,150); for(int = 0; < 7; i++) { coins.position = ccp (coins.position.x + 20, coins.position.y); [self addchild:coins]; } } and added in update method
[self coinpatterns]; but reason, code keeps crashing. know how can fix this?
thanks!
i think want here create separate sprite node each coin, instead of reusing same 1 on , on (i'm not sure if works). this
- (void)coinpatterns { nsinteger originalx = 150; for(int = 0; < 7; i++) { ccsprite *coin = [ccsprite spritewithfile:@"coins.png"]; coin.position = ccp(originalx, 150); originalx += 20; [self addchild:coin]; } } this creates 7 coins, each spaced 20. also, in code provided, 7 coins have been stacked on top of each other, since x value never incremented. if use this, it's unnecessary have variable or property coins.
if want access these coins later, example see if character hit them, make nsmutablearray property coinarray , add each coin array add line [self.coinarray addobject:coin]; for-loop under [self addchild:coin];. put them in array.
to detect collision, along lines of this
- (void)charactermoved:(ccsprite *)character { (ccsprite *coin in self.coinarray) { if (cgrectintersectsrect(coin.frame, character.frame) // character , coin collided, add points or remove coin or } } this need have method charactermoved: fires every time character moves.
Comments
Post a Comment