Implement the ability to pick up and throw objects in Super Mario USA in Gamemaker…
- User
Implement the ability to pick up and throw objects in Super Mario USA in Gamemaker studio 2.
- Assistant
Implement the ability to pick up and throw objects in Super Mario USA in Gamemaker studio 2.
Implementing the ability to pick up and throw objects in a platformer game like Super Mario USA using GameMaker Studio 2 (GMS2) involves several steps. Below is a comprehensive guide, complete with sample code snippets, to help you achieve this functionality.
First, create an object that the player can pick up and throw, for example, obj_ThrowableBox.
obj_ThrowableBox:
spr_ThrowableBox.Create Event:
// Initialization
isHeld = false;
holder = noone;
throw_speed = 10; // Adjust as needed
Step Event:
if (isHeld) {
// Follow the holder (player)
x = holder.x + holder.image_xscale * holder.sprite_width / 2;
y = holder.y - holder.sprite_height / 2;
// Optionally disable physics when held
physics_apply = false;
} else {
// Normal physics and behavior
physics_apply = true;
}
Assuming you have a player object, e.g., obj_Player, you'll need to modify it to handle picking up and throwing.
Variables to Add in Player’s Create Event:
holding_object = noone;
is_holding = false;
throw_strength = 12; // Adjust as needed
Implement collision detection to determine when the player is near a throwable object and allow picking it up.
Step Event in obj_Player:
// Picking up
if (keyboard_check_pressed(ord('Z')) && !is_holding) {
// Check for nearby throwable objects
var obj = instance_nearest(x, y, obj_ThrowableBox);
var distance = point_distance(x, y, obj.x, obj.y);
if (obj != noone && distance < 32) { // Adjust the distance as needed
holding_object = obj;
obj.isHeld = true;
obj.holder = id;
is_holding = true;
// Optionally change player sprite to holding state
sprite_index = spr_PlayerHolding;
}
}
// Throwing
if (keyboard_check_pressed(ord('X')) && is_holding) {
// Determine throw direction based on player facing
var direction = sprite_xscale; // Assuming sprite_xscale is 1 or -1
holding_object.isHeld = false;
holding_object.holder = noone;
// Apply velocity to the thrown object
holding_object.vx = direction * throw_strength;
holding_object.vy = -throw_strength / 2; // Optional: add some upward motion
holding_object = noone;
is_holding = false;
// Revert player sprite to normal
sprite_index = spr_PlayerNormal;
}
Ensure that the thrown object behaves correctly with gravity and collisions.
Step Event in obj_ThrowableBox:
if (!isHeld) {
// Apply gravity
vy += gravity; // Ensure 'gravity' is defined globally or locally
// Apply movement
x += vx;
y += vy;
// Simple collision with the ground
if (place_meeting(x, y + 1, obj_Ground)) {
vy = 0;
y = obj_Ground.y - sprite_height / 2;
vx = 0; // Stop horizontal movement upon landing
}
}
Update the player's sprite or add animations to indicate when holding or throwing an object.
Player Sprite Handling:
spr_PlayerNormalspr_PlayerHoldingspr_PlayerNormal after throwingIf you have multiple types of throwable objects, consider using inheritance by creating a base obj_ThrowableBase and deriving specific objects from it.
After implementing the above steps, thoroughly test the functionality:
Here’s a more integrated code example to put everything together.
obj_Player:
// Create Event
holding_object = noone;
is_holding = false;
throw_strength = 12;
// Step Event
// Picking up
if (keyboard_check_pressed(ord('Z')) && !is_holding) {
// Check for nearby throwable objects
var obj = instance_nearest(x, y, obj_ThrowableBox);
var distance = point_distance(x, y, obj.x, obj.y);
if (obj != noone && distance < 32 && !obj.isHeld) {
holding_object = obj;
obj.isHeld = true;
obj.holder = id;
is_holding = true;
// Change sprite to holding
sprite_index = spr_PlayerHolding;
}
}
// Throwing
if (keyboard_check_pressed(ord('X')) && is_holding) {
// Determine throw direction based on facing
var direction = sprite_xscale; // 1 for right, -1 for left
holding_object.isHeld = false;
holding_object.holder = noone;
// Apply velocity
holding_object.vx = direction * throw_strength;
holding_object.vy = -throw_strength / 2; // Add upward motion if desired
holding_object = noone;
is_holding = false;
// Revert to normal sprite
sprite_index = spr_PlayerNormal;
}
// Move the player (left/right/jump logic goes here)
// Example:
var move_speed = 4;
if (keyboard_check(vk_left)) {
x -= move_speed;
sprite_xscale = -1;
}
if (keyboard_check(vk_right)) {
x += move_speed;
sprite_xscale = 1;
}
// Jumping logic...
obj_ThrowableBox:
// Create Event
isHeld = false;
holder = noone;
vx = 0;
vy = 0;
throw_speed = 10;
// Step Event
if (isHeld) {
// Follow the holder (player)
x = holder.x + holder.image_xscale * holder.sprite_width / 2;
y = holder.y - holder.sprite_height / 2;
// Disable physics when held
vx = 0;
vy = 0;
} else {
// Apply gravity
vy += gravity; // Ensure 'gravity' is defined
// Apply movement
x += vx;
y += vy;
// Collision with ground
if (place_meeting(x, y + 1, obj_Ground)) {
vy = 0;
y = obj_Ground.y - sprite_height / 2;
vx = 0;
}
}
// Optional: Handle collision with walls or other objects
obj_Ground:
obj_Ground) that the thrown object can collide with.By following the steps above and integrating the sample code into your GameMaker Studio 2 project, you should be able to implement a functional pick-up and throw mechanic similar to that in Super Mario USA. Remember to customize and expand upon this foundation to suit the specific needs and mechanics of your game.
If you encounter any issues or have further questions, feel free to ask!
Not yet included in a published release.