如何在球桶中投球?

时间:2011-12-29 07:23:10

标签: iphone objective-c cocos2d-iphone

我是Cocos2d的新手,因此不了解大多数类和函数。在我的应用程序中,我想要实现一个模块,如触摸并沿着枪管的方向拖动球将球传递并将球放入枪管中。我能够在方向上传球但是它会跟随坐标(x.y)离开屏幕。

    - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
    {
        // Choose one of the touches to work with
        UITouch *touch = [touches anyObject];
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];

        //  Determine offset of location to projectile
        int offX = location.x - ball.position.x;
        int offY = location.y - ball.position.y;

        // Bail out if we are shooting down or backwards
        if (offX <= 0)
            return;

        // Determine where we wish to shoot the projectile to
        int realX = winSize.width + (ball.contentSize.height/2);
        float ratio = (float) offY / (float) offX;
        int realY = (realX * ratio) + ball.position.y;
        CGPoint realDest = ccp(realX, realY);

        if(realX>=320)
            realX = 320;
        if(realY>=480)
            realY = 480;
        CGPoint realDest = ccp(realX, realY);

        int good = goodBarrel.position.x;
        int bad = badBarrel.position.x;

        int destY = realDest.x;
        if(destY<=good)
            destY = good;

        if(destY>=bad)
            destY = bad;

        realDest.x = destY+10;

        // Determine the length of how far we're shooting
        int offRealX = realX - ball.position.x;
        int offRealY = realY - ball.position.y;
        float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
        float velocity = 480/1; // 480pixels/1sec
        float realMoveDuration = length/velocity;

        // Move projectile to actual endpoint
        [ball runAction:[CCSequence actions:
                               [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
                               [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                               nil]];                         
}

我需要做类似的事情才能了解枪管的位置并找到枪管的顶部,并将球放在枪管的中心,同时在移动球时,球的大小会缩小到将球扔到遥远的地方。 GoodBarrel和BadBarrel是我放在球的对面的两个球,以便将球放在同一个球中。我可以使用任何功能或方法吗?

2 个答案:

答案 0 :(得分:0)

您可以使用碰撞检测,或者换句话说检查球和桶的CGRect是否相交

- (void)collisionDetection:(ccTime)dt{
CGRect ballRect = CGRectMake(
                                   ball.position.x, 
                                   ball.position.y, 
                                   10, 
                                   10);


    CGRect barrelRect = CGRectMake(
                                   barrel.position.x, 
                                   barrel.position.y, 
                                   50, 
                                   10);

    if (CGRectIntersectsRect(ballRect, barrelRect)) 
 {
        // Run Animation of Ball Dropping in the Barrel

        [self removeChild:ball cleanup: YES];


}
}

希望这就是你所需要的。

答案 1 :(得分:0)

[ball runAction:....]之后立即;你可以同时在同一个对象上运行另一个动作。添加

[ball runAction:[CCScaleTo actionWithDuration:realMoveDuration scale:0.4f];

这两项行动将同时完成,提供您想要创造的距离“幻觉”。