在 Cocos2d 中以正确的方式实现多个 CCSprite 的触摸?
我正在使用 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 上运行操作?
I am developing my first game with cocos2d and i have run into a problem of which i cant seem to find a right solution. i am adding and animating a CCSprite every second from top of the screen to the bottom and need to hide these sprites when the player touches on any one of them. So i thought of giving tags to every sprite i add and later on access that sprite with that particular tag. Do i need to put the tag number in some array as they get incremented every second even before i access them in the touch method??
- (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;
}
}
also i have 3 methods which are called every few seconds in which i create these CCSpriteFrameCache and CCSpriteBatchNode object to make my character run while it animates. will it be too heavey to crate caches every second like this or should i create them in init method and just run the action on CCSprite here??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CCSprite 的子类。然后在它的 init 中:
实现 ccTouch 方法:
现在您有两个选择。您可以使用委托变量来回调或使用 NSNotifications。在这里使用回调,速度更快。
在你的游戏类中,当你创建坏人时:
当你触摸一个坏人时:
最后,你的 touch: 方法:
Subclass CCSprite. Then in it's init:
Implement the ccTouch methods:
Now you have two options. You can have a delegate variable to callback or use NSNotifications. Go with the callback here, it's faster.
Inside your game class, when you're creating your bad guys:
And when you touch one:
Finally, your touch: method: