I was able to make the targets spawn randomly along the top of the screen and dissappear at the bottom of the screen. The only problem is that the targets only move to one fixed point every time. I want them to move to a random point on 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 Ray Wenderlich's code for this. My question is, how do I make the targets spawn 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 tutorial says it should, the targets move from right to left.
What I am trying to do (should have been more specific) is, instead of making the targets 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 to bottom). Somehow the code is telling the targets to spawn on the right side but I don't know where that is. This is the main meat of the code, nothing else could impact the position of the targets.
-(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 actionWit开发者_运维问答hDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}
(to be honest, this would probably be less confusing posted as another question, but I'll answer it here anyway :P)
Your line :
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(160,0)];
is what tells the sprite where to move. If you want to randomise it, you need to make sure that there is some sort of random function included in the x position of that.
The arc4random() function generates a random number. What you need to do is a little bit of maths to make sure it picks a x position that will still show up on your screen, and feed that 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)];
On another note, just as a coding practice thing, you're probably best off not using raw numbers like 160, 0, etc in your code. Put those into variables (or #defines if they're used in multiple files). That way, when you want to tweak things in the future you only need to change those numbers in one lace, rather than all over your code!
Hope this helps. Good luck!
The following code sets the start 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);
Currently, that randomises the y position by setting the variable actualY to a randomised number within a range. To change it so that the y position is constantly at the bottom of the screen, you'll need to change the actualY variable.
int actualY = target.contentSize.height/2;
This should place the target at the bottom of the screen the start. Note that since I haven't made any changes to the x paramater, it will always appear at the bottom right. I'm sure you can figure out how to change the x parameter!
As for changing which direction it moves, that is done in the following line.
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)];
This creates what Cocos2D calls an Action. This particular one is to move the sprite to the designated position. That position is set inside that function call.
If you try:
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, winSize.height)];
it should start moving upwards, because the move target is now above where it starts. Note that with this function call, its movement to the left will continue because I have not defined a different target in the x direction - I think you can figure out how to make it stop that based on the rest of this!
Good luck, hope this helps!
No matter what I change, nothing really happens.
What have you tried changing?
Specifically, I don't see anything that code that checks for landscape vs. portrait mode or conditionally tries to change a coordinate based upon some queried attribute.
精彩评论