在我的程序中,我有精灵从屏幕顶部掉落,用户点击每个精灵以防止它们掉到地上,这导致游戏结束。这些精灵呈星星状。
我的问题是,如何跟踪每颗星被点击的次数,因此当用户点击星星3次时,它会消失?
我目前有两种不同类型的星星从天而降,每当它们被添加到游戏中时,它们都被添加到一个数组中。希望我的代码解释我在做什么。
我是目标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];
}
}
}
答案 0 :(得分:0)
- (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");
}
}
}
}
}