【问题标题】:Implementing Touches on Multiple CCSprites in Cocos2d the right way??在 Cocos2d 中实现多个 CCSprites 的正确方式??
【发布时间】:2011-10-15 18:06:19
【问题描述】:

我正在使用 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 运行操作??

【问题讨论】:

    标签: iphone cocos2d-iphone ccsprite


    【解决方案1】:

    子类 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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      相关资源
      最近更新 更多