# GMLscripts.com

Discuss and collaborate on GML scripts

You are not logged in.

## #21 2014-06-05 02:08:27

xot
Registered: 2007-08-18
Posts: 1,239

### Re: Help with instance_nth_nearest or something similar

This part, the last two checks, you are adding to and subtracting from y with hspeed. You should be using ghost_speed. Using hspeed will cause the checks to look in the opposite of the intended directions when hspeed is negative (ghost is moving left).

Expand        //horizontal movement
if vspeed = 0 {
//check for open position
if (!place_meeting(x + hspeed, y, obj_wall)) {
which_way = 3;
}
if (!place_meeting(x, y + hspeed, obj_wall)) {
which_way = 1;
}
if (!place_meeting(x, y - hspeed, obj_wall)) {
which_way = 5;
}

There is the same problem here, use ghost_speed instead of hspeed.

Expand            // if going forward & down
if (which_way = 4) {
if irandom(1) = 0 {
if (!place_meeting(x, y + hspeed, obj_door)) {
direction = 270;
exit;
}
}
}

Here you should adding 180 to direction to make the ghost reverse its current direction.

Expand            // if not moving
if (which_way = 0) {
direction = 180;
exit;
}

There are the same sorts of problems in the second half.

For the first check you should be adding vspeed to y instead of hspeed. For the last two checks you should be adding to and subtracting from x with ghost_speed.

Expand        else {
//vertical movement
//check for open position
if (!place_meeting(x, y + hspeed, obj_wall)) {
which_way = 3;
}
if (!place_meeting(x + hspeed, y, obj_wall)) {
which_way = 1;
}
if (!place_meeting(x - hspeed, y, obj_wall)) {
which_way = 5;
}

And again here you should be adding 180.

Expand            // if not moving
if (which_way = 0) {
direction = 180;
exit;
}
// end vertical moving

I don't know if that will fix all of the problems but they are significant changes.

In answer to your question, speeds of 1 or 2 should work fine. I don't understand why they wouldn't work when 4 does.

Abusing forum power since 1986.

Offline

## #22 2014-06-05 10:42:30

koltz
Member
Registered: 2014-05-26
Posts: 15

### Re: Help with instance_nth_nearest or something similar

I ended up kind of combing the two scripts. irandom works better than what they were using.

Anyway, again I appreciate the help. I am looking through the code. After some experimenting, if I have ghost_speed = 1, one of the enemies will move around the screen, 2 and two will move around the screen, 4 and all of them.

Offline

## #23 2014-06-05 14:24:23

koltz
Member
Registered: 2014-05-26
Posts: 15

### Re: Help with instance_nth_nearest or something similar

Figured it out. I switch the Bounding Box on the sprites from Automatic to Full Image and that fixed it.

Offline

## #24 2014-06-05 14:56:11

xot
Registered: 2007-08-18
Posts: 1,239

### Re: Help with instance_nth_nearest or something similar

Figured it out. I switch the Bounding Box on the sprites from Automatic to Full Image and that fixed it.

Oh, of course. That is such an easy thing to miss. In almost every case bounding box is a best collision option. Certainly for any tile-based game or platformer. It really should be the default setting. Glad you figured it out.

Abusing forum power since 1986.

Offline

## #25 2014-06-16 14:22:26

koltz
Member
Registered: 2014-05-26
Posts: 15

### Re: Help with instance_nth_nearest or something similar

One quick minor issue that I can't understand. This is the code that I am using for the movement based on character pressed. I am needing to count the number of incorrect keys.

Expandif (isMoving == false)
{
// Disable keys by default.
var key_up = -1;
var key_down = -1;
var key_left = -1;
var key_right = -1;
// Enable keys for neighboring instances, if any.
// I'm assuming object_instance is a key code.

key_up = collision_point(x, y-32, obj_letter, false, false);
key_down = collision_point(x, y+32, obj_letter, false, false);
key_left = collision_point(x-32, y, obj_letter, false, false);
key_right = collision_point(x+32, y, obj_letter, false, false);

if (instance_exists(key_up))
{
if (keyboard_check(key_up.object_instance))
{
isMoving = true;
moveTimer = gridSize;
speedX = 0;
speedY = -moveSpeed;
movingToward = key_up;
}
}
if (instance_exists(key_down))
{
if (keyboard_check(key_down.object_instance))
{
isMoving = true;
moveTimer = gridSize;
speedX = 0;
speedY = moveSpeed;
movingToward = key_down;
}
}
if (instance_exists(key_left))
{
if (keyboard_check(key_left.object_instance))
{
isMoving = true;
moveTimer = gridSize;
speedX = -moveSpeed;
speedY = 0;
movingToward = key_left;
}
}
if (instance_exists(key_right))
{
if (keyboard_check(key_right.object_instance))
{
isMoving = true;
moveTimer = gridSize;
speedX = moveSpeed;
speedY = 0;
movingToward = key_right;
}
}
else {
global.mistakes += 1;
}
}
if (isMoving == true)
{
x += speedX;
y += speedY;

moveTimer -= moveSpeed;
if (moveTimer == 0) isMoving = false;
}

In the bolded/italiced area, I just added a global.mistakes +=1 and it counts but it counts more than just wrong letters pressed. Any idea how I can count the wrong letters properly?

Offline

## #26 2014-06-16 14:28:19

koltz
Member
Registered: 2014-05-26
Posts: 15

### Re: Help with instance_nth_nearest or something similar

Ok, I guess it didn't bold or whatever,

else {
global.mistakes += 1;
}

Offline

## #27 2014-06-16 22:39:34

xot
Registered: 2007-08-18
Posts: 1,239

### Re: Help with instance_nth_nearest or something similar

What you have there will count up whenever there isn't a key press to the right. Remove that else statement, we'll try something differently.

I think the simplest thing to do is this:

1. At the top, set a flag if any key has been pressed.

2. When you are checking specific keys, if any of them are correct, clear the flag.

3. At the bottom, if the flag is set, increase the mistakes counter.

Since true and false are the same as 1 and 0, I can simplify this a little.

Expandif (isMoving == false)
{
// Disable keys by default.
var key_up = -1;
var key_down = -1;
var key_left = -1;
var key_right = -1;
// Enable keys for neighboring instances, if any.
// I'm assuming object_instance is a key code.

key_up = collision_point(x, y-32, obj_letter, false, false);
key_down = collision_point(x, y+32, obj_letter, false, false);
key_left = collision_point(x-32, y, obj_letter, false, false);
key_right = collision_point(x+32, y, obj_letter, false, false);

var mistake = keyboard_check_pressed(vk_any); //  <-------- CHANGE 1

if (instance_exists(key_up))
{
if (keyboard_check(key_up.object_instance))
{
isMoving = true;
moveTimer = gridSize;
speedX = 0;
speedY = -moveSpeed;
movingToward = key_up;
mistake = 0; //  <-------- CHANGE 2
}
}
// ... REPEAT CHANGE 2 FOR THE OTHER KEYBOARD CHECKS

global.mistakes += mistake; //  <-------- CHANGE 3
}
if (isMoving == true)
{
x += speedX;
y += speedY;

moveTimer -= moveSpeed;
if (moveTimer == 0) isMoving = false;
}

Abusing forum power since 1986.

Offline

## #28 2014-06-17 07:29:12

koltz
Member
Registered: 2014-05-26
Posts: 15

### Re: Help with instance_nth_nearest or something similar

You know, I had sort of that same idea after I posted here. In detecting correct key press, if pressed, change variable to true, if variable is still false increase by one. I will give yours a try.

Thanks again for the help.

Offline