IOS-使用Cocos2d在精灵上进行碰撞检测

IOS-使用Cocos2d在精灵上进行碰撞检测,ios,objective-c,cocos2d-iphone,Ios,Objective C,Cocos2d Iphone,在这个游戏中,我有小行星从屏幕顶部向下飞行,一艘飞船可以自由移动,但我添加了一些碰撞检测,当我调用[self scheduleupdate]时,它在更新方法中似乎不起作用 1似乎不起作用不是问题描述始终提供详细的问题描述,包括任何错误消息、调用堆栈等2只是粘贴整个代码并不会邀请用户帮助3使用基本调试技术断点、NSLog、。。。要检查代码是否运行4它是Cocos2d,而不是Cocoa2d 5您的更新方法拼写错误:“updtate”,正如learncos2D所说,拼写错误的“updtate”。因此,

在这个游戏中,我有小行星从屏幕顶部向下飞行,一艘飞船可以自由移动,但我添加了一些碰撞检测,当我调用[self scheduleupdate]时,它在更新方法中似乎不起作用


1似乎不起作用不是问题描述始终提供详细的问题描述,包括任何错误消息、调用堆栈等2只是粘贴整个代码并不会邀请用户帮助3使用基本调试技术断点、NSLog、。。。要检查代码是否运行4它是Cocos2d,而不是Cocoa2d 5您的更新方法拼写错误:“updtate”,正如learncos2D所说,拼写错误的“updtate”。因此,即使在计划更新之后,您的更新功能也不会被调用。嗨,谢谢您的回复,是的,slog正在打印hit这个词谢谢
// Import the interfaces
#import "HelloWorldLayer.h"

// Needed to obtain the Navigation Controller
#import "AppDelegate.h"

#pragma mark - HelloWorldLayer

// HelloWorldLayer implementation
@implementation HelloWorldLayer

// Helper class method that creates a Scene with the HelloWorldLayer as the only child.
+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
    if( (self=[super init]) ) {
        self.isTouchEnabled = YES;

        moveLeft = NO;
        moveRight = NO;

        speed = 3;
        fireSpeed = 8;

        fireArray = [[NSMutableArray alloc] init];

        CGSize winSize = [[CCDirector sharedDirector] winSize];
        CCSprite *bg = [[CCSprite alloc] initWithFile:@"space_bg.jpg"];
        [bg setPosition:ccp(160, 240)];
        CGSize imageSize = bg.contentSize;
        bg.scaleX = winSize.width / imageSize.width;
        bg.scaleY = winSize.height / imageSize.height;
        bg.position = ccp(winSize.width/2, winSize.height/2);
        [self addChild:bg];

        ship = [[CCSprite alloc] initWithFile:@"ship.png"];
        [ship setPosition:ccp(100, 100)];
        [self addChild:ship];
        [self schedule:@selector(fireLoop:)];

        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1
                                                          target:self
                                                        selector:@selector(fireCreate)
                                                        userInfo:nil
                                                         repeats:YES];

        asteroidArray = [[NSMutableArray alloc] init];
        for(int i = 0; i < 100; ++i) {
            CCSprite *asteroid = [[CCSprite alloc] initWithFile:@"asteroid1.png"];
            [asteroidArray addObject:asteroid];
        }

        [self asteroidCreate];
        [self scheduleUpdate];
    }
    return self;
}

CCSpriteBatchNode *spriteSheet;
NSMutableArray *asteroidAnimFrames;

- (float)randomValueBetween:(float)low andValue:(float)high {
    return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
}


-(void)updtate:(ccTime)dt{
    CGPoint shipPosition = [ship position];
    for(int i = 0; i < asteroidArray.count; i++){

        CCSprite *asteroid = [asteroidArray objectAtIndex:i];
        if (CGRectIntersectsRect(ship.boundingBox, asteroid.boundingBox)) {
            NSLog(@"hit");
        }
    }
}

- (void)countDownToCreateNextAsteroid {
    int minTime = 1;
    int maxTime = 10;

    int randomTime = (arc4random() % (3));

    id countdownDelay = [CCDelayTime actionWithDuration:randomTime];
    id creationMethod = [CCCallFunc actionWithTarget:self selector:@selector(asteroidCreate)];

    id countdownToCreateSeq = [CCSequence actions:countdownDelay, creationMethod, nil];

    [self stopAllActions];
    [self runAction:countdownToCreateSeq];
}

-(void)asteroidCreate {

    CGSize winSize = [[CCDirector sharedDirector] winSize];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"asteroids.plist"];
    spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"asteroids.png"];

    asteroidAnimFrames = [NSMutableArray array];

    for(int i=1; i <= 8; i++) {
        [asteroidAnimFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"asteroid%d.png", i]]];
    }
    CCAnimation *moveAsteroidAnim = [CCAnimation animationWithFrames:asteroidAnimFrames delay:0.1f];

    CCSprite *asteroid = [CCSprite spriteWithSpriteFrameName:@"asteroid1.png"];
    int x = arc4random() % 320;
    int y = arc4random() % 480;
    asteroid.position = ccp(x, 480);
    CCAction *asteroidAction = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:moveAsteroidAnim restoreOriginalFrame:NO]];

    [asteroidArray addObject:asteroid];

    int q = arc4random() % 320;
    int r = arc4random() % 10;
    CCAction *moveAction = [CCMoveTo actionWithDuration:r position:ccp(q, -50)];

    id asteroidRemove = [CCCallBlock actionWithBlock:^
                         {
                             [asteroid removeFromParentAndCleanup:YES];
                             [asteroidArray removeObject:asteroid];
                         }];

    id asteroidSeq = [CCSequence actions:moveAction, asteroidRemove, nil];
    [asteroid runAction:asteroidSeq];
    [asteroid runAction:asteroidAction];
    [spriteSheet addChild:asteroid];
    [self addChild:spriteSheet];

    [self countDownToCreateNextAsteroid];
}

-(void)fireLoop:(ccTime)fl {

    if(fireArray.count > 0){

        for(int i = 0; i < fireArray.count; i++){
            CCSprite *tmpFire = [fireArray objectAtIndex:i];
            if(tmpFire.position.y < 500){
                [tmpFire setPosition:ccp([tmpFire position].x, [tmpFire position].y + fireSpeed)];
            }else{
                [fireArray removeObjectAtIndex:i];
            }
        }
    } else {

    }
}

-(void)fireCreate{

    int shootPositionX = [ship position].x;
    int shootPositionY = ([ship position].y) + 35;

    CCSprite *fire;
    fire = [[CCSprite alloc] initWithFile:@"fire.png"];
    [fire setPosition:ccp(shootPositionX, shootPositionY)];
    [fireArray addObject:fire];
    [self addChild:fire];

    [fire release];
}

-(void)gameLoop:(ccTime)dt {

    int shipPositionX = 41/2;

    if([ship position].x > shipPositionX){
        [ship setPosition:ccp([ship position].x - speed, [ship position].y)];
    }
}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {
    for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];
        //if(point.x <= 160){
        //    moveRight = NO;
        //    moveLeft = YES;
        //}else{
        //    moveRight =YES;
        //    moveLeft = NO;
        //}

        if(allowedToMove)
            [ship setPosition:ccp(point.x, point.y + 76)];
    }
}

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];

        int shipX = [ship position].x;
        int shipY = [ship position].y;

        if (CGRectContainsPoint(CGRectMake (shipX - 20.5, shipY - 96, 50, 50), point))
        {
            allowedToMove = true;
        }
    }
}




-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    for(UITouch *t in touches){
        CGPoint point = [self convertTouchToNodeSpace:t];
        allowedToMove = false;
    }
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
    // in case you have something to dealloc, do it in this method
    // in this particular example nothing needs to be released.
    // cocos2d will automatically release all the children (Label)

    // don't forget to call "super dealloc"
    [super dealloc];
}

#pragma mark GameKit delegate

-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController
{
    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
    [[app navController] dismissModalViewControllerAnimated:YES];
}

-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{
    AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
    [[app navController] dismissModalViewControllerAnimated:YES];
}
@end