Comparing X coordinates of player and enemy

Get help using Construct 2

Post » Sun Mar 09, 2014 1:17 pm

Image

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?
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Sun Mar 09, 2014 6:54 pm

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

Image

You can always add code to that and select that the Pathfinding behavior not rotate from the Properties window.
B
19
S
7
G
7
Posts: 88
Reputation: 4,102

Post » Sun Mar 09, 2014 7:00 pm

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
B
83
S
22
G
15
Posts: 1,039
Reputation: 15,219

Post » Mon Mar 10, 2014 5:56 am

@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....

Note:
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

Image

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) ?

Edit:
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.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Mon Mar 10, 2014 8:43 am

Don't use "Compare two values", use the "Compare X" condition of the object you want
B
83
S
22
G
15
Posts: 1,039
Reputation: 15,219

Post » Mon Mar 10, 2014 10:29 am

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?
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Mon Mar 10, 2014 7:08 pm

sgn15 wrote: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

sgn15 wrote: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

sgn15 wrote: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
B
83
S
22
G
15
Posts: 1,039
Reputation: 15,219

Post » Tue Mar 11, 2014 1:32 am

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: http://oi62.tinypic.com/oswjf8.jpg

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.
B
16
S
7
Posts: 301
Reputation: 2,726

Post » Tue Mar 11, 2014 2:26 am

Why do you need GoonBox at all?
B
83
S
22
G
15
Posts: 1,039
Reputation: 15,219

Post » Tue Mar 11, 2014 3:05 am

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
B
16
S
7
Posts: 301
Reputation: 2,726

Next

Return to How do I....?

Who is online

Users browsing this forum: Alvaro2 and 15 guests