Comparing X coordinates of player and enemy

0 favourites
  • 11 posts
From the Asset Store
Basic Rounded Vector Geometry Player Design with Glow for 3 player games
  • First part (under comment) are 3 events for "Run Right", while the next 3 events below it are for "Run Left". Those are for the enemy's AI (chasing after player)

    The rule is for the enemy to run (right or left) towards player if:

    • player is 30 to 120 pixels horizontally away from enemy
    • player is less than 10 pixels vertically away from enemy

    The enemy is supposed to attack the player if they are within 30 pixels or less from each other, but that's not the problem.

    I think the condition encircled is not working. Enemy can run right, but can't run left (even if the player's x is less than enemy's x)

    X_DistanceFromPlayer - instance var that is always set to abs(Playerbox.x - Goonbox.x)

    Y_DistanceFromPlayer - instance var that is always set to abs(Playerbox.y - Goonbox.y)

    AI_Movement_Number = 3 means to run right

    AI_Movement_Number = 4 means to run left

    I would think my conditions are perfectly correct, but why is the enemy not running left when the intended conditions are met?

  • Sure you don't want to give the Enemy a Behavior of Pathfinding?

    You can always add code to that and select that the Pathfinding behavior not rotate from the Properties window.

  • The System events don't pick objects. So when you compare the position using a System condition, the game is just like "Is there a Goon at this position?" instead of "Pick a Goon that is at this position"

    Try using a For Each loop and an instance variable for the Goon

    For Each Goon

    + Goon.X > Player.X

    : Set Goon.position = 1

    + Goon.X < Player.X

    : Set Goon.position = -1

    Then you can compare Goon.position to know if it is on the left or right of the player

  • 7Soul

    Ok. but at this point, even if I am using instance variables and that system event, I only have 1 GoonBox and 1 Goon object in the layout though....


    GoonBox - rectangle invisible object and acts as the enemy, contains all the variables

    Goon - contains the actual animations of goon/enemy, only purpose is animation

    So I run right when the value is "Left" and run left when the value is "Right".

    It still doesn't work. It still makes the enemy keep running right regardless of where I (player) am.

    It's either the first 3 events (in my previous post) are overriding the last 3 events OR the last 3 events just doesn't really work.

    Aside from the "for each" issue you brought up, are there any other mistakes I did in my events (in my previous post) ?


    I tried disabling the first 3 events, the enemy did not run left. So it's really about the last 3 events not working for some unknown reason.

  • Don't use "Compare two values", use the "Compare X" condition of the object you want

  • oh, that actually solved the problem! thanks man.

    So, when can I appropriately use the "System - > Compare two values" condition?

    I would like to ask some questions.

    What is the difference between "Overlapping" and "Colliding"? Some objects doesn't have the "colliding with another object" condition?

    How do you make a "spawned" (or even those not spawned) object's sprite be always "over another object's sprite?

  • So, when can I appropriately use the "System - > Compare two values" condition?

    This one is used to compare... values! But not good for multiple instances of the same object.

    Let's say you want to compare some kind of calculation, like "score * 10 > 1000", then you use "Compare two values"

    See System Conditions

    What is the difference between "Overlapping" and "Colliding"? Some objects doesn't have the "colliding with another object" condition?

    Overlapping is a repeating action, so it will be tested every tick. Something like "player overlapping lava + every 1 second" will fire every second while the player is on lava.

    Colliding is a one time trigger. So you can use like "bomb collides with ground > play sound" and the sound will only play once (but can play again if it collides another time)

    Sometimes "On collision with another object" disappears because you can only have one trigger event on a condition (trigger events are represented by a green arrow before them)

    See Sprite

    How do you make a "spawned" (or even those not spawned) object's sprite be always "over another object's sprite?

    You can have "move to top" or "move to object" on every tick

    See Common actions

  • Ok thanks. I want to ask something about the "For Each" you previously mentioned.

    So with the fixing of the run events, I finished the system of my basic enemies. I used instance variables to make the enemy move, attack, take damage and die. So I thought I can create multiple instances already of my enemy in the layout and they will behave (independent from each other) using the same events. But they did not.

    I did not use a container (I have my own reasons why), but here is how I coded it:

    I use families except for object specific events.

    GoonBoxes (family name) - contains different goonbox objects, but I only have 1 object under this family at present

    Goons (family name) - different objects in this, but only 1 goon coded (the one corresponding to the only goonbox I have so far)

    +start of layout

    ->Goonbox (not the family) object spawns goon object (not the family)

    an instance var for AI is randomized every 3 secs to 1 or 2 (for walking left and right) and it will keep walking left and right until it sees the enemy (this is when it will run towards player and attack it and goes back to stance and can go to run and/or attack again if the player is near). Distance from player is being checked every tick.

    Attack animation uses object instead of family

    +Goon's animation "Attack" is playing

    +animation frame = 7

    +Goon colliding with Playerbox

    ->Set Player to Damage (this isn't the real event, it's too long to type but you get the idea)

    Then everything else is in instance variables.

    So because it doesn't work for multiple instances, I tried adding "For Each GoonBoxes (family) object" condition to each and every single event related to my enemies, but I get an error about loop like this:

    How do I fix this and make my enemies behave independently from each other using the same events?

    I really thought the essence of instance vars was for this situation but ugh.... help please.

  • Why do you need GoonBox at all?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Because the manual said to use a rectangle invisible object for platform behavior and pin the actual object (containing the animations) to the rectangle object

  • Anyone can help?

    Even using containers did not solve the problem

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)