游戏允许用户控制飞船并击落小行星,当小行星被激光击中时,我显示爆炸,但我需要爆炸在1秒后消失。所以我在Cocos2d中使用了CCCallBlock,但Blocks似乎没有在内部启动方法:
for(int i = 0; i < fireArray.count; i++){
CCSprite *fire = [fireArray objectAtIndex:i];
for(int j = 0; j < asteroidArray.count; j++){
CCSprite *asteroid = [asteroidArray objectAtIndex:j];
if (CGRectIntersectsRect(fire.boundingBox, asteroid.boundingBox)) {
CCSprite *explode = [[CCSprite alloc] initWithFile:@"explode.png"];
[explode setPosition:ccp([asteroid position].x, [asteroid position].y)];
CGSize imageSize = explode.contentSize;
[self addChild:explode];
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:1],
[CCCallBlock actionWithBlock:^
{
[self removeExpode:explode];
}], Nil]];
//[self stopAllActions];
[explodeDeleteArray addObject:explode];
[asteroidDeleteArray addObject:asteroid];
}
}
}
-(void)removeExpode:(CCSprite *)explodeObj{
[explodeObj removeFromParentAndCleanup:YES];
}
更新
我已经创建了Explode自定义Sprite,但init似乎永远不会启动,因为NSLog不会打印出“新爆炸”,所以这意味着永远不会调用时间来使爆炸消失:
//
// Explode.m
// Asteroids
//
// Created by trikam patel on 02/08/2014.
// Copyright 2014 trikam patel. All rights reserved.
//
#import "Explode.h"
@implementation Explode
// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init]) ) {
NSLog(@"new explode");
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(removeExplode)
userInfo:nil
repeats:YES];
}
return self;
}
-(void)removeExplode{
NSLog(@"removeExplode");
[self removeFromParentAndCleanup:YES];
}
@end
if (CGRectIntersectsRect(fire.boundingBox, asteroid.boundingBox)) {
Explode *explode = [[Explode alloc] initWithFile:@"explode.png"];
[explode setPosition:ccp([asteroid position].x, [asteroid position].y)];
[self addChild:explode];
[asteroidDeleteArray addObject:asteroid];
}
更新
代码现在似乎有效:
//
// Explode.m
// Asteroids
//
// Created by trikam patel on 02/08/2014.
// Copyright 2014 trikam patel. All rights reserved.
//
#import "Explode.h"
@implementation Explode
// on "init" you need to initialize your instance
-(id) init
{
if( (self=[super init]) ) {
[super initWithFile:@"explode.png"];
[self schedule:@selector(removeExplodeShedule:) interval:1];
}
return self;
}
-(void)removeExplodeShedule:(ccTime)res{
[self removeFromParentAndCleanup:YES];
[self unschedule:@selector(removeExplodeShedule:)];
}
@end