0
\$\begingroup\$

I've created an sprite class that has a detection radius checking if the player enters the zone. If yes, the sprite moves towards the player. It works perfectly fine when the sprite checks for three directions (X is greater, X is lower, Y is greater/lower), in other words, the sprite only moves up and down if all four directions are enabled..

Here is my code for the move function:

def move(self):
        target_x, target_y = self.target.pos

        if self.mob_wc + 1 <= 24:
                           
                if self.pos_x < target_x:
                    self.vel += vec(self.speed, 0)
                    self.image = self.game.orc_mob_walkRight[self.mob_wc//6]
                    self.mob_wc += 1
                    
                elif self.pos_x > target_x:
                    self.vel = vec(-self.speed, 0)
                    self.image = self.game.orc_mob_walkLeft[self.mob_wc//6]
                    self.mob_wc += 1

                if self.pos_y > target_y:
                    self.vel = vec(0, -self.speed)
                    self.mob_wc += 1

                elif self.pos_y < target_y:                   <-- if this condition is disabled
                    self.vel = vec(0, self.speed)                 The sprites move perfectly upwards
                    self.mob_wc += 1                              towards the player, shifting between
                                                                  moving On X and moving on Y.
        else:                                                     However, if it is enabled, the 
            self.mob_wc = 0                                       sprites ONLY move on the Y field
                

Here is my code for the update function:

    def update(self):
        self.rect = self.image.get_rect()
        self.rect.center = self.pos
        self.pos += self.vel * self.game.dt
        
        self.pos_x, self.pos_y = self.pos
        
        target_dist = self.target.pos - self.pos
        

        if target_dist.length_squared() < self.detect_radi**2:
            self.move()    
        else:
            self.vel = vec(0,0)
            self.image = self.game.orc_mob_img

For the way they move i followed this post. I am aware my build differs due to the use of vectors, which is something that i'm suspecting may be the cause of the issue. If anyone have any idea or tips to why this issue may occur, please tell me cause i've been sitting trying to fix this for 3+ hours now..

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.