GML code problem: enemy centering focus
Author Message
EXtremeXPerience Offline
(uhhrmmm...)
***
Registered

Posts: 71
Threads: 5
Joined: Sep 2016
Reputation: 0
#1
GML code problem: enemy centering focus
A sample problem I encountered just days ago... and still I can't solve this problem!

Basically, I have this script, embedded already with the program, named centerEnemyScript, and this is how the code looks:

if instance_exists(megaManObj)
{
if x <= megaManObj.x+sprite_width/2
{sprite_index = argument0;}
if x > megaManObj.x+sprite_width/2
{sprite_index = argument1;}
}
else
{sprite_index = choose(argument0,argument1);}

Not a problem for me, but checking out with debugs, I encountered this! MM - Ill





When the enemy is on the right, and you're just pixels away on his left side, he doesn't jump left. Otherwise, it does well. Anyway to solve this code problem? MM - What
03-01-2017, 02:50 PM
Find Reply
Smedis2 Offline
Are video games real?
*****
Registered

Posts: 349
Threads: 34
Joined: Jun 2014
Reputation: 16
#2
RE: GML code problem: enemy centering focus
if (megamanexistsorwhatever)
{
if x <= megaManObj.x + sprite_width / 2 //Greater or equal? That's a bit odd.
{
sprite_index = argument0; \\What does this do?
}
if x > megaManObj.x + sprite_width / 2 //Are you sure the Sprite_width / 2 isn't screwing it up? Try getting the player's X position instead, unless you have the offsetting done weirdly.
{
sprite_index = argument1; //What does this do?
}
}

CHECK OUT STUFF I MAKE OVER AT
[Image: d03b37145145880c6532998fa3a49c20.png]
Here's my TWITTER
(This post was last modified: 03-01-2017, 08:24 PM by Smedis2.)
03-01-2017, 08:15 PM
Website Find Reply
EXtremeXPerience Offline
(uhhrmmm...)
***
Registered

Posts: 71
Threads: 5
Joined: Sep 2016
Reputation: 0
#3
RE: GML code problem: enemy centering focus
(03-01-2017, 08:15 PM)Smedis2 Wrote:
if (megamanexistsorwhatever)
{
if x <= megaManObj.x + sprite_width / 2 //Greater or equal? That's a bit odd.
{
sprite_index = argument0; \\What does this do?
}
if x > megaManObj.x + sprite_width / 2 //Are you sure the Sprite_width / 2 isn't screwing it up? Try getting the player's X position instead, unless you have the offsetting done weirdly.
{
sprite_index = argument1; //What does this do?
}
}

Oooops... sorry, I forgot to elaborate.

The script centerEnemyScript(argument1,argument0) //definitely means...

if instance_exists(megaManObj) //<- if character exists
{
if x <= megaManObj.x+sprite_width/2 //<- if enemy is less than or equal to character's x point + character's sprite width divided by 2
{sprite_index = argument0;} //<- then enemy's sprite is the first sprite or facing left
if x > megaManObj.x+sprite_width/2 //<- if enemy is greater than the character's x point + character's sprite width divided by 2
{sprite_index = argument1;} //<- then enemy's sprite is the second sprite or facing right
}
else
{sprite_index = choose(argument0,argument1);} // else, it's the enemy choice whether he'll face left or right

Actually, smedis2, I want the enemy to face MegaMan in the right direction and with most accurate position. For example, MegaMan's width sprite is 42 and enemy's sprite is 32, I want the enemy to face MegaMan left with (x + 5), when MegaMan advance by 1 pixel, or simply (x + 6), the enemy faces right. Did you get it?
(This post was last modified: 04-01-2017, 03:09 AM by EXtremeXPerience.)
04-01-2017, 03:08 AM
Find Reply
EXtremeXPerience Offline
(uhhrmmm...)
***
Registered

Posts: 71
Threads: 5
Joined: Sep 2016
Reputation: 0
#4
RE: GML code problem: enemy centering focus
Update: What I need is precision in this code.

Need to have a update re. fixing this code. Anyone?

[Image: self1_by_megamanno99-dakh6k5.png]
09-01-2017, 03:52 AM
Find Reply
Lamda Offline
Ace of Spades
*****
Registered

Posts: 954
Threads: 28
Joined: May 2012
Reputation: 62
#5
RE: GML code problem: enemy centering focus
I don't know why you're relying on multiple sprite files when image_xscale can do what you're wanting without the need for them, so...

(I am assuming your base sprite faces right. If it faces left, swap the image_xscale values.)

//Step Event
Code:
if instance_exists(megaManObj)
    {
        if x>=megaManObj.x+(sprite_index/2)
            {
                image_xscale=1
            }
        if x<=megaManObj.x-(sprite_width/2)
            {
                image_xscale=-1
            }
    }
        else
    {
        image_xscale=choose(-1,1)
    }

//Draw Event
Code:
draw_sprite_ext(sprite_index,-1,x,y,image_xscale,image_yscale,image_angle,c_white,image_alpha)

Now, if you'd rather use sprites for each direction, use the following instead.

//Step Event
Code:
if instance_exists(megaManObj)
    {
        if x>=megaManObj.x+(sprite_index/2)
            {
                sprite_index=sprFaceLeft
            }
        if x<=megaManObj.x-(sprite_width/2)
            {
                sprite_index=spFaceRight
            }
    }
        else
    {
        sprite_index=choose(sprFaceLeft,spFaceRight)
    }

Quote:Game making in 6 steps:
1: Get assets and program
2: Smash the two together until stuff happens
3: Beat your face into your keyboard when stuff breaks/doesn't want to work
4: Continue beating your face into your keyboard until you've smashed the right code
5: Rejoice that it works and move on to the next thing
6: Go back to Step 2 and repeat
09-01-2017, 07:42 AM
Find Reply
EXtremeXPerience Offline
(uhhrmmm...)
***
Registered

Posts: 71
Threads: 5
Joined: Sep 2016
Reputation: 0
#6
RE: GML code problem: enemy centering focus
(09-01-2017, 07:42 AM)Lamda Wrote:
I don't know why you're relying on multiple sprite files when image_xscale can do what you're wanting without the need for them, so...

(I am assuming your base sprite faces right. If it faces left, swap the image_xscale values.)

//Step Event
Code:
if instance_exists(megaManObj)
    {
        if x>=megaManObj.x+(sprite_index/2)
            {
                image_xscale=1
            }
        if x<=megaManObj.x-(sprite_width/2)
            {
                image_xscale=-1
            }
    }
        else
    {
        image_xscale=choose(-1,1)
    }

//Draw Event
Code:
draw_sprite_ext(sprite_index,-1,x,y,image_xscale,image_yscale,image_angle,c_white,image_alpha)

Now, if you'd rather use sprites for each direction, use the following instead.

//Step Event
Code:
if instance_exists(megaManObj)
    {
        if x>=megaManObj.x+(sprite_index/2)
            {
                sprite_index=sprFaceLeft
            }
        if x<=megaManObj.x-(sprite_width/2)
            {
                sprite_index=spFaceRight
            }
    }
        else
    {
        sprite_index=choose(sprFaceLeft,spFaceRight)
    }

It still doesn't solve my problem. Confused

Lemme get straight to the point.

MegaMan's width is 42 pixels; and each boss is larger than him. For example, Ballade is 60 pixels.

[Image: flip1_by_megamanno99-dausjwy.png]

If MegaMan's on the left, Ballade will face right...

[Image: flip2_by_megamanno99-dausjx1.png]

...and still does not face right until MegaMan is on the exact center position...

[Image: flip3_by_megamanno99-dausjs5.png]

... and move by only 1 pixel, then Ballade will face right. Precisely.

That's what I really needed for the modification of the code, and not the first code that I have(the one with the included video on it.)
09-01-2017, 12:16 PM
Find Reply
Lamda Offline
Ace of Spades
*****
Registered

Posts: 954
Threads: 28
Joined: May 2012
Reputation: 62
#7
RE: GML code problem: enemy centering focus
You're doing an abysmal job of explaining what the hell you want, then.

Can't help you if I don't understand what you're wanting or what's not working. All you keep doing is quoting pixel dimensions and talking about Ballade turning precisely. Neither of those things tell anyone what you want him to do versus what he's doing wrong.

Now, if I'm reading this last post right and you want him to turn precisely, then I know you didn't test the code I gave you because I know for a fact that's what it does (tested it myself multiples times before posting it).

Quote:Game making in 6 steps:
1: Get assets and program
2: Smash the two together until stuff happens
3: Beat your face into your keyboard when stuff breaks/doesn't want to work
4: Continue beating your face into your keyboard until you've smashed the right code
5: Rejoice that it works and move on to the next thing
6: Go back to Step 2 and repeat
09-01-2017, 01:27 PM
Find Reply
Rhythm Offline
Rhythm is Gonna Get Ya
*******
Co-Admin

Posts: 1,016
Threads: 69
Joined: May 2012
Reputation: 41
#8
RE: GML code problem: enemy centering focus
[Image: flip1_by_megamanno99-dausjwy.png]

why is his ankle broken?

[Image: 67617b7e1d6Ur.png]
09-01-2017, 01:54 PM
Website Find Reply
EXtremeXPerience Offline
(uhhrmmm...)
***
Registered

Posts: 71
Threads: 5
Joined: Sep 2016
Reputation: 0
#9
RE: GML code problem: enemy centering focus
(09-01-2017, 01:27 PM)Lamda Wrote: You're doing an abysmal job of explaining what the hell you want, then.

Can't help you if I don't understand what you're wanting or what's not working. All you keep doing is quoting pixel dimensions and talking about Ballade turning precisely. Neither of those things tell anyone what you want him to do versus what he's doing wrong.

Now, if I'm reading this last post right and you want him to turn precisely, then I know you didn't test the code I gave you because I know for a fact that's what it does (tested it myself multiples times before posting it).

I tested it already, calculated it a few times and re-run it many times before I replied, making sure that the code works, but it still ain't.

...and mind, I reply if I am "really sure" of what I am doing. Would you think I reply here if I am sure of the answers? Probably not... so keep your cool! Wink I am not barbaric like what you think.

(09-01-2017, 01:54 PM)Rhythm Wrote: [Image: flip1_by_megamanno99-dausjwy.png]

why is his ankle broken?

Actually, it's the Neo-Geo version of MegaMan sprite re-colored, so you see why some sprites look odd.
09-01-2017, 03:19 PM
Find Reply
Lamda Offline
Ace of Spades
*****
Registered

Posts: 954
Threads: 28
Joined: May 2012
Reputation: 62
#10
RE: GML code problem: enemy centering focus
Well, there's the problem. I typo'd and put sprite_index instead of sprite_width.

Anyway, if it's precise checking you want, change sprite_width/2 to sprite_width/32.

[Image: W0AAOFo.gif]

Works just fine.

Quote:Game making in 6 steps:
1: Get assets and program
2: Smash the two together until stuff happens
3: Beat your face into your keyboard when stuff breaks/doesn't want to work
4: Continue beating your face into your keyboard until you've smashed the right code
5: Rejoice that it works and move on to the next thing
6: Go back to Step 2 and repeat
09-01-2017, 04:23 PM
Find Reply
Valo Offline
Nooblet Tour guide
*****
Registered

Posts: 629
Threads: 11
Joined: May 2012
Reputation: 26
#11
RE: GML code problem: enemy centering focus
Thats not a recolored neo geo megaman at all

[Image: RF8iZPq.png]
10-01-2017, 04:22 AM
Find Reply
EXtremeXPerience Offline
(uhhrmmm...)
***
Registered

Posts: 71
Threads: 5
Joined: Sep 2016
Reputation: 0
#12
Lightbulb  RE: GML code problem: enemy centering focus
(09-01-2017, 04:23 PM)Lamda Wrote:
Well, there's the problem. I typo'd and put sprite_index instead of sprite_width.

Anyway, if it's precise checking you want, change sprite_width/2 to sprite_width/32.

[Image: W0AAOFo.gif]

Works just fine.

Coding is finally done. Credits to yah! Smile
17-02-2017, 04:53 AM
Find Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)

Return to Top