J
J.T.
Guest
Hello! I'm going through the "Breakthrough" tutorial and was just wondering why one code worked and one doesn't . . . This is concerning the part where the bat must be coded to stay within the boundaries of the game. This is the correct code in a Step event:
if keyboard_check(vk_right)
// Check for the right arrow key being held down
{
// This check is to make sure the bat doesn't go out of
// the room to the right
if x < room_width - sprite_xoffset - spd
{
x += spd;
}
else
{
x = room_width - sprite_xoffset;
}
}
But I'm wondering why this wouldn't work as well:
if keyboard_check(vk_right)
// Check for the right arrow key being held down
{
// This check is to make sure the bat doesn't go out of
// the room to the right
if x < bbox_right
{
x += spd;
}
else
{
x = room_width - sprite_xoffset;
}
}
When I use the second option the object continues to go off screen, but when I use bbox (in an intersect boundary event) to contain the ball within the game screen there is no problem:
if bbox_top < 0
{
// Reverse the vertical component of the speed vector
vspeed *= -1
}
Can someone explain the logic of this to me? Thank you!
if keyboard_check(vk_right)
// Check for the right arrow key being held down
{
// This check is to make sure the bat doesn't go out of
// the room to the right
if x < room_width - sprite_xoffset - spd
{
x += spd;
}
else
{
x = room_width - sprite_xoffset;
}
}
But I'm wondering why this wouldn't work as well:
if keyboard_check(vk_right)
// Check for the right arrow key being held down
{
// This check is to make sure the bat doesn't go out of
// the room to the right
if x < bbox_right
{
x += spd;
}
else
{
x = room_width - sprite_xoffset;
}
}
When I use the second option the object continues to go off screen, but when I use bbox (in an intersect boundary event) to contain the ball within the game screen there is no problem:
if bbox_top < 0
{
// Reverse the vertical component of the speed vector
vspeed *= -1
}
Can someone explain the logic of this to me? Thank you!
Last edited by a moderator: