background image

1// objc with cocos2d-iphone

2-(void)addTarget

3{

4 CCSprite *target = [CCSprite spriteWithFile:@"Target.png"

5 rect:CGRectMake(0, 0, 27, 40)];

6

7 // Determine where to spawn the target along the Y axis

8 CGSize winSize = [[CCDirector sharedDirector] winSize];

9 int minY = target.contentSize.height/2;

10 int maxY = winSize.height - target.contentSize.height/2;

11 int rangeY = maxY - minY;

12

13 int actualY = (arc4random() % rangeY) + minY;

14

15 // Create the target slightly off-screen along the right edge,

16 // and along a random position along the Y axis as calculated

17 target.position =

18 ccp(winSize.width + (target.contentSize.width/2),

19 actualY);

20 [self addChild:target];

21

22 // Determine speed of the target

23 int minDuration = 2.0;