简体   繁体   English

如何跟踪cocos2d中的精灵被触摸了多少次?

[英]How can I keep track of how many times a sprite has been touched in cocos2d?

In my program, I have sprites fall from the top of the screen and the user taps each sprite to keep them from falling to the ground, which results in game over. 在我的程序中,我让精灵从屏幕顶部掉落,用户点击每个精灵以防止它们掉落到地面,这导致游戏结束。 These sprites are in the shape of stars. 这些精灵是星形的。

My question is, how can I keep track of how many times each star has been tapped, so when a user taps the star 3 times, it disappears? 我的问题是,我该如何跟踪每颗恒星被轻拍了多少次,因此当用户轻按三下恒星时,恒星会消失吗?

I currently have 2 different types of stars fall from the sky, and whenever they are added to the game, they are added into an array. 我目前有2种不同类型的星星从天上掉下来,每当它们添加到游戏中时,它们都会添加到数组中。 Hopefully my code explains what I'm doing. 希望我的代码可以解释我在做什么。

I'm new to objective C, so please feel free to let me know what else is wrong in my code... looking to get better! 我是目标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

UPDATE: I think I'm close..this is what I've got, but I must be missing something because the stars still won't disappear...ALSO - "world" is an undeclared identifier - not sure how to identify 更新:我想我已经接近了,这就是我得到的,但是我必须丢失一些东西,因为星星仍然不会消失...另外-“世界”是一个未声明的标识符-不确定如何识别

@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];

    }
}
}
 - (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");
            }
         }

    }

   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM