Help with applying movement to another

J

Jmarlin3

Guest
I'm having trouble with one object operating based on what another does correctly.
Here's what it's supposed to do:
Based on the direction of what obj_player is facing, obj_player_head should travel that direction when the 'Z' button is pressed.
Code for Obj_player:
Code:
/// Movements

//Get the player's inputs
//all of these return 1 or 0
key_right = keyboard_check(vk_right);//red means it's a constant
key_left = -keyboard_check(vk_left);//the '-' is there to return the negative of the
//original value
key_jump = keyboard_check_pressed(vk_space); //pressed means you have to pressed
//every single time

key_down = keyboard_check(vk_down);

//React to inputs
move = key_left + key_right; // based off numbers returned, that determines if move left or right
hsp = move * movespeed; //number returned from move times movespeed

if (vsp < 10) vsp += grav; //adds vsp to grav once it's less than 10 pixels

if (place_meeting(x,y+1,obj_wall)) //if one pixel below obj_player there is a wall
{   
    if (key_jump) vsp = -jumpspeed //sets to 0
}

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall))
{
    while(!place_meeting(x+sign(hsp),y,obj_wall))
    {
        x += sign(hsp);//sign returns 1 or -1 based
    //if hsp is positive or negative
    }
    hsp = 0; //stops moving
}

//Vertical Collision
if (place_meeting(x,y+vsp,obj_wall))
{
    while(!place_meeting(x,y+sign(vsp),obj_wall))
    {
        y += sign(vsp);//sign returns 1 or -1 based
    //if vsp is positive or negative
    }
    vsp = 0; //stops moving
}
x += hsp; //add horizontal speed to x
y += vsp; //add vertical speed to y

//Animate
//this means if move is either 1 or -1, it flip the sprite vertically in that direction
if (move != 0) image_xscale = move;
And here's the code for obj_player_head:
Code:
/// @description Insert description here
// You can write your code in this editor


//while head is less than 150, keep adding to x
//when z is held, increase x axis
//when x axis is 150, set hsp back to 0

//keyboard_key = keyboard_check_pressed(ord("Z"));
if (keyboard_check_pressed(ord("Z")))
{
    if (obj_player.move == -1){
        movespeed = -1 * movespeed
        hsp = movespeed
    }
    else if (obj_player.move == 1){
            movespeed = 1 * movespeed
            hsp = movespeed
    }
    
    //else if (obj_player.image_xscale == 1){
        //movespeed = obj_player.image_xscale * movespeed
        //hsp = movespeed
    //}
        
    
    x += hsp; //add horizontal speed to x
}
//else
//{
    //x = obj_player.x;
    //y = obj_player.y;   
    
//}
I tried to have based on obj_player.move is set as. From my understanding, based on the number .move returns (-1 or 1), obj_player_head should move that direction as well. What am I missing here?
 

Nidoking

Member
movespeed = -1 * movespeed
I don't think this line is doing what you think it's doing. This is switching the sign of movespeed every step when obj_player.move is negative. You probably just want hsp = -1 * movespeed there, and hsp = movespeed in the other one. movespeed should remain a constant positive value.
You're also likely to find the head moving through walls because it's not doing the same collision checks.
 
J

Jmarlin3

Guest
I don't think this line is doing what you think it's doing. This is switching the sign of movespeed every step when obj_player.move is negative. You probably just want hsp = -1 * movespeed there, and hsp = movespeed in the other one. movespeed should remain a constant positive value.
You're also likely to find the head moving through walls because it's not doing the same collision checks.
I see! Thank You! Ok, now it's kind of doing what I want it to do. It only moves after I use key_right or key_left, then it moves in that specific direction. I instead want it to move based on which direction obj_player is facing, regardless of the last input. I originally tried to use obj_playe.hsp rather than .move, but sadly it has the same effect to what you suggested...
 

Nidoking

Member
For that, I would stick another variable in the obj_player called facing, and set it to 1 for right and -1 for left, or something similar. Then you check that variable rather than hsp or move, which will change based on player movement.
 
Top