Im trying to program a Age of War like game. This game is programmed in p5.js with the p5.play library. Im having trouble having sprites moving towards each other and stopping and then activating a function. I’am currently changing the collisions of the sprites form static to dynamic to keep them from bouncing away from each other. But every other run the sprites bounces away from each other. Can anybody help me
class SpriteBuilder {
constructor(Type, Team) {
if (Type == "Melee") {
this.Melee = new Melee(Team);
this.ApplyTeam(this.Melee.Sprite, Team);
}
}
ApplyTeam(Sprite, Team) {
if (Team == "Left") {
Sprite.color = "Blue";
Sprite.pos = { x: 10, y: 480 };
}
if (Team == "Right") {
Sprite.color = "Red";
Sprite.pos = { x: 690, y: 480 };
}
}
}
class Melee {
constructor(Team) {
this.Team = Team;
if (Team == "Left") {
this.Sprite = new Sprite();
TeamLeft.push(this);
}
if (Team == "Right") {
this.Sprite = new Sprite();
TeamRight.push(this);
}
this.Attackinterval;
this.currentTarget;
this.life = 200;
this.Sprite.width = 20;
this.Sprite.height = 20;
this.Sprite.text = "Me";
this.Sprite.textColor = "White";
this.Move()
}
Move(){
if (this.Team=="Left"){
this.Sprite.collider="d"
this.Sprite.direction="Right"
this.Sprite.speed=1
}
if (this.Team=="Right"){
this.Sprite.collider="d"
this.Sprite.direction="Left"
this.Sprite.speed=1
}
}
CollideCheck() {
if (this.Team == "Left") {
//Collide Red Blue
for (let i = 0; i < TeamRight.length; i++) {
if (TeamRight[i].Sprite.collides(this.Sprite)) {
this.Sprite.collider = "s";
TeamRight[i].Sprite.collider="s";
this.DealDamage(TeamRight[i]);
console.log("Rød skader")
}
}
//Collide Red Red
for (let i = 0; i < TeamLeft.length; i++) {
if (TeamLeft[i].Sprite.collides(this.Sprite)) {
this.Sprite.collider = "s";
TeamLeft[i].Sprite.collider = "s";
}
}
}
if (this.Team == "Right") {
//Collide Blue Red
for (let i = 0; i < TeamLeft.length; i++) {
if (TeamLeft[i].Sprite.collides(this.Sprite)) {
this.Sprite.collider = "s";
TeamLeft[i].Sprite.collider="s";
this.DealDamage(TeamLeft[i]);
console.log("Blå skader")
}
}
//Collide Blue Blue
for (let i = 0; i < TeamRight.length; i++) {
if (TeamRight[i].Sprite.collides(this.Sprite)) {
this.Sprite.collider = "s";
}
}
}
}
AmIDead() {
console.log(this.life);
if (this.life <= 0) {
if (this.Team == "Left") {
let index = TeamLeft.indexOf(this.Sprite);
TeamLeft.splice(index, 1);
}
if (this.Team == "Right") {
let index = TeamRight.indexOf(this.Sprite);
TeamRight.splice(index, 1);
}
this.Sprite.remove();
}
}
DealDamage(CollidedSprite) {
if (this.Attackinterval!=null)clearInterval(this.Attackinterval);
this.Attackinterval = setInterval(
() => {
if (CollidedSprite == null) {
clearInterval(this.Attackinterval);
} else {
CollidedSprite.life -= 10;
}
},
1000
);
}
}