0
votes

I have created a movement of the enemy that moves randomly around the screen, but sometimes it comes out from the axis Y moving, how do I make so that the sprite doesn't come out from the axis Y when it moves??????

this is the code randomly move the sprites:

 
[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]];
1
I don't understand the problem. What do you mean by "comes out from the axis Y"? What happens exactly, and what do you expect to happen? - LearnCocos2D
I would like to say that sometimes, when the sprite move, its exit off axis y. I want that the sprite doesn't go out from the screen. I'd like that when the sprite arrives at the top or the bottom of the screen it comes back. - IlNero

1 Answers

1
votes

You can't prevent the sprite going off screen with CCMoveBy AND random distances. There's always a chance that the randomized offsets together add up in such a way that the sprite moves a great distance along any axis.

Stacking multiple random CCMoveBy actions does not give you control over where the sprite will ultimately end up, or where it moves to along the way.

You should split up the sequence into individual "wait & move" sequences. That way, you can generate random offsets and before creating the CCMoveBy add them to the sprite position to test if the resulting position will be off screen. In that case, cap the values to a location within the screen.

PS:

[CCCallFuncN actionWithTarget:self selector:@selector(invisNode:)]

If that only sets the sprite to invisible, you could simply replace it with the CCHide action.