Hello again,
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 actionWithDuration:actualDuration position:ccp(-target.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)];
[target runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}