Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411283 Posts in 69325 Topics- by 58380 Members - Latest Member: bob1029

March 29, 2024, 03:18:30 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)[Solved] comparing x values between two objects in game maker
Pages: [1]
Print
Author Topic: [Solved] comparing x values between two objects in game maker  (Read 1162 times)
Conker
Guest
« on: July 18, 2016, 05:27:03 PM »

Code:
(enemyTarget)=instance_nearest(x,y,oFighter)

    if ((enemyTarget.x)<x)
    {
        x-=moveSpeed;
        image_xscale=-1
    }

There are two objects named oFighter, they both will come together and fight each other.. Right now I'm trying to make one walk to the other one.

What am I missing? I feel like this should work. D:
« Last Edit: July 21, 2016, 02:33:49 PM by ConkrMich » Logged
oahda
Level 10
*****



View Profile
« Reply #1 on: July 18, 2016, 11:22:18 PM »

Why are there parentheses around enemyTarget? That should turn it into some sort of temporary expression that wouldn't even be legal in a lot of languages. My guess is due to this weirdness, enemyTarget isn't even getting assigned. Try removing those parentheses?
Logged

Vampade
Level 0
**



View Profile WWW
« Reply #2 on: July 19, 2016, 06:15:15 AM »

In GameMaker if I remember correctly there should be a MoveTowards function. I think that could help you https://docs.yoyogames.com/source/dadiospice/002_reference/movement%20and%20collisions/movement/move_towards_point.html
Logged

mokesmoe
Level 10
*****



View Profile WWW
« Reply #3 on: July 21, 2016, 12:00:17 PM »

instance_nearest doesn't care about what object is calling it; the closest oFighter is itself. Finding the other oFighter is a little tricky. (Also, I'd recommend finding it once and just reuse that variable.)

You could do:
Code:
with(oFighter){
    if(id!=other.id) enemyTarget = id
}
which would iterate through every oFighter (aka. both of them) and add the id of the one that isn't itself to the enemy target variable.

Another way would be to spawn both of them through a control object so you can track their ids:
Code:
fighter1 = instance_create(x,y,oFighter)
fighter2 = instance_create(x+40,y,oFighter)
fighter1.enemyTarget = fighter2
fighter2.enemyTarget = fighter1
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic