gpt4 book ai didi

ios - 我的 Sprite 从 Y 轴出来

转载 作者:行者123 更新时间:2023-11-29 04:25:04 24 4
gpt4 key购买 nike

我创建了一个敌人在屏幕上随机移动的运动,但有时它会从 Y 轴移动,我该如何使 Sprite 在移动时不会从 Y 轴出来? ????

这是随机移动 Sprite 的代码:

 [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]];

最佳答案

您无法使用 CCMoveBy 和随机距离阻止 Sprite 离开屏幕。随机偏移总有可能以某种方式加起来,使 Sprite 沿任何轴移动很远的距离。

堆叠多个随机 CCMoveBy 操作并不能让您控制 Sprite 最终到达的位置,或者沿途移动到的位置。

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

附注:

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

如果这只将 Sprite 设置为不可见,您可以简单地将其替换为 CCHide 操作。

关于ios - 我的 Sprite 从 Y 轴出来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12430122/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com