Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
iOS - Cocos2d e azioni degli sprite
Forum - iOS - Cocos2d e azioni degli sprite

Avatar
_mikele_ (Member)
Rookie


Messaggi: 40
Iscritto: 06/12/2010

Segnala al moderatore
Postato alle 19:38
Venerdì, 11/05/2012
Sto procedendo col mio progetto, purtroppo però mi sono bloccato nuovamente perchè durante la gestione del touchscreen, quando viene toccato un orsetto, lo sprite non fa quello che dovrebbe fare.

In pratica quando gli sprite terminano la loro azione la ricominciano da capo con una posizione diversa e un movimento diverso, gli orsetti diventano quindi invisibili, vengono aggiunti alla coda tutti gli orsetti invisibili (la coda mi servirà in un secondo tempo) e viene richiamata la funzione che riposiziona gli orsetti e fa iniziare una nuova azione.

Quando invece vengono toccati vorrei potessero cambiare di posizione o diventare invisibili (HelloWorldLayer.m, ccTouchEnded) in modo da poter essere aggiunti alla coda, se dico di cambiare posizione non la cambiano (forse perchè sono nel bel mezzo di un'azione?) se invece dico di diventare invisibili vengono aggiunti alla coda, viene richiamata la funzione per riposizionare l'orso e far iniziare una nuova azione ma gli orsetti non cambiano di posizione e iniziano la nuova azione dal punto in cui erano posizionati prima, inoltre non terminano mai la loro azione perchè una volta "usciti" dallo schermo non riappaiono più.

Codice sorgente - presumibilmente VB.NET

  1. // HelloWorldLayer.m
  2. #import "HelloWorldLayer.h"
  3. #import "TeddyBear.h"
  4.  
  5. #define kNumTeddyBears 15
  6.  
  7. // HelloWorldLayer implementation
  8. @implementation HelloWorldLayer
  9.  
  10. + (CCScene *) scene
  11. {
  12.         // 'scene' is an autorelease object.
  13.         CCScene *scene = [CCScene node];
  14.        
  15.         // 'layer' is an autorelease object.
  16.         HelloWorldLayer *layer = [HelloWorldLayer node];
  17.        
  18.         // add layer as a child to scene
  19.         [scene addChild: layer];
  20.        
  21.         // return the scene
  22.         return scene;
  23. }
  24.  
  25. // on "init" you need to initialize your instance
  26. - (id) init
  27. {
  28.         if((self = [super init]))
  29.     {
  30.         /* Touch abilitato. */
  31.         self.isTouchEnabled = YES;
  32.        
  33.         /* Abilita il metodo update. */
  34.         [self scheduleUpdate];
  35.        
  36.         /* Inizializzazione della coda di teddyBear. */
  37.         _teddyBearsQueue = [[CCArray alloc] initWithCapacity:kNumTeddyBears];
  38.        
  39.         /* Animazione teddyBear. */
  40.         [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"AnimTeddyBear.plist"];
  41.        
  42.         CCSpriteBatchNode *spriteSheet = [CCSpriteBatchNode batchNodeWithFile:@"AnimTeddyBear.png"];
  43.         [self addChild:spriteSheet];
  44.        
  45.         walkAnimFrames = [[NSMutableArray array] retain];
  46.         for(int i = 1; i <= 4; ++i)
  47.         {
  48.             [walkAnimFrames addObject: [[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"teddybear%d.png", i]]];
  49.         }
  50.        
  51.         /* Array di teddyBear. */
  52.         _teddyBears = [[CCArray alloc] initWithCapacity:kNumTeddyBears];
  53.         for(int i = 0; i < kNumTeddyBears; i++)
  54.         {
  55.             TeddyBear *tmpTeddyBear = [TeddyBear new];
  56.             [tmpTeddyBear initTeddyBear:1 withAnimFrames:walkAnimFrames withSpriteFrameName:@"teddybear1.png" withDelay:0.09f];
  57.             [spriteSheet addChild:[tmpTeddyBear teddyBearSprite]];
  58.             [_teddyBears addObject:tmpTeddyBear];
  59.            
  60.             /* Aggiunge alla coda. */
  61.             NSNumber *touchedTeddyBear = [NSNumber numberWithInt:i];
  62.             [_teddyBearsQueue addObject:touchedTeddyBear];
  63.         }
  64.        
  65.     }
  66.         return self;
  67. }
  68.  
  69. - (float) randomValueBetween:(float) low andValue:(float) high
  70. {
  71.     return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
  72. }
  73.  
  74. - (void) update:(ccTime) dt
  75. {
  76.     int k = 0;
  77.    
  78.     /* Scorre _teddyBears, se un oggetto è invisibile lo mette nella coda. */
  79.     for(TeddyBear *tmpTeddyBear in _teddyBears)
  80.     {
  81.         if([[tmpTeddyBear teddyBearSprite] visible] == NO)
  82.         {
  83.             NSNumber *touchedTeddyBear = [NSNumber numberWithInt:k];
  84.             [_teddyBearsQueue addObject:touchedTeddyBear];
  85.             [tmpTeddyBear teddyBearSprite].visible = YES;
  86.         }
  87.         k++;
  88.     }
  89.    
  90.     if([_teddyBearsQueue count] > 0)
  91.     {
  92.         int nextTeddyBear = [[_teddyBearsQueue objectAtIndex:0] integerValue];
  93.         [_teddyBearsQueue removeObjectAtIndex:0];
  94.         [[_teddyBears objectAtIndex:nextTeddyBear] startAnimation];
  95.     }
  96. }
  97.  
  98. - (void) registerWithTouchDispatcher
  99. {
  100.     [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
  101. }
  102.  
  103. -(BOOL) ccTouchBegan:(UITouch *) touch withEvent:(UIEvent *) event {
  104.         return YES;
  105. }
  106.  
  107. -(void) ccTouchEnded:(UITouch *) touch withEvent:(UIEvent *) event {
  108.     CGPoint location = [touch locationInView:[touch view]];
  109.     location = [[CCDirector sharedDirector] convertToGL:location];
  110.     for(TeddyBear *tmpTeddyBear in _teddyBears)
  111.     {
  112.         if([tmpTeddyBear teddyBearSprite].visible == NO)
  113.             continue;
  114.        
  115.         if(CGRectContainsPoint([[tmpTeddyBear teddyBearSprite] boundingBox], location))
  116.         {
  117.             //[[tmpTeddyBear teddyBearSprite] setPosition:ccp(200, 200)];
  118.             [tmpTeddyBear setInvisible];
  119.             break;
  120.         }
  121.     }  
  122. }
  123.  
  124.  
  125. // TeddyBear.m
  126. #import "TeddyBear.h"
  127.  
  128. @implementation TeddyBear
  129.  
  130. @synthesize teddyBearSprite = _teddyBearSprite;
  131. @synthesize walkAction = _walkAction;
  132. @synthesize moveAction = _moveAction;
  133. @synthesize walkAnim = _walkAnim;
  134.  
  135. - (float)randomValueBetween:(float)low andValue:(float)high {
  136.     return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
  137. }
  138.  
  139. - (void) initTeddyBear:(int) l withAnimFrames:(NSMutableArray*) animFrames withSpriteFrameName:(NSString*) spriteFrameName withDelay:(float) d
  140. {
  141.     /*CGSize winSize = [CCDirector sharedDirector].winSize;
  142.     int posX, posY;
  143.     float randDuration;*/
  144.     //NSLog(@"winSize.height = %f\n", winSize.height);
  145.     int posX, posY;
  146.    
  147.     life = l;
  148.    
  149.     _teddyBearSprite = [CCSprite spriteWithSpriteFrameName:spriteFrameName];
  150.    
  151.     posX = -_teddyBearSprite.contentSize.width;
  152.     posY = -_teddyBearSprite.contentSize.height;
  153.     _teddyBearSprite.position = ccp(posX, posY);
  154.    
  155.     //[self startAnimation];
  156.    
  157.     _walkAnim = [CCAnimation animationWithFrames:animFrames delay:d];
  158.     _walkAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:_walkAnim restoreOriginalFrame:NO]];
  159.     [_teddyBearSprite runAction:_walkAction];
  160. }
  161.  
  162. - (void) startAnimation
  163. {
  164.     CGSize winSize = [CCDirector sharedDirector].winSize;
  165.     int posX, posY;
  166.     float randDuration;
  167.    
  168.     NSLog(@"Start animation called");
  169.    
  170.     posX = -_teddyBearSprite.contentSize.width;
  171.     posY = [self randomValueBetween:100.0 andValue:(winSize.height - _teddyBearSprite.contentSize.height/2)];
  172.     _teddyBearSprite.position = ccp(posX, posY);
  173.    
  174.     NSLog(@"Posizionato in (%d, %d)", posX, posY);
  175.    
  176.     randDuration = [self randomValueBetween:10.0 andValue:10.0];
  177.    
  178.     //[CCDelayTime actionWithDuration:3.0];
  179.     [_teddyBearSprite runAction:[CCSequence actions:[CCMoveTo actionWithDuration:randDuration position:ccp(winSize.width + _teddyBearSprite.contentSize.width, [self randomValueBetween:0.0 andValue:winSize.height])], [CCCallFuncN actionWithTarget:self selector:@selector(setInvisible)], nil]]; /* Movimento. */
  180. }
  181.  
  182. - (void) pauseAnimation
  183. {
  184.     [_teddyBearSprite pauseSchedulerAndActions];
  185. }
  186.  
  187. - (void) setInvisible
  188. {
  189.     _teddyBearSprite.visible = NO;
  190. }
  191.  
  192. @end


Ultima modifica effettuata da _mikele_ il 11/05/2012 alle 19:39
PM Quote
Avatar
pierotofy (Admin)
Guru^2


Messaggi: 6230
Iscritto: 04/12/2003

Segnala al moderatore
Postato alle 0:19
Sabato, 12/05/2012
Non ho ben capito cosa vuoi fare... ma se stai utilizzando delle actions sulla tua sprite dovrai probabilmente fermarle prime di riposizionare il tuo oggetto...

http://www.cocos2d-iphone.org/api-ref/0.9.0-beta2/interfac ...



Il mio blog: https://piero.dev
PM Quote
Avatar
_mikele_ (Member)
Rookie


Messaggi: 40
Iscritto: 06/12/2010

Segnala al moderatore
Postato alle 15:45
Sabato, 12/05/2012
Ho provato ma non funziona lo stesso comunque ho trovato un altro modo per farlo, forse un po' cattivo ma per ora funziona, elimino semplicemente gli elementi dall'array (:

PM Quote