简体   繁体   中英

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

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.

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. 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" > "screen_width + sprite_width",
"y" < "0 - sprite_height", or
"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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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