Error: I can press e to lift objBox, but both the player object and objBox fly upward when player moves.
//Create Event of objBox
grav = 0.4;
max_vspeed = 10;
vspeed_ = 0; // Using vspeed_ since vspeed is a built-in variable
hspeed_ = 0; // Using hspeed_ since hspeed is a built-in variable
on_ground = false;
is_carried = false;
carrier = noone;
carrier_facing_right = true;
//Step Event of objBox
if (!is_carried) {
// Floor collision
if (place_meeting(x, y + vspeed_, objSolid)) {
while (!place_meeting(x, y + sign(vspeed_), objSolid)) {
y += sign(vspeed_);
}
vspeed_ = 0;
on_ground = true;
} else {
on_ground = false;
}
code
// Apply gravity when not carried
if (!on_ground) {
vspeed_ += grav;
if (vspeed_ > max_vspeed) {
vspeed_ = max_vspeed;
}
}
// Move
x += hspeed_;
y += vspeed_;
// Check for player pickup
var player_nearby = collision_rectangle(x - 16, y - 16, x + sprite_width + 16,
y + sprite_height + 16, Object1, false, true);
if (player_nearby != noone && keyboard_check_pressed(ord("E"))) {
is_carried = true;
carrier = player_nearby;
carrier_facing_right = (carrier.image_xscale > 0);
}
} else {
// When carried, follow the player
carrier_facing_right = (carrier.image_xscale > 0);
var offset_x = carrier_facing_right ? 32 : -32;
var offset_y = -32;
code
x = carrier.x + offset_x;
y = carrier.y + offset_y;
hspeed_ = 0;
vspeed_ = 0;
// Drop the box
if (keyboard_check_pressed(ord("E"))) {
is_carried = false;
carrier = noone;
// Check if there's space to drop the box
if (place_meeting(x, y, objSolid)) {
// Try to find nearby empty space
var test_distance = 0;
while (test_distance < 32) {
test_distance += 1;
// Check left
if (!place_meeting(x - test_distance, y, objSolid)) {
x -= test_distance;
break;
}
// Check right
if (!place_meeting(x + test_distance, y, objSolid)) {
x += test_distance;
break;
}
}
}
}
Assistant
}
//part of step event of player object
var carryingBox = false;
with (objBox) {
if (is_carried && carrier == other.id) {
carryingBox = true;
break;
}
}
code
// Modify player's movement speed when carrying
if (carryingBox) {
walkSpeed = 1.296875 * 0.75; // Slow down when carrying
canMinJump = false; // Disable short hops while carrying
// Optionally disable other abilities while carrying
} else {
walkSpeed = 1.296875; // Normal speed
// Reset other abilities
}
It sounds like you're experiencing an issue where both the player and the objBox are moving upward unintentionally when the player moves while carrying the box. Let's break down potential causes and provide solutions to fix this behavior.
1. Incorrect Comparison in Player's Step Event
Issue:
In your player's Step Event, you're comparing the carrier (which is an instance of the player) to other.id (which is likely an integer representing the instance ID). This mismatch can cause the condition to fail unexpectedly.
Current Code:
gml
with (objBox) {
if (is_carried && carrier == other.id) {
carryingBox = true;
break;
}
}
Solution:
Compare the carrier directly to the player instance (other) instead of other.id. This ensures that you're comparing two instances rather than an instance to an integer.
Updated Code:
gml
with (objBox) {
if (is_carried && carrier == other) {
carryingBox = true;
break;
}
}
2. Potential Position Conflicts Between Player and Box
Issue:
When the box is carried, its position is directly tied to the player's position. If there's any logic in the player’s movement code that depends on or modifies the box's position, it could lead to unintended side effects like upward movement.
Solution:
Ensure that the player's movement logic doesn't inadvertently modify the box's position. The box should independently follow the player's position without affecting the player's own coordinates.
3. Gravity and Vertical Speed Management
Issue:
Both the player and the box might have their own gravity and vertical speed (vspeed) variables. If the box's movement affects the player's vspeed, it could cause both to move upward.
Solution:
For the Box (objBox):
When carried, ensure that all gravity-related variables are properly managed to prevent unintended movement.
You're already setting vspeed_ = 0 when carried, which is good. Make sure no other parts of the code modify vspeed_ when the box is carried.
For the Player:
Ensure the player's vspeed is handled separately and isn't being influenced by the box's movement. Check the player's physics and movement code to confirm that carrying the box doesn't interfere with gravity or movement logic.
4. Debugging Position Updates
Issue:
It's unclear where exactly the upward movement originates without additional debugging information.
Solution:
Add debug messages to monitor the positions of both the player and the box each step. This can help identify if and when their y positions are being incorrectly modified.
Example Debug Code:
gml
// In both player and objBox Step Events
show_debug_message("Player Y: " + string(y) + ", Box Y: " + string(objBox.y));
5. Review Offset Values When Carrying the Box
Issue:
The offset values used to position the box relative to the player might be causing it to appear above the player, and if combined with other movement logic, could push the player upward.
Current Code:
gml
var offset_x = carrier_facing_right ? 32 : -32;
var offset_y = -32;
x = carrier.x + offset_x;
y = carrier.y + offset_y;
Solution:
Adjust offset_y: Ensure that the offset doesn't place the box in a position that conflicts with the player's collision or gravity logic.
Alternative Approach: Instead of setting the box's y position directly, consider parenting or using a joint to attach the box to the player, which can provide more controlled movement.
6. Ensure Single Instance Carrying
Issue:
Multiple instances of objBox being carried simultaneously or conflicting interactions can cause unpredictable behavior.
Solution:
Limit Carrying to One Box at a Time: Ensure that the player can carry only one box at a time.
Prevent Multiple Boxes from Being Carried: Add checks to prevent multiple boxes from being carried or interfering with each other.
7. Comprehensive Code Review
Go through both the player and objBox scripts to ensure there are no unintended interactions affecting their positions. Pay special attention to any sections of the code that modify x and y coordinates, gravity, or collision handling.
Revised objBox Step Event Example
Here’s an example of how you might revise the objBox Step Event to incorporate some of these fixes:
gml
// Step Event of objBox
if (!is_carried) {
// Floor collision
if (place_meeting(x, y + vspeed_, objSolid)) {
while (!place_meeting(x, y + sign(vspeed_), objSolid)) {
y += sign(vspeed_);
}
vspeed_ = 0;
on_ground = true;
} else {
on_ground = false;
}
// Apply gravity when not carried
if (!on_ground) {
vspeed_ += grav;
if (vspeed_ > max_vspeed) {
vspeed_ = max_vspeed;
}
}
// Move
x += hspeed_;
y += vspeed_;
// Check for player pickup
var player_nearby = collision_rectangle(x - 16, y - 16, x + sprite_width + 16,
y + sprite_height + 16, Object1, false, true);
if (player_nearby != noone && keyboard_check_pressed(ord("E"))) {
is_carried = true;
carrier = player_nearby;
carrier_facing_right = (carrier.image_xscale > 0);
// Optional: Reset box's vertical speed when picked up
vspeed_ = 0;
}
} else {
// When carried, follow the player
carrier_facing_right = (carrier.image_xscale > 0);
var offset_x = carrier_facing_right ? 32 : -32;
var offset_y = -32;
x = carrier.x + offset_x;
y = carrier.y + offset_y;
hspeed_ = 0;
vspeed_ = 0;
// Drop the box
if (keyboard_check_pressed(ord("E"))) {
is_carried = false;
carrier = noone;
// Check if there's space to drop the box
if (place_meeting(x, y, objSolid)) {
// Try to find nearby empty space
var test_distance = 0;
while (test_distance < 32) {
test_distance += 1;
// Check left
if (!place_meeting(x - test_distance, y, objSolid)) {
x -= test_distance;
break;
}
// Check right
if (!place_meeting(x + test_distance, y, objSolid)) {
x += test_distance;
break;
}
}
}
}
}
Final Thoughts
By ensuring that comparisons between instances are accurate, managing gravity and movement logic correctly, and thoroughly debugging the position updates, you should be able to resolve the issue where both the player and the box fly upward when the player moves. If problems persist, consider isolating parts of the code to identify where the unintended behavior originates or providing more detailed code snippets for further assistance.
Summary of Steps to Fix the Issue:
Correct the Carrier Comparison:
Change carrier == other.id to carrier == other in the player's Step Event.
Ensure Independent Movement Logic:
Verify that the box's position updates don't interfere with the player's movement and physics.
Manage Gravity Separately:
Ensure gravity is only applied where appropriate and doesn't cause unintended upward movement.
Add Debugging Statements:
Use debug messages to monitor positions and identify where the upward movement starts.
Review and Adjust Offsets:
Ensure the offsets used when carrying the box don't cause position conflicts.
Limit Carrying to One Box:
Prevent multiple boxes from being carried simultaneously to avoid conflicts.
By following these steps, you should be able to identify and fix the issue causing both the player and the box to fly upward when moving.