0

I'm working on tower defense game and I'm using stencyl.

I want to make 2d tower defense game like (clash of clans), so I want to know how to make a turret pointing at an object using frames like (canon in clash of clans).

I mean when an object enters the range of tower the tower will point at it without rotating the tower but using 2d frames instead by some way using code or mathematical way.

double-beep
  • 5,031
  • 17
  • 33
  • 41
ali kamel
  • 33
  • 1
  • 4
  • Welcome to Stack Overflow. Please read about [asking questions](https://stackoverflow.com/help/asking). Please [edit] the question to clearly narrow down the question as right now it is very broad. Are you asking about how to select sprites that will give the appearance of a "tower" pointing in a specific direction? – Jason Aller Jun 06 '19 at 22:52
  • something like that. but I want it with code or mathematical way I've already edit the question for more understand. – ali kamel Jun 07 '19 at 14:46

1 Answers1

0

I've found the solution. Do this :

float Direction = 0;
float FinalDirection = 0;
float DirectionDegree = 0;
int NumberOfDirections = 0; // eg: 24 or 32 or even 128 or anything Directions

DirectionDegree = 360 / NumberOfDirections;

void update() // this will run every frame
{
    Direction = Math.atan2(target.y - tower.y, target.x - tower.x ) * (180 / Math.PI);

    if(Direction < 0)
    {
        Direction += 360;
    }

    FinalDirection = Direction / DirectionDegree;
    tower.frame = FinalDirection;
}
ali kamel
  • 33
  • 1
  • 4