Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void) drawChar:(ccTime) dt
- {
- //Se mi trovo in pausa esco dal metodo
- if(self.paused)
- return;
- //Altrimenti controllo cosa e dove stampare
- //Get della parola attuale da stampare
- NSString *parola = [self.messToArray objectAtIndex:0];
- //Se precedentemente ho inserito uno spazio
- if(self.isLastSpace){
- //rimuovo la parola precedente allo spazio e carico la nuova parola
- self.isLastSpace = false;
- [self.messToArray removeObjectAtIndex:0];
- //Se il count delle parole da scrivere è 0 elimino lo scheduler ed esco dal metodo
- if([self.messToArray count] == 0){
- [[CCScheduler sharedScheduler] unscheduleSelector:@selector(drawChar:) forTarget:self];
- self.isFinished = true;
- return;
- }
- self.currWord = 0;
- parola = [self.messToArray objectAtIndex:0];
- }else if(self.currWord >= [parola length]){
- //se la lunghezza del contatore alla parola da stampare è maggiore della lunghezza della parola allora devo mettere uno spazio
- self.currWord = 0;
- self.isLastSpace = true;
- parola = [NSString stringWithFormat:@" "];
- }
- //La lattera da stampare
- NSString *lettera = [NSString stringWithFormat:@"%C", [parola characterAtIndex:self.currWord]];
- //La label che contiene la parola completa per controllare se entra tutta nello schermo del telefono
- CCLabelTTF *labelToCheck = [CCLabelTTF labelWithString:[parola substringFromIndex:self.currWord]
- fontName:[GeneralManager sharedManager].fontName fontSize:16];
- //La label che contiene la lettera da scrivere a video
- CCLabelTTF *label = [CCLabelTTF labelWithString:lettera
- fontName:[GeneralManager sharedManager].fontName fontSize:16];
- float labelWidth = [labelToCheck contentSize].width;
- //se la width attualmente scritta a schermo più quella della parola da scrivere sorpassa la finestra allora vado a capo
- if(self.lineWidth + labelWidth > [[CCDirector sharedDirector] winSize].width - 20*2){
- //Nuova linea
- //Se vado a capo e la linea nuova da aggiungere sorpassa la finestra del telefono allo metto in pausa e aspetto il touch dell'utente per continuare .. aggiorno anche la height attualmente scritta
- if(self.lineHeight + [labelToCheck contentSize].height > [[CCDirector sharedDirector] winSize].height){
- self.paused = true;
- return;
- }
- self.lineWidth = 0;
- self.lineHeight += [labelToCheck contentSize].height;
- self.lineX = 14;
- self.lineY -= [label contentSize].height;
- }
- //Setto l'anchor point e la position della label per poi stamparla sul ccLayer e aumentare il contatore alla prossima parola da scrivere
- //e aggionrare anche la width attualmente scritta sul ccLayer
- label.anchorPoint = ccp(0,0);
- label.position = ccp(self.lineX, self.lineY);
- self.lineX += [label contentSize].width;
- [self.actualLayer addChild:label];
- self.currWord++;
- self.lineWidth += [label contentSize].width;
- }
- -(void) drawAllText
- {
- //Array temporaneo
- NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:self.messToArray];
- //Get della parola attuale da stampare
- for (NSString *parola in self.messToArray) {
- if(self.currWord == 0){
- //se la lunghezza del contatore alla parola da stampare è maggiore della lunghezza della parola allora devo mettere uno spazio
- self.currWord = 0;
- CCLabelTTF *spazio = [CCLabelTTF labelWithString:[NSString stringWithFormat:@" "] fontName:[GeneralManager sharedManager].fontName fontSize:16];
- spazio.anchorPoint = ccp(0,0);
- spazio.position = ccp(self.lineX, self.lineY);
- self.lineX += [spazio contentSize].width;
- [self.actualLayer addChild:spazio];
- self.lineWidth += [spazio contentSize].width;
- }
- //La label che contiene la parola completa per controllare se entra tutta nello schermo del telefono
- CCLabelTTF *labelToCheck = [CCLabelTTF labelWithString:[parola substringFromIndex:self.currWord]
- fontName:[GeneralManager sharedManager].fontName fontSize:16];
- float labelWidth = [labelToCheck contentSize].width;
- //se la width attualmente scritta a schermo più quella della parola da scrivere sorpassa la finestra allora vado a capo
- if(self.lineWidth + labelWidth > [[CCDirector sharedDirector] winSize].width - 20*2){
- //Nuova linea
- //Se vado a capo e la linea nuova da aggiungere sorpassa la finestra del telefono allo metto in pausa e aspetto il touch dell'utente per continuare .. aggiorno anche la height attualmente scritta
- if(self.lineHeight + [labelToCheck contentSize].height > [[CCDirector sharedDirector] winSize].height){
- self.paused = true;
- break;
- }
- self.lineWidth = 0;
- self.lineHeight += [labelToCheck contentSize].height;
- self.lineX = 14;
- self.lineY -= [labelToCheck contentSize].height;
- }
- //Setto l'anchor point e la position della label per poi stamparla sul ccLayer e aumentare il contatore alla prossima parola da scrivere
- //e aggionrare anche la width attualmente scritta sul ccLayer
- labelToCheck.anchorPoint = ccp(0,0);
- labelToCheck.position = ccp(self.lineX, self.lineY);
- self.lineX += [labelToCheck contentSize].width;
- [self.actualLayer addChild:labelToCheck];
- self.currWord = 0;
- self.lineWidth += [labelToCheck contentSize].width;
- //NSLog(@"temp: %d, mess: %d, parola: %@", [temp count], [self.messToArray count], parola);
- [temp removeObject:parola];
- }
- //[self.messToArray release];
- //self.messToArray = nil;
- self.messToArray = [[NSMutableArray alloc] initWithArray:temp];
- //NSLog(@"ce ne sono ancora %d", [self.messToArray count]);
- if([self.messToArray count] == 0){
- self.isFinished = true;
- }else
- self.paused = true;
- return;
- }
- -(void) changeVelocityTo:(float)vel
- {
- [[CCScheduler sharedScheduler] unscheduleSelector:@selector(drawChar:) forTarget:self];
- [[CCScheduler sharedScheduler] scheduleSelector:@selector(drawChar:) forTarget:self interval:vel paused:NO];
- }
- - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
- {
- CGPoint touchLocation = [touch locationInView: [touch view]];
- CGPoint location = [[CCDirector sharedDirector] convertToGL:touchLocation];
- /*NSLog(@"Location x: %f, y: %f", location.x, location.y);
- NSLog(@"Rect dimensione x:%f y:%f, w:%f, h:%f", self.dimension.origin.x, self.dimension.origin.y, self.dimension.size.width, self.dimension.size.height);*/
- if(CGRectContainsPoint(self.dimension, location)){
- if(self.isFinished){
- self.isRun = false;
- [self.actualLayer removeAllChildrenWithCleanup:YES];
- [self.boxLayer removeAllChildrenWithCleanup:YES];
- [self.nameLayer removeAllChildrenWithCleanup:YES];
- [self.winLayer removeChild:self.actualLayer cleanup:YES];
- [self.winLayer removeChild:self.boxLayer cleanup:YES];
- [self.winLayer removeChild:self.nameLayer cleanup:YES];
- [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
- [self.winLayer runAction:[[CCCallFuncO alloc] initWithTarget:self.winLayer selector:@selector(goToNextAction:) object:self]];
- return true;
- }else if(self.paused){
- //Elimino quello scritto fino ad ora e setto la pausa a false e le altre variabili al loro stato iniziale
- [self.actualLayer removeAllChildrenWithCleanup:YES];
- [self initVariables];
- [self changeVelocityTo:self.writeVel];
- }else{
- //Scrivo tutto il messaggio in una volta sola ed elimino il selector su drawChar
- [[CCScheduler sharedScheduler] unscheduleSelector:@selector(drawChar:) forTarget:self];
- NSLog(@"Curr Word: %d", self.currWord);
- [self drawAllText];
- }
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement