【问题标题】:How can I keep track of how many times a sprite has been touched in cocos2d?如何跟踪 cocos2d 中精灵被触摸的次数?
【发布时间】:2014-08-02 09:20:42
【问题描述】:

在我的程序中,我有精灵从屏幕顶部掉下来,用户点击每个精灵以防止它们掉到地上,这会导致游戏结束。这些精灵是星星的形状。

我的问题是,我怎样才能跟踪每个星星被点击了多少次,所以当用户点击星星 3 次时,它就会消失?

我目前有 2 种不同类型的星星从天而降,每当它们被添加到游戏中时,它们就会被添加到一个数组中。希望我的代码能解释我在做什么。

我是 Objective C 的新手,所以请随时告诉我我的代码中还有什么问题...希望变得更好!

#import "MainScene.h"
#import <CoreGraphics/CGGeometry.h>
#include <stdlib.h>
#import "Star.h"
#import "redStar.h"

@implementation MainScene {
    CCSprite *_star;
    CCSprite *_redStar;
    CCPhysicsNode *_physicsNode;
    CCNode *_ground;
    CCNode *_leftWall;
    CCNode *_rightWall;
    CCNode *_ceiling;
    CCLabelTTF *_scoreLabel;
    NSInteger _points;
    BOOL _gameOver;
    CCButton *_restartButton;
    NSInteger _taps;
    NSMutableArray *_allStars;
    NSInteger _rando;
}

- (void)didLoadFromCCB {
    _rando = (arc4random_uniform(1000));
    self.userInteractionEnabled = TRUE;
    self.starList = [NSMutableArray array];
    [self addNewStar];

    // set collision txpe
    _ground.physicsBody.collisionType = @"level";
    // set this class as delegate
    _physicsNode.collisionDelegate = self;
}

-(void)addNewStar {
    float ranNum01 = (arc4random_uniform(200)+60);
    float pos1 = ranNum01;

    _star = (Star *)[CCBReader load:@"Star"];
    _star.physicsBody.collisionGroup = @"starGroup";
    _star.physicsBody.collisionType = @"star";
    _star.position = ccp(pos1,500);

    [_physicsNode addChild:_star];
    [self.starList addObject:_star];
}

-(void)addRedStar {
    float ranNum01 = (arc4random_uniform(200)+60);
    float pos1 = ranNum01;
    _redStar = (redStar *)[CCBReader load:@"redStar"];
    _redStar.physicsBody.collisionGroup = @"starGroup";
    _redStar.physicsBody.collisionType = @"star";
    _redStar.position = ccp(pos1,500);
    [_physicsNode addChild:_redStar];
    [self.starList addObject:_redStar];
}

-(BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair star:(CCNode *)star level:(CCNode *)level {
    [self gameOver];
    return TRUE;
}

- (void)update:(CCTime)delta {
    // clamp velocity.  -1*MAXFLOAT means no falling speed limit.
    float yVelocity = clampf(_star.physicsBody.velocity.y, -1 * MAXFLOAT, 1000.f);
    _star.physicsBody.velocity = ccp(0, yVelocity);
}

- (void)starDropper
{
    if (_taps == 0)
    {
        return;
    }

    if ((_taps + (_rando)) % 5 == 0)
    {
        [self addNewStar];
    }

    if ((_taps + (_rando)) % 8 == 0)
    {
        [self addRedStar];
    }
}

- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    float ranNum1 = (arc4random_uniform(100));
    float ranNum2 = (arc4random_uniform(100));
    float sideForce = ranNum1 - ranNum2;

    self._tappedStar = NO;

    if (!_gameOver) {
        for (_star in self.starList) {

            CGPoint touchLocation = [touch locationInNode:_physicsNode];
            if(CGRectContainsPoint([_star boundingBox], touchLocation)) {

                [_star.physicsBody applyImpulse:ccp(sideForce, 2500.f)];
                [_star.physicsBody applyAngularImpulse:2500.f];
                self._tappedStar = YES;
                _taps++;
                _points++;
                _scoreLabel.string = [NSString stringWithFormat:@"%d", _points];
            }
        }

        if (self._tappedStar == NO)
        {
            return;
        }

        if (self._tappedStar == YES)
        {
            [self starDropper];
        }
    }
}

- (void)restart {
    CCScene *scene = [CCBReader loadAsScene:@"MainScene"];
    [[CCDirector sharedDirector] replaceScene:scene];
}

- (void)gameOver {
    if (!_gameOver) {

        _gameOver = TRUE;
        _restartButton.visible = TRUE;
        _star.rotation = 90.f;
        _star.physicsBody.allowsRotation = FALSE;
        [_star stopAllActions];

        CCActionMoveBy *moveBy = [CCActionMoveBy actionWithDuration:0.2f position:ccp(-2, 2)];
        CCActionInterval *reverseMovement = [moveBy reverse];
        CCActionSequence *shakeSequence = [CCActionSequence actionWithArray:@[moveBy,     reverseMovement]];
        CCActionEaseBounce *bounce = [CCActionEaseBounce actionWithAction:shakeSequence];
        [self runAction:bounce];
    }
}

@end

更新:我想我已经接近了..这就是我所拥有的,但我一定错过了一些东西,因为星星仍然不会消失......另外 - “世界”是一个未声明的标识符- 不知道如何识别

@implementation MainScene {

CCSprite *touchedStar;
CCSprite *_star;
CCSprite *_redStar;

CCSprite *oneTappedStar;
CCSprite *twoTappedStar;
CCSprite *threeTappedStar;

CCPhysicsNode *_physicsNode;

CCLabelTTF *_scoreLabel;
NSInteger _points;

BOOL _gameOver;

CCButton *_restartButton;
NSInteger _rando;

CCActionFadeIn *fadeIn;

}

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

float ranNum1 = (arc4random_uniform(100));
float ranNum2 = (arc4random_uniform(100));
float sideForce = ranNum1 - ranNum2;

self._tappedStar = NO;


if (!_gameOver) {

    for (_star in self.starList) {


    CGPoint touchLocation = [touch locationInNode:_physicsNode];
        if(CGRectContainsPoint([_star boundingBox], touchLocation))

        {
            [_star.physicsBody applyImpulse:ccp(sideForce, 2500.f)];
            [_star.physicsBody applyAngularImpulse:2500.f];
            self._tappedStar = YES;
            _taps++;
            _points++;
            _scoreLabel.string = [NSString stringWithFormat:@"%d", _points];

           // starTaps++;


            if (touchedStar == threeTappedStar) {

                [self removeChild:touchedStar];
                world->DestroyBody(touchedStar.physicsBody);

            }

            //3

            if (touchedStar == twoTappedStar) {

                touchedStar = threeTappedStar;
            }

            //2

            if (touchedStar == oneTappedStar) {

                touchedStar = twoTappedStar;
            }

            //1

            if (touchedStar == _star) {

                touchedStar = oneTappedStar;
            }




        }
    }



    if (self._tappedStar == NO)
    {
        return;

    }


    if (self._tappedStar == YES)
    {
        [self starDropper];

    }
}
}

【问题讨论】:

    标签: ios objective-c xcode cocos2d-iphone


    【解决方案1】:
     - (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
    
    float ranNum1 = (arc4random_uniform(100));
    float ranNum2 = (arc4random_uniform(100));
    float sideForce = ranNum1 - ranNum2;
    CCSprite *starTouched;
    
    if (!_gameOver) {
        for (starTouched in starList) {
    
            CGPoint touchLocation = [touch locationInNode:_physicsNode];
            if(CGRectContainsPoint([starTouched boundingBox], touchLocation)) {
    
                [starTouched.physicsBody applyImpulse:ccp(sideForce, 2500.f)];
                [starTouched.physicsBody applyAngularImpulse:2500.f];
                _taps++;
                _points++;
                _scoreLabel.string = [NSString stringWithFormat:@"%ld", (long)_points];
    
    
                if (starTouched.scale == 1.000002) {
    
                    [_physicsNode removeChild:starTouched];
    
                    CCLOG(@"DESTROY");
                }
    
                if (starTouched.scale == 1.000001) {
    
                    starTouched.scale = 1.000002;
                    CCLOG(@"2");
                }
    
                // FOR DIFFERENT TYPE OF STARS
    
                //star
                if (starTouched == _star) {
    
                    starTouched.scale = 1.000001;
                    CCLOG(@"1");
                }
    
                //redStar
                if (starTouched == _redStar) {
    
                    starTouched.scale = 1.000001;
                    CCLOG(@"1");
                }
             }
    
        }
    
       }
    }
    

    【讨论】:

    • 参考你的第二个代码,你会把这段代码放在哪里?
    • touchBegan 方法,您可以在其中获取触摸的星星。
    • 好的,还有一个问题,我如何/在哪里定义touchedStar和oneTappedStar(等等)?非常感谢你的帮助!我认为这会奏效。
    • 在您的实现中:CCSprite *oneTappedStar (等)。在 touchBegan 方法中,touchStar 是你的 _star。
    • 嘿@flowmachine1,我刚刚更新了我的代码以反映你所做的,但我的星星仍然没有消失。我想我真的很接近,你能看到我做错了什么吗?非常感谢所有这些帮助。
    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多