0

我已经创建了一个敌人在屏幕上随机移动的动作,但有时它会从 Y 轴移动出来,我该如何让精灵在移动时不会从 Y 轴出来???? ??

这是随机移动精灵的代码:

 
[敌人 runAction:[CCSequence 动作:[CCMoveBy actionWithDuration:2.0 位置:ccp(-winSize.width*0.4, 0)],
                              [CCMoveBy actionWithDuration:randomValueBetween(1.0, 0.3)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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)
                                                  位置: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 位置:ccp(-winSize.width*1.5, 0)],
                              [CCCallFuncN actionWithTarget:self 选择器:@selector(invisNode:)], nil]];
4

1 回答 1

1

您无法使用 CCMoveBy 和随机距离阻止精灵离开屏幕。随机偏移总有可能以这样一种方式加起来,即精灵沿着任何轴移动很长一段距离。

堆叠多个随机 CCMoveBy 动作并不能让您控制精灵最终将在哪里结束,或者它沿途移动到哪里。

您应该将序列拆分为单独的“等待和移动”序列。这样,您可以生成随机偏移,并在创建 CCMoveBy 之前将它们添加到精灵位置以测试生成的位置是否会在屏幕外。在这种情况下,将值限制在屏幕内的某个位置。

PS:

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

如果这只将精灵设置为不可见,您可以简单地用 CCHide 动作替换它。

于 2012-09-15T08:14:30.430 回答