Random sprites

I was able to make the targets randomly at the top of the screen and disappear at the bottom of the screen. The only problem is that the targets only move to one fixed point each time. I want them to move to a random point at the bottom of the screen. Any ideas? Thanks.

Here is the code:

- (void) addTarget {

CCSpriteSheet *sheet = [CCSpriteSheet spriteSheetWithFile:@"meteorImgs.png" capacity:50]; [self addChild:sheet]; [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"metImg.plist"]; target = [CCSprite spriteWithFile:@"Frame2.png" rect:CGRectMake(0, 0, 60, 120)]; CGSize winSize = [[CCDirector sharedDirector] winSize]; int minY = target.contentSize.height/2; int maxY = winSize.width*1.5 - target.contentSize.height/2; int rangeY = maxY - minY; int actualY = (arc4random() % rangeY) + minY; target.position = ccp(actualY, target.contentSize.height*5); //- (-target.contentSize.height/2), actualY); [self addChild:target]; int minDuration = 2.0; int maxDuration = 4.0; int rangeDuration = maxDuration - minDuration; int actualDuration = (arc4random() % rangeDuration) + minDuration; id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(160,0)]; id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)]; [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; 

}

Previous:

Hello,

I used the code of Paradise Wenderlich . My question is how to make targets a target at the top of the screen in portrait mode and move to the bottom of the screen. No matter what I change, nothing really happens. Right now, as the textbook says, goals should move from right to left.

What I'm trying to do (should be more specific), instead of forcing the targets to move from the right side of the screen to the left, I want them to move to the bottom of the screen from the top. (Top down). Somehow, the code reports that targets appear on the right side, but I don't know where it is. This is the main meat of the code, nothing else can affect the position of the goals.

 -(void)addTarget { CCSprite *target = [CCSprite spriteWithFile:@"dude_jump.png" rect:CGRectMake(0, 0, 30, 60)]; CGSize winSize = [[CCDirector sharedDirector] winSize]; int minY = target.contentSize.width/2; int maxY = winSize.width - target.contentSize.width/2; int rangeY = maxY - minY; int actualY = (arc4random() % rangeY) + minY; target.position = ccp(winSize.width + (target.contentSize.width/2), actualY); [self addChild:target]; int minDuration = 2.0; int maxDuration = 4.0; int rangeDuration = maxDuration - minDuration; int actualDuration = (arc4random() % rangeDuration) + minDuration; id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)]; id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)]; [target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]]; } 
+4
source share
3 answers

(to be honest, this is likely to be less confusing as another question, but I will answer it anyway: P)

Your line:

 id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(160,0)]; 

- this is what tells the sprite where to move. If you want to randomize it, you need to make sure that at position x there is some random function.

The arc4random () function generates a random number. What you need to do is a little math to make sure that it selects the x position, which will still appear on your screen and pass it on to your move action.

 int randomX = arc4random() % something; // fill in how you want to restrict your x value so that the sprite stays on the screen! id actionMove = [CCMoveTo actionWithDuration:actulDuration position:ccp(randomX, 0)]; 

In another note, like coding practice, you are probably better off not using raw numbers like 160, 0, etc. in your code. Put them in variables (or #defines if they are used in multiple files). Thus, when you want to change the settings in the future, you only need to change these numbers in one lace, and not throughout your code!

Hope this helps. Good luck

+1
source

The following code sets the starting position of the target

  int minY = target.contentSize.width/2; int maxY = winSize.width - target.contentSize.width/2; int rangeY = maxY - minY; int actualY = (arc4random() % rangeY) + minY; target.position = ccp(winSize.width + (target.contentSize.width/2), actualY); 

This currently randomizes the position of y by setting the variable actualY to a randomized number within the range. To change it so that the y position is always at the bottom of the screen, you need to change the current variable.

 int actualY = target.contentSize.height/2; 

This should put the target at the bottom of the screen at the beginning. Please note that since I have not made any changes to x paramater, it will always be displayed in the lower right corner. I am sure you can understand how to change the x parameter!

As for the change in direction of movement, which is performed in the next line.

 id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)]; 

This creates what Cocos2D triggers the action. In particular, this is moving the sprite to the specified position. This position is set inside this function call.

If you try:

 id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, winSize.height)]; 

he must begin to move up, because the goal of the movement is now higher, where it begins. Please note that with this function call, its leftward movement will continue because I did not define another target in the x direction - I think you can figure out how to make it stop based on the rest of this!

Good luck, hope this helps!

+1
source

No matter what I change, nothing really happens.

What were you trying to change?

In particular, I see nothing of such code that checks the landscape or portrait mode or conditionally tries to change the coordinate based on some queried attribute.

0
source

All Articles