Hi Im trying to get an enemy to move from top to bottom, I am in landscape mode.
I have been studying this code and have modified it to move on the X axis. Unfortunately the enemy moves from top to bottom but diagonally as if its being dragged to the y axis. Would really appreciate if anyone has an idea what the problem is?
Thanks
-(void)addTarget {
CCSprite *target = [CCSprite spriteWithFile:@"Target.png"
rect:CGRectMake(0, 0, 27, 40)];
// Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY = target.contentSize.height/2;
int maxY = winSize.height - target.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target.position = ccp(winSize.width + (target.contentSize.width/2), actualY);
[self addChild:target];
// Determine speed of the target
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
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]];
}
my code looks like this -
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = enemy.contentSize.width/2;
int maxX = winSize.width - enemy.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
enemy.position = ccp(actualX, winSize.height + (enemy.contentSize.height/2));
[self addChild:enemy];
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
id actionMove = [CCMoveTo actionWithDuration:actualDuration
position:ccp(-enemy.contentSize.height/2, actualX)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:@selector(spriteMoveFinished:)];
[enemy runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];