-3
\$\begingroup\$

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..

\$\endgroup\$
4
  • \$\begingroup\$ Can't understand what you mean by "comes out the screen". Can you post a screenshot and explain better what the problem is? \$\endgroup\$ Commented Sep 17, 2012 at 1:26
  • 1
    \$\begingroup\$ @NathanReed I think he means to ask how you can keep the character within the screen boundaries. \$\endgroup\$
    – Sidar
    Commented Sep 17, 2012 at 2:21
  • \$\begingroup\$ Not clear and too localized in my opinion. \$\endgroup\$
    – House
    Commented Sep 17, 2012 at 3:26
  • \$\begingroup\$ @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. \$\endgroup\$
    – IlNero
    Commented Sep 17, 2012 at 10:04

1 Answer 1

1
\$\begingroup\$

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)]
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .