But my AI is too hard :(
Here is my code: it is being called every frame
- (void)stepAI { //check which direction if (delta.y > 0) { //ball is going toward me int compY = pComp.y-8; int ballY = pBall.y-8; int time = (compY-ballY/*distance*/)/delta.y;//divided by speed int latX = time*delta.x+pBall.x; //no wall //bounce if (latX > [self getWidth]) { latX -= [self getWidth]; latX = [self getWidth]-latX; } latX = abs(latX); if (abs(latX - pComp.x) >= ESPEED) { NSLog(@"%i",latX); if (latX < pComp.x) { pComp.x -= ESPEED; } else { pComp.x += ESPEED; } } else { pComp.x = latX; } } else { int latX = wSize.width/2; if (abs(latX - pComp.x) >= ECENTER) { NSLog(@"%i",latX); if (latX < pComp.x) { pComp.x -= ECENTER; } else { pComp.x += ECENTER; } } else { pComp.x = latX; } }}
VARIABLE EXPLANATION
delta = speed
pComp, pBall: points of the center of the ball and computer
wSize: view size
Good enough :P
EDIT: This is my first post
EDIT:
latX: later x :P
ESPEED: enemy speed
ECENTER: Speed to move toward center (set to -1 currently, still too hard)
[Edited by - firefly431 on December 29, 2010 1:54:03 PM]