0
votes

I am developing a game sprite kit. I have the player moving through the scene and enemies spawn randomly around the screen. The problem is that I can not persecute the enemy to the player sprite. I intend to implement an observer pattern so that the enemies know at all times where the player sprite, but I did not succeed. I've also thought about implementing a delegate, but still have not succeeded. know any way to get it?. thank you very much

this code shows my scene. my player moves CoreMotion and enemies appear. I do not know how to make enemies chase the player. plis help me. thank you very much

@implementation PRMyScene

-(id)initWithSize:(CGSize)size {

    if (self = [super initWithSize:size]) {

    _jugador = [SKSpriteNode spriteNodeWithImageNamed:@"Player.png"];
    _jugador.position = CGPointMake(100.0, 100.0);
    [self addChild: _jugador];

    _enemigo =  [SKSpriteNode spriteNodeWithImageNamed:@"Cannon.png"];
    _enemigo.position = CGPointMake(200.0, 120.0);
    [self addChild:_enemigo];

    _motionManager = [CMMotionManager new];
    _motionManager.deviceMotionUpdateInterval = 1.0 / 60.0;
    [self starMonitorizacionAceleracion];

     _winSize = CGSizeMake(self.size.width, self.size.height);

    }
    return self;
}


-(void)didBeginContact:(SKPhysicsContact *)contact{
      NSLog(@"contacto!!!!!!");
}

-(void)starMonitorizacionAceleracion{
    if (_motionManager.deviceMotionAvailable) {
        [_motionManager startDeviceMotionUpdates];
        NSLog(@"Actualizaciones del acelerometro ON ...");
    }
}

// this methoss only is for new CGPoint to move player in the update
-(CGPoint)actualizaMovimiento{

    CMDeviceMotion * motionData = _motionManager.deviceMotion;
    CMAcceleration gravedad = motionData.gravity;

    CGPoint pos =_jugador.position;
    pos.x -= gravedad.y * KRapidezdeMvimiento;
    pos.y += gravedad.x * KRapidezdeMvimiento;

    //prevenir que el jugador se salga de la pantalla
    //prevenir que el jugador salga de la pantalla
    float imageMitad= _jugador.texture.size.width * 0.5f;

    float izquiBordeLimite = imageMitad;
    float derecBordeLimite = _winSize.width - imageMitad;

    float superBordeLimite = _winSize.height - imageMitad;
    float inferBordeLimite = imageMitad;

    if (pos.x < izquiBordeLimite) {
        pos.x = izquiBordeLimite;
    } else if (pos.x > derecBordeLimite) {
        pos.x = derecBordeLimite;
    }

    if (pos.y < inferBordeLimite) {
        pos.y = inferBordeLimite;
    }else if (pos.y > superBordeLimite) {
        pos.y = superBordeLimite;
    }

    return pos;
}
-(void)update:(CFTimeInterval)currentTime {
    _jugador.position = [self actualizaMovimiento];

}

@end
1

1 Answers

1
votes
-(void)update:(CFTimeInterval)currentTime {
    _jugador.position = [self actualizaMovimiento];

    int iSteps = 1; // Speed of you enemy
    CGPoint pos = _enemigo.position;

    if(_jugador.position.x < pos.x) {
      pos.x=pos.x-iSteps;
    } else {
      pos.x=pos.x+iSteps;
    }

    if(_jugador.position.y < pos.y) {
      pos.y=pos.y-iSteps;
    } else {
      pos.y=pos.y+iSteps;
    }

    _enemigo.position=pos;
}