简体   繁体   English

使多个对象/精灵在屏幕内随机移动?

[英]Making multiple objects/sprites move randomly within a screen?

Basing my question on this original posted question: Making a sprite move randomly across the screen 将我的问题基于此最初发布的问题: 使精灵在屏幕上随机移动

It explains how to move a sprite across the screen starting from any random location within a point location in the screen. 它说明了如何从屏幕上某个点位置内的任意随机位置开始,在整个屏幕上移动精灵。 But my question is how can you modify the code so that an object/sprite (same image file)is randomly put on to the screen multiple times after a specific interval, or so that multiple objects/sprites (different image files) are put on the screen after a specific interval. 但是我的问题是如何修改代码,以便将对象/精灵(相同的图像文件)在特定间隔后多次随机显示在屏幕上,或者将多个对象/精灵(不同的图像文件)放置在屏幕上在指定的时间间隔后屏幕。

Example: I want to have bubbles pop out of nowhere at any point on the screen and start moving around the screen. 示例:我想让气泡在屏幕上的任何地方突然冒出来并开始在屏幕上四处移动。 Bubbles should pop out every 3 seconds. 气泡应每3秒弹出一次。 The bubbles should be able enter the screen at any random location and leave the screen as well. 气泡应该能够在任何随机位置进入屏幕并离开屏幕。

The initialization: 初始化:

 CCSprite* s = [CCSprite spriteWithFile:@"yourImage.png"];
 [self addChild: s];
 [self moveRandom:s];

Moving the sprite around: 移动精灵:

 -(void)moveRandom:(CCSprite*)s
 {
CGPoint randomPoint = ccp(arc4random()%480, arc4random()%320);
NSLog(@"%@", NSStringFromCGPoint(randomPoint));

[s runAction:
 [CCSequence actions:
  [CCMoveTo actionWithDuration:arc4random()%5+1 position: randomPoint],
  [CCCallBlock actionWithBlock:^{
     [self performSelector:@selector(moveRandom:) withObject:s afterDelay:0.5];
   }],
  nil]
 ];
 }

I:m using the latest version of xcode. 我:正在使用最新版本的xcode。

You may be able to use my sample code at: https://github.com/edfrederick/bugsquash.git It was written for Cocos2d HTML5 (which you will find very similar) but has all of the math to do exactly what you are looking to do and should be easily translatable. 您可能可以在以下位置使用我的示例代码: https : //github.com/edfrederick/bugsquash.git它是为Cocos2d HTML5编写的(您会发现它非常相似),但是具有所有的数学功能可以准确地完成您的工作希望这样做,并且应该易于翻译。

Particularly, you simply use a randomizer function to pick a random a point to load the bubble. 特别是,您只需使用随机化函数来随机选择一个点来加载气泡。 Then pick another random point and make the bubble move toward it. 然后选择另一个随机点并使气泡向其移动。 Once the bubble reaches that point you can decide to make it disappear or pick another random point on the screen and have the bubble start moving toward that point. 气泡到达该点后,您可以决定使其消失或在屏幕上选择另一个随机点,并使气泡开始向该点移动。

It may be worth noting that my code moves objects in a linear fashion but you could switch up the movement with a quadratic equation to create a "wobble" effect on the bubble. 可能值得注意的是,我的代码以线性方式移动对象,但是您可以使用二次方程式来切换运动,从而在气泡上产生“摆动”效果。 It just depends on what you are looking for. 这仅取决于您要查找的内容。

Hope this helps. 希望这可以帮助。

Updated With Specific file reference 更新了特定文件参考

https://github.com/edfrederick/bugsquash/blob/master/scripts/actors/BugActor.js is the specific file I am referring to. https://github.com/edfrederick/bugsquash/blob/master/scripts/actors/BugActor.js是我要指的特定文件。 It covers all of the movement ideas you were talking about and is well commented (for me anyway!) 它涵盖了您正在谈论的所有运动想法,并得到了很好的评论(无论如何对我而言!)

Have you tried extracting the bubble creation/initialization into a separate method? 您是否尝试过将气泡创建/初始化提取到单独的方法中? Where you currently have the initialization lines of code, replace them with this: 当前您拥有初始化代码行的位置,将其替换为:

[ self schedule: @selector( createBubble: )
       interval: 3.0f
];

This schedules a new bubble creation every three seconds. 这样会每三秒钟安排一次新的气泡创建。 Then you can have a separate method that does the actual initialization: 然后,您可以有一个单独的方法来执行实际的初始化:

- ( void ) createBubble: ( ccTime ) delta

{

    CCSprite * const s = [ CCSprite spriteWithFile: @"yourImage.png" ];
    [ self addChild: s
                  z: 100
    ];
    [ s setPosition: ccp( arc4random_uniform( 480 ), arc4random_uniform( 320 ) ) ];
    [ self moveRandom: s ];

    return;

}

As for having the bubbles move off-screen, you can either allow them to return randomly, which means that no extra code is required, or you can check the bounds of the screen and the position of the bubble to see if you need to remove the object when it goes off-screen. 至于将气泡移出屏幕,您可以允许它们随机返回,这意味着不需要任何额外的代码,也可以检查屏幕的边界和气泡的位置以查看是否需要移除离开屏幕时的对象。 The simplest bounds check is to look for: 最简单的边界检查是寻找:

"x" < "0 - sprite_width", “ x” <“ 0-sprite_width”,
"x" > "screen_width + sprite_width", “ x”>“ screen_width + sprite_width”,
"y" < "0 - sprite_height", or “ y” <“ 0-sprite_height”,
"y" > "screen_height + sprite_height" “ y”>“ screen_height + sprite_height”

This allows you to be sure that the sprite has completely left the screen before you remove it. 这样可以确保在删除精灵之前,精灵已完全离开屏幕。

Also, if you want to make things appear a little more "bubble-like", you can mix up the motion by replacing the "CCMoveTo" section with the following: 另外,如果您想使事物看起来更像“气泡”,则可以通过将“ CCMoveTo”部分替换为以下内容来混合动作:

CGPoint const ptNow = [ s position ];
CGFloat const oneThirdDiffX = ( randomPoint.x - ptNow.x ) * 0.25f;
CGFloat const oneThirdDiffY = ( randomPoint.y - ptNow.y ) * 0.25f;
CGPoint const ctrlPoint1 = ccp( ptNow.x + oneThirdDiffX, ptNow.y - oneThirdDiffY );
CGPoint const ctrlPoint2 = ccp( randomPoint.x - oneThirdDiffX, randomPoint.y + oneThirdDiffY );

//  move the sprite to the new random point, with a random speed, and then schedule
//  the next update...
[ s runAction: [ CCSequence actions: [ CCBezierTo actionWithDuration: arc4random( ) % 5 + 1
                                                              bezier: (ccBezierConfig){ randomPoint, ctrlPoint1, ctrlPoint2 }
                                     ]
                                   , [ CCCallBlock actionWithBlock:
                                         ^{
                                          [ self performSelector: @selector( moveRandom: )
                                                      withObject: s
                                                     // afterDelay: 0.5f
                                          ];
                                          }
                                     ]
                                   , nil
               ]
];

I hope that this helps.... 我希望这个对你有用....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM