Windows Issue with collision checking

J

James71603

Guest
Hello. I am new to forums and would be ecstatic if someone would see what is wrong with my code. I made a script for checking movement. Basically, it checks if there is a wall colliding with the player. It doesnt seem to work and i know there is some other way of doing it but can someone please help?

Here is the script for Left movement:
wallArray[0] = obj_wallStraight
wallArray[1] = obj_wallHorizontal
wallArray[2] = obj_wallLeftCornerBottom
wallArray[3] = obj_wallLeftCornerTop
wallArray[4] = obj_wallRightCornerBottom
wallArray[5] = obj_wallRightCornerTop

left = keyboard_check(vk_left);

if(left && !position_meeting(obj_mazeRunner.x,obj_mazeRunner.y, for(var i= array_length_1d(wallArray); i =< 0; i--;) {
wallArray;
})) x -= 3;
 
J

James71603

Guest
the wink is supposed to be ;_) but without the underscore
 
T

TimothyAllen

Guest
Use code tags [ code] [ /code] without the spaces
Code:
;)
 

Kerberos

Member
I don't understand your code exactly but I would do it like this:
Code:
// You'll need to initialize new variables in the players Create Event:
spd = 3; // the speed of the player
hspd = 0; // speed along the x-axis

// get input
left = keyboard_check(vk_left);
right = keyboard_check(vk_right);

// initialize collision objects
wallArray[0] = obj_wallStraight
wallArray[1] = obj_wallHorizontal
wallArray[2] = obj_wallLeftCornerBottom
wallArray[3] = obj_wallLeftCornerTop
wallArray[4] = obj_wallRightCornerBottom
wallArray[5] = obj_wallRightCornerTop

// determine hspd
hspd = sign(right-left)*spd

for(var i = 0; i < array_length_1d(wallArray); i++)
{
   if(instance_exists(wallArray[i])) // only collide if the object exists in the room
   {
      if(place_meeting(x+hspd, y, wallArray[i]))
      {
         hspd = 0; // stop moving horizontal
      }
   }
}
x += hspd;
Note that you'll have to adjust the collision masks of the objects.
 
Last edited:

Docker

Member
Is there a reason you need them separated into an array and checked using a for loop? you could just give them all the same parent such as par_wall if not and use
Code:
if(left && !position_meeting(obj_mazeRunner.x,obj_mazeRunner.y, par_wall) {
   x -= 3;
}
This is much more efficient...
 
J

James71603

Guest
thanks for the ideas guys! i think i am going to go with making the parent wall, thanks for the help!
 
J

James71603

Guest
here is my new collision script:
//
left = keyboard_check(vk_left);
up = keyboard_check(vk_up);
down = keyboard_check(vk_down);
right = keyboard_check(vk_right);

if(left && !position_meeting(obj_mazeRunner.x - 16|15|14|13,obj_mazeRunner.y, obj_Wall)) x -= 3;
if(right && !position_meeting(obj_mazeRunner.x + 16|15|14|13,obj_mazeRunner.y, obj_Wall)) x += 3;
if(down && !position_meeting(obj_mazeRunner.x,obj_mazeRunner.y - 16|15|14|13, obj_Wall)) y += 3;
if(up && !position_meeting(obj_mazeRunner.x,obj_mazeRunner.y + 16|15|14|13, obj_Wall)) y -= 3;
//
as you guys can see i am very new to coding and thanks for the support!!
 
Top