2013-08-31 227 views
1

我正在嘗試製作一個應用程序,其中的錯誤在屏幕上運行並被擠壓。雖然我有問題。當我觸摸屏幕上的一個錯誤時,我的代碼就會這樣,它就會消失。雖然,這隻適用於右側。這是我的代碼來擺脫這個錯誤。屏幕左側不能正常工作

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch * touch = [touches anyObject]; 
    CGPoint touchLocation = [touch locationInView:[touch view]]; 
    touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation]; 
    touchLocation = [self convertToNodeSpace: touchLocation]; 

    if (CGRectContainsPoint(bug.boundingBox, touchLocation)) { 
     [self removeChild:bug cleanup:YES]; 


    } 

} 

然後,這是我的代碼產卵屏幕

- (void) addBug { 

bug = [CCSprite spriteWithFile:@"bug.jpg"]; 

CGSize size = [[CCDirector sharedDirector] winSize]; 
int minY = bug.contentSize.height /2; 
int maxY = size.height - bug.contentSize.height /2; 
int rangeY = maxY - minY; 
int actualY = (arc4random() % rangeY) + minY; 

bug.position = ccp(size.width + bug.contentSize.width/2, actualY); 
[self addChild:bug]; 

int minSpeed = 2.0; 
int maxSpeed = 4.0; 
int rangeSpeed = maxSpeed - minSpeed; 
int actualDuration = (arc4random() % rangeSpeed) + minSpeed; 


actionMove = [CCMoveTo actionWithDuration:actualDuration 
              position:ccp(-bug.contentSize.width/2, actualY)]; 
actionMoveDone = [CCCallBlockN actionWithBlock:^(CCNode *node) { 
    [node removeFromParentAndCleanup:YES]; 
}]; 
[bug runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

} 

-(void)gameLogic:(ccTime)dt { 
    [self addBug]; 
} 

在init右側的bug,我有

[self setTouchEnabled:YES]; 

    [self schedule:@selector(gameLogic:) interval:1.0]; 

我也有一個背景,如果那很重要。我還測試了日誌,如果在屏幕左側觸摸了錯誤但沒有移除錯誤,點擊模擬器中的錯誤,沒有任何記錄,但是當我在屏幕的右側單擊它們時,它被成功記錄。非常感謝您的幫助。

*編輯時,我調整了gameLogic的時間間隔:當屏幕上只有一個錯誤時,左側工作。我應該如何調整我的代碼以使其工作,以便可以一次在屏幕上出現多個錯誤,我仍然可以將它們全部刪除?我猜測,我正在使用的代碼爲最新的錯誤創建了一個邊界框,並刪除了所有其他邊界框。任何額外的幫助將是偉大的!謝謝!

回答

0

您正在每個gameLogic:調用中添加錯誤,但您只保留指向最後創建的最後一個碰撞檢查的指針,其位置爲ccTouchesBegan:withEvent:。將每個new添加到數組或任何適合您其他需求的數據結構中,並檢查其中的所有對象。