0
我有2個按鈕(左和右)和一個包含所有55個圖像的精靈表。我想知道,使用按鈕瀏覽每個精靈的最佳方式是什麼?使用按鈕更改精靈 - Cocos2d
E.G. 按下左鍵並添加第一個精靈。按下右鍵,第一個精靈將被刪除,第二個精靈將被添加。等等,直到它到達最後的圖像。
這是精靈表
#import "cocos2d.h"
@interface GameScene : CCLayer {
CCSpriteBatchNode *pspriteSheet
}
+(CCScene *) scene;
@property (nonatomic, retain) CCSprite *p;
@end
----------------------------------------------------
#import "GameScene.h"
@implementation GameScene
@synthesize p = _p;
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
GameScene *layer = [GameScene node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if ((self = [super init]))
{
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:
@"PAnim.plist"];
pspriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"PAnim.pvr.ccz"];
[self addChild:pspriteSheet z:0];
}
return self;
}
- (void) dealloc
{
CCLOG(@"%@: %@", NSStringFromSelector(_cmd), self);
_p = nil;
[CCSpriteFrameCache purgeSharedSpriteFrameCache];
[CCTextureCache purgeSharedTextureCache];
[super dealloc];
}
我應該不斷加入他們,消除他們?
E.G.
-(void)buttons:(CGPoint)touchLocation
{
if (CGRectContainsPoint(leftB.boundingBox, touchLocation) && tapP == YES && paused == NO) {
if (count == 1)
{
_p = [CCSprite spriteWithSpriteFrameName:@"p1.png"];
[pspriteSheet addChild:_p];
count = 2;
_p.position = ccp(240, 215);
}
if (CGRectContainsPoint(rightB.boundingBox, touchLocation) && tapP == YES && paused == NO) {
if (count == 2)
{
[pspriteSheet removeChild:_p cleanup:YES];
_p = [CCSprite spriteWithSpriteFrameName:@"p2.png"];
_p.position = ccp(240, 215);
[pspriteSheet addChild:_p];
count = 3;
}
}
這裏就是 「按鈕」 方法被調用
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];
[self buttons:touchLocation];
return TRUE;
}
我有多個精靈表使用相同的方法(我不知道這是否使differen CE)。我可以使用它,因爲我得到「EXC_BAD_ACCESS」錯誤或精靈甚至沒有出現。有任何想法嗎? – Jonathan 2011-05-04 05:45:58
我在「ccTouchBegan」 – Jonathan 2011-05-04 05:49:43
中調用了我的「 - (void)按鈕:(CGPoint)touchlocation」,我不太確定,因爲我只改變了同一個batchnode中的顯示幀。 – xuanweng 2011-05-07 15:26:39