English 中文(简体)
Coco2d游戏中的协作探测?
原标题:Collision Detection in Cocos2d game?

i 试图以下述方式检测到Collision 两个sprites......但当我试图驾车时,没有发生碰撞。

- (void)update:(ccTime)dt {



    CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                       projectile.position.y - (projectile.contentSize.height/2), 
                                       projectile.contentSize.width, 
                                       projectile.contentSize.height);

    //CGRectMake(0,220,320,50);
    CGRect targetRects =  CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                                 _monkey.position.y - (_monkey.contentSize.height/2), 
                                 _monkey.contentSize.width, 
                                 _monkey.contentSize.height);

        if (CGRectIntersectsRect(projectileRect, targetRects)) {
                    NSLog(@"ha ha Collision detected"); 
        iii                       

iii

<>strong>projectilespirite is animating from summary to the past and key,spirite is animating from留 to right at the Worst the projectile go through the monkey but the log do not have been calls?

- (void)update:(ccTime)dt {

CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
iii

CGRect projectileRects = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), 
                                   projectile.position.y - (projectile.contentSize.height/2), 
                                   projectile.contentSize.width, 
                                  projectile.contentSize.height);
CGRect targetRect = CGRectMake(_monkey.position.x - (_monkey.contentSize.width/2), 
                               _monkey.position.y - (_monkey.contentSize.height/2), 
                               _monkey.contentSize.width, 
                              _monkey.contentSize.height);
if (CGRectIntersectsRect(projectileRects, targetRect)) {
    NSLog(@"@@@@@@@@@@@@@@@@@@@@@@@@@@@@");             
iii

iii

- 废除 MoveFinished (id)sender{

//NSLog(@"spriteMoveFinished");
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];

if (sprite.tag == 1) { 
    [_targets removeObject:sprite];



iii else if (sprite.tag == 2) { 
    [_projectiles removeObject:sprite];
iii

iii

- (避免) 具体目标1

projectile = [CCSprite spriteWithFile:@"egg.png" rect:CGRectMake(0, 0, 10, 10)];
projectile.position = ccp(_bear.position.x,_bear.position.y-20);
projectile.tag=2;
[self addChild:projectile];

CGPoint realDest = ccp(_bear.position.x, _bear.position.y - 380);

int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;

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

// Add to projectiles array
projectile.tag = 2;
[_projectiles addObject:projectile];

iii

-(void) registerWithTouchDispatcher
{
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
iii

- (BOOL) 活动:活动*

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];


if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
{
    if (![_walkMonkey isDone]) {
        [_monkey runAction:_walkMonkey];
    iii


iii
else {

iii

return YES;

iii

- 加入: 活动:活动*

CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

if(CGRectContainsPoint(CGRectMake(0,0,320,50),touchLocation)) 
{

    [_monkey stopAction:_walkMonkey];

iii

iii

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {       

    CGPoint touchLocation = [self convertTouchToNodeSpace:touch];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];

    CGPoint translation = ccpSub(touchLocation, oldTouchLocation);    

    if (translation.x > 3) {

        _monkey.flipX=YES;
    iii
    else if (translation.x < -3){
        _monkey.flipX=NO;

    iii

    if(CGRectContainsPoint(CGRectMake(40,0,240,50),touchLocation)) 
    {

        CGPoint newPos = ccpAdd(translation,_monkey.position);
        if(newPos.x >= 320 || newPos.x <= 20)
        {
            NSLog(@"monkey not walking");
        iii
        else {
            newPos.y = 100;
            _monkey.position = newPos;
        iii

    iii

iii
最佳回答

您应利用内在功能:

CGRect projectileRect = [projectile boundingBox];
CGRect targetRects = [_monkey boundingBox];

if (CGRectIntersectsRect(projectileRect, targetRects))
{
    NSLog(@"ha ha Collision detected");
}

捆绑的Box方法更多地考虑到几个方面,例如,如果 no子与母体相比处于地位的话。

还注意到在目标C中,用强调物预先确定变数被认为是不良做法。 Apple果保留可变名称,主要强调其内部图书馆。 如果你真的需要分类变量,一种共同的方法就是用强调加以完善。

问题回答

暂无回答




相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签