1

我正在使用 cocos2d 开发我的第一个游戏,但我遇到了一个我似乎无法找到正确解决方案的问题。我每秒从屏幕顶部到底部添加一个 CCSprite 并为其设置动画,当玩家触摸其中任何一个时,我需要隐藏这些精灵。所以我想给我添加的每个精灵都加上标签,然后用那个特定的标签访问那个精灵。我是否需要将标签号放入某个数组中,因为它们每秒都会增加,甚至在我以触摸方法访问它们之前?

- (void)addStraightBugs 
{
currentAntTag++;

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"smallAunt.plist"];        

spriteSheetmedAnt = [CCSpriteBatchNode batchNodeWithFile:@"smallAunt.png"];
[self addChild:spriteSheetmedAnt z:0 tag:kSpriteManager];

CCSprite *ant= [CCSprite spriteWithSpriteFrameName:@"small-aunt1.png"];
[spriteSheetmedAnt addChild:ant z:1 tag:currentAntTag];

NSMutableArray *walkAnimFrames = [NSMutableArray array];
for(int i = 1; i <= 2; ++i) {
    [walkAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"small-ant%d.png", i]]];
}

CCAnimation *walkAnim = [CCAnimation animationWithFrames:walkAnimFrames delay:0.15f];
CCAction *action=[CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:walkAnim restoreOriginalFrame:NO]];


ant.position = ccp(100,500);
[ant runAction:action];

CGPoint realDest = ccp(60,140);

int minDuration = 2.0;
int maxDuration = 5.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

[ant runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:actualDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(moveFinished:)],
                       nil]];

}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event  
{
CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

CCSpriteBatchNode *spriteManager;
spriteManager = (CCSpriteBatchNode*)[self getChildByTag:kSpriteManager];
CCSprite *ant = (CCSprite*)[spriteManager getChildByTag:currentAntTag];

CGRect abc= CGRectInset([ant boundingBox],30, 85);

if(CGRectContainsPoint(abc,touchLocation))  
{

    ant.visible=NO;
}
}

我也有 3 个方法,每隔几秒调用一次,我创建这些 CCSpriteFrameCache 和 CCSpriteBatchNode 对象以使我的角色在动画时运行。像这样每秒创建缓存会不会太重,或者我应该在 init 方法中创建它们,然后在这里对 CCSprite 运行操作?

4

1 回答 1

2

子类 CCSprite。然后在它的初始化:

[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];

实现 ccTouch 方法:

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event;
- (void) ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;

现在你有两个选择。你可以有一个委托变量来回调或使用 NSNotifications。在这里使用回调,它更快。

// in your @interface
id touchDelegate; // between the {}'s

@property (nonatomic, assign) id touchDelegate;

在你的游戏类中,当你创建你的坏人时:

NewCCSprite = newSprite = [NewCCSprite init];
newSprite.touchDelegate = self;

当你触摸一个时:

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
    if (self.touchDelegate == nil) return;
    [self.touchDelegate performSelector:@selector(touched:) withDelay:0.0f];
}

最后,你的 touch: 方法:

- (void) touch:(id)sender {
    NewCCSprite* sprite = (NewCCSprite*)sender;
    // hide/kill off/etc
}
于 2011-08-17T20:52:13.733 回答