Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If i an action moves the sprite,how can i keep the CCSprite on the screen???? this is my code:

[enemy runAction:[CCSequence actions:[CCMoveBy actionWithDuration:2.0 position:ccp(-winSize.width*0.4, 0)],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(winSize.width*0.2, -winSize.width*0.2),
                                                               randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(winSize.width*0.2, -winSize.width*0.2),
                                                               randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(winSize.width*0.2, -winSize.width*0.2),
                                                               randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(winSize.width*0.2, -winSize.width*0.2),
                                                               randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(winSize.width*0.2, -winSize.width*0.2),
                                                               randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(-winSize.width*0.3,winSize.width*0.3),
                                                       randomValueBetween(winSize.height*0.3, -winSize.height*0.3))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(-winSize.width*0.2,winSize.width*0.2),
                                                       randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(-winSize.width*0.3,winSize.width*0.3),
                                                       randomValueBetween(winSize.height*0.3, -winSize.height*0.3))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(-winSize.width*0.2,winSize.width*0.2),
                                                       randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(-winSize.width*0.3,winSize.width*0.3),
                                                       randomValueBetween(winSize.height*0.3, -winSize.height*0.3))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:ccp(randomValueBetween(-winSize.width*0.2,winSize.width*0.2),
                                                       randomValueBetween(winSize.height*0.2, -winSize.height*0.2))],
                      [CCDelayTime actionWithDuration:0.5],
                      [CCMoveBy actionWithDuration:2.0 position:ccp(-winSize.width*1.5, 0)],
                      [CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)], nil]];

but whit this code the sprite sometimes comes out the screen, i need the sprite moves randomly in the screen without comes out..

share|improve this question
    
Can't understand what you mean by "comes out the screen". Can you post a screenshot and explain better what the problem is? –  Nathan Reed Sep 17 '12 at 1:26
1  
@NathanReed I think he means to ask how you can keep the character within the screen boundaries. –  Sidar Sep 17 '12 at 2:21
    
Not clear and too localized in my opinion. –  Byte56 Sep 17 '12 at 3:26
    
@NathanReed I would like to say that sometimes, when the sprite move,it goes out from the axys Y. I want that the sprite dosen't go out from the screen. I want that the sprite remains inside the limits of the screen. I'd like that when the sprite arrives at the top or bottom of the screen,it dosen't go out from the screen. –  IlNero Sep 17 '12 at 10:04
add comment

1 Answer

To limit the movement within the screen bounds, get the position to ccmoveto first, here which is determined by random. Perform a check to see if the position is within the bounds of the screen. If it is, then do nothing. If the new position is out of bounds, then set it to a position within bounds.

I am not sure how your anchor/relative points are set. On my projects, negative usually means outside of the screen.

To integrate, I would suggest creating a separate function that returns the position desired:

Using my settings and taking into account the sprite/node size, for example:

-(CGPoint) boundPosition(CGPoint desiredPosition, CGRect spriteSize) {
  CGPoint newPosition = desiredPosition;
  //position is outside of the screen on the left
  if (desiredPosition.x < (0 + spriteSize.width/2)) {
    //set the position to the leftmost possible
    newPosition.x = spriteSize.width/2;
  }
  //position is outside of the screen on the right
  if (desiredPosition.x > (winSize.width - spriteSize.width/2)) {
    //set the position to the rightmost possible
    newPosition.x = winSize.width - spriteSize.width/2;
  }
  //position is below the screen
  if (desiredPosition.y < (0 + spriteSize.height/2)) {
    //set the position to the bottommost possible
    newPosition.y = spriteSize.height/2;
  }
  //position is above the screen
  if (desiredPostiion.y > (winSize.height - spriteSize.height/2)) {
    //set position to uppermost possible
    newPosition.y = winSize.height - spriteSize.height/2;
  }
}

Should be able to plug into each CCMoveBy as so:

[CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                          position:boundPosition(ccp(randomValueBetween(winSize.width*0.2, winSize.width*0.6),
                                                               randomValueBetween(winSize.height*0.2, winSize.height*0.6)), enemy.contentSize)]
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.