• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

[Unresolved]Enemy Tile Collison - with additional random movement -

S

SolariumScum

Guest
I'm trying to get my enemy sprite/objects to not fall off (for certain enemies) or to actually fall off (certain other enemies) the top of the tiles in my 2d Platformer... also when hitting a collision tile object (box's ect...) then just turn around reversing direction -- ALL WHILE KEEPING MY RANDOMIZED DIRECTIONAL HOROZONTAL MOVEMENT.

I'm completely new to coding, like 3 weeks, first game ever attempted. First post ever as well.

I used the tutorial for the Platformer in Game Maker 2 as base -- YOYO PLATFORMER LITE --
I got the randomized direction (sudo AI) for the enemies working but then seems to cancel out the tile edge detection the original game had - essentially letting the enemies continue to go horizontally off the tiles tops, continuing the randomness in direction, but not staying on top of the tiles and reversing to only stay on top (would be great to have gravity make OTHER enemies fall down to next tile top as well) ...additionally when hitting a tile collision (box collision tile) also reversing direction?
Hope someone can help has really stopped my progress on the basic enemy movement and essentially the foundation of the game.


Code:
/// --ENEMY OBJECT CREATE EVENT--
// You can write your code in this editor
dir = choose(-1,1);
   // direction the ghost will be moving in
spd = 0;            // speed the ghost moves
   g= 0.2;        // ghosts gravity
vspd = 0;            // vertical speed the ghost is moving in
hspd = 0;            // horizontal speed the ghost is moving in
////////////////speed the ghost is moving in

sprite_index = walk_duck;        // animation to play
       // default speed of the animation
           // default speed of the animation
  
 ///my random asnimation
healthduck = 4;
knockback = false;
alarm[2] = 60;
ranmov = irandom_range(8,16);


Code:
///--ENEMY OBJECT SCRIPT--


var xx,yy,c1,c2;   
  
                                   // temp var only for use in this script
   dir = choose(-1,1);
  
dir = ranmov;
hspd = dir * spd;                        // find out how far the object has moved
x += hspd;          
                                       // apply the distance to the object
if (hspeed <= 0){
image_xscale = -1;}

                                       // set the sprite to point the correct wayif (hspeed >= 0) {
if (hspeed >= 0){
   image_xscale = 1;}

  
  


if(x < 0){                                // the the enemy has moved off the edge of the screen
       x = room_width;                        // wrap around to the other side of the screen
   }




if(dir == -1){                                                // if moving left
   image_index = walk_duck;                                        // set the sprite to point the correct way
   c1 = -1;                                                // reset the collision tile checkers
   c2 = -1;
   c1 = tilemap_get_at_pixel(oGame.map, x, y);                // check the left edge of the sprite
   c2 = tilemap_get_at_pixel(oGame.map,x,y+sprite_height);                // check below the left edge of the sprite, since ghosts dont like heights
   if((c1 >= 1) || (c2 <= 0)){                                // if there is something blocking the left or if there would be a fall to the left
       x = (x&$ffffffc0)+sprite_width;                        // move the object to the edge of the tile
       dir *= -1;                                            // change the direction
   }
}else if(dir == 1)                                            // if moving right
{                                                            // Otherwise, check collision to the right
   image_index = walk_duck;                                        // set the sprite to point the correct way
   c1 = -1                                                    // reset the collision tile checkers
   c2 = -1;
   c1 = tilemap_get_at_pixel(oGame.map,x+sprite_width,y);                    // check the right edge of the sprite
   c2 = tilemap_get_at_pixel(oGame.map,x+sprite_width,y+sprite_height);    // check below the right edge of the sprite, since ghosts dont like heights
   if((c1 >= 1) || (c2 <= 0)){                                // if there is something blocking the right or if there would be a fall to the right
       x = (x&$ffffffc0);                                    // move the object to the edge of the tile || $ffffffc0 rounds the position to the nearest 64
       dir *= -1;                                            // change the direction
   }
}
 
Last edited by a moderator:
M

mochipon

Guest
Perhaps I'm overlooking something, but it seems to me that every step, dir is randomized again. You have to put something in place that this line: "dir = choose(-1,1);" is only executed ever so often.
The actual displacement of the enemy, "x += hspd" takes places right after the randomization, before the collision code can take place again. If you move that line way down it should make things better.
Still, the way this looks to me, don't your enemies keep wiggling around?

Also, the wraparound won't work when the enemy walks out to the right side of the screen.

Edit:
Actually, what's the deal with "ranmov"? If you randomize it in the create event, the value assigned to the variable stays the same throughout the game. But it will be somewhere between 8 and 16 which doesn't seem to make sense for determining a direction.

Also, you sometimes used hspd and sometimes hspeed...
 
Last edited by a moderator:
S

SolariumScum

Guest
1st - Id need to make a specific Alarm for the line: "dir = choose(-1,1);"
so it happens only so often? Is the alarm the best route I'm guessing?

2nd - The line "x += hspd"
I moved DOWN all the way to the bottom of the script .. no change

3rd - So it does seem to be the "ranmov" which works great, cause they'll randomly between 8 and 16 choose a left or right - I just need to add the collisions on top of that and all is well?

hspeed is built in so highlghts green
hspd was setup separately - finally got the random direction functional?
 
Last edited by a moderator:
M

mochipon

Guest
1 - Perhaps. I guess it depends on what you're aiming for. An alarm might be one way of doing it.
2 - Then there is something else that's wrong, but I'd say for now leave it somewhere at the bottom, so that all the changes to hspd affect the x coordinate. It often doesn't make a difference because game maker keeps "cycling" through the script, but still. :D
3 - I'm not sure if I understand you correctly. If you do "dir = ranmov" that means that dir becomes the same value that ranmov had before, which was somewhere between 8 and 16, and which is then later multiplied by spd to create hspd. Your comment at that point in the code says "find out how far the object has moved", which it totally doesn't at that point. There's gotta be a misunderstanding somewhere at that point.
Beyond that I find it hard to give good advice because I can't see what's going on in your game, how it behaves.

One thing that always helps me is when I use the draw event to write stuff onto the screen. For example you could put in a "draw_text" to give out a characters direction as a string above its head and that'll help you figure out what it does and doesn't.
 
S

SolariumScum

Guest
What happens in the game is really simple

Enemies move left or right walking - they do this randomly between 8,16...sometimes it'll change direction sooner sometimes later than others -- it's like sudo AI.

The problem is - they continually go off the edge of the tiles(ground) floating in the air continuing their horizontal random movement.

Basically they don't detect the collision box tile (ground) EDGE and revers direction to stay on top OR reverse direction when they walk into a Box (collision Tile) as the Ghost do in the demo tutorial.


1 - I just want the collisions to come back with the sudo AI in tact - really don't understand or know how separating the: " dir = choose(-1,1); " with an alarm would accomplish this or any other way of doing it regretfully as putting this in finally got the randomization in movement

2 - hspd has been permanently placed at the bottom of the script lol


3 - every attempt to disassociate "dir" from "ranmov" --completely breaks the nice random movement the enemies give which is wanted.


I've looked literally through like 20+ hours of tutorials to try and just get the collision back through other alternative methods besides the original script for the enemy objects and it could function separately from my random AI code from my understanding

they just will not detected the edge of the tile to turn around or turn around when running into a box/block tile -



If you load the demo you can see how the Ghost react to edges and the boxes - they revers and go the other direction
 
S

SolariumScum

Guest
Working on this, I erased everything from the Script altogether... the enemies still did the awesome randomly chosen horizontal movement, but I didn't realize it was what I set up in the Alarm 2 for the random movement.

Just this controls their random movement and "hspeed" lighting in green when input being a "built in variable" affects it more differently than a custom variable from my understanding.

The alarm is before the script I guess canceling out most of the other code I put in the script? Idk but this and this only does all the randomized horizontal movement for the enemy.

Code:
--ALARM 2--

{
}
alarm_set(2, 60);
hspeed = choose(-1,1);
{
    direction = choose(180,0);

}
 
S

SolariumScum

Guest
So I got it working..

used all original code thst came with the demo and just my added Alarm for the random movement...

My main mistake was not placing the enemies perfectly in the room on the Y axis... after repeatedly testing works great when postiioned at the right hight from the Tile.

Issues is now...when I try any code for flipping the Image_xscale the collisions again fail to work
 
Top