Objective c CgRectSpoint问题
在我的测试游戏中,我有一些精灵(泡泡=NSMutableArray),它们出现在屏幕底部的随机位置 我有addBubble和spawBubble方法:Objective c CgRectSpoint问题,objective-c,cocos2d-iphone,touch,Objective C,Cocos2d Iphone,Touch,在我的测试游戏中,我有一些精灵(泡泡=NSMutableArray),它们出现在屏幕底部的随机位置 我有addBubble和spawBubble方法: - (void) addBubble { CGSize winSize = [[CCDirector sharedDirector] winSize]; bubbles = [[NSMutableArray alloc] init]; [[CCSpriteFrameCache sharedSpriteFrameCache] addSprit
- (void) addBubble {
CGSize winSize = [[CCDirector sharedDirector] winSize];
bubbles = [[NSMutableArray alloc] init];
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bubbleSpriteList.plist"];
CCSpriteBatchNode *bubbleSpriteList = [CCSpriteBatchNode batchNodeWithFile:@"bubbleSpriteList.png"];
[self addChild:bubbleSpriteList];
bigBubble = [CCSprite spriteWithSpriteFrameName:@"bubble"];
[self addChild:bigBubble];
[bubbles addObject:bigBubble];
for (CCSprite *bubble in bubbles) {
int minX = bubble.contentSize.width/2;
int maxX = winSize.width-bubble.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
bubble.position = ccp(actualX, 0);
int minSpeed = 15.0;
int maxSpeed = 20.0;
int rangeSpeed = maxSpeed - minSpeed;
int actualSpeed = (arc4random() % rangeSpeed) + minSpeed;
ccBezierConfig bubblePath;
bubblePath.controlPoint_1 = ccp(200, winSize.height/3);
bubblePath.controlPoint_2 = ccp(-200, winSize.height/1.5);
bubblePath.endPosition = ccp(0, winSize.height+bubble.contentSize.height/2);
id bezierMove = [CCBezierBy actionWithDuration:actualSpeed bezier:bubblePath];
[bubble runAction:bezierMove];
}}
-(void)spawBubble:(ccTime)dt {
[self addBubble];}
然后在我的init方法中,我添加了背景和带有随机时间间隔的spawBubble方法
[self schedule:@selector(spawBubble:) interval:actualTime];
我试图让每个泡泡从泡泡吹出,当它被触摸时,用这个代码
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInView:[touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL:touchLocation];
touchLocation = [self convertToNodeSpace:touchLocation];
for (CCSprite *bubble in bubbles) {
CGRect bubbleRect = CGRectMake(bubble.position.x - (bubble.contentSize.width/2),
bubble.position.y - (bubble.contentSize.height/2),
bubble.contentSize.width,
bubble.contentSize.height);
if (CGRectContainsPoint(bubbleRect, touchLocation)) {
NSLog(@"%i", [bubbles count]);
[bubble setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"bubbleBlow"]];
id disappear = [CCFadeTo actionWithDuration:0.1 opacity:0];
[bubble runAction:disappear];
}
}
return TRUE;}
如果屏幕上只有一个气泡,那么每个气泡都会完美地吹出,但是如果屏幕上出现一个气泡和另一个气泡,那么只有最后一个气泡会被吹出
我做错了什么?如果看不到更多的代码,就很难说了-但我从这里开始: 在这条线上:
for(CCSprite*气泡中的气泡){
加:
NSLog(@“%i”,气泡计数];
编辑:
根据您添加的代码:
您的问题在于这一行:
bubbles=[[NSMutableArray alloc]init];
每次添加新的气泡时,它都会有效地清除气泡
因此,阵列中只有一个气泡-最后一个
因此,您遇到了问题。我通过删除气泡=[[NSMutableArray alloc]init];
并将其添加到init方法,更改了addBubble方法。。。
现在一切都正常了!)所以每次添加气泡时,你都在清除气泡数组并创建一个新的…这看起来像是你的问题。bubbles=[[NSMutableArray alloc]init];
不应该在那里-of应该被封装在if(!bubbles)中
或类似的东西。@OnOff-您现在在哪里初始化气泡数组?还是现在总是为零?CGRectContainsPoint中存在问题…我刚刚删除了气泡数组。现在它只是一个大气泡ccSprite@OnOff,没有-你说只有一个气泡在工作-你的气泡阵列有一个明显的问题-请告诉我你在哪里正在初始化数组。能否添加一个断点来验证气泡现在是否为零?如果您删除了数组,现在如何循环它们?我在目标c中的知识不足以回答您的问题)